在IDEA里创建一个Spring Boot 框架
因为是初步学习,pom.xml里的依赖不在这里选择,而是在创建完成后,自己添加。
这是创建一个初始Spring boot项目的结构。其它结构要后续添加。
在为pom.xml 添加依赖之前先创建generatorConfig.xml 文件,该文件创建在resources/generator 文件夹里。 代码为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--数据库驱动--> <!--添加mysql驱动的位置--> <classPathEntry location="C:\Program Files\Apache Software Foundation\maven-repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/> <context id="mysql" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8 &characterEncoding=utf-8&useSSL=false&" userId="root" password= "123456" > </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.project.gc.domain" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.project.gc.dao" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.project.gc.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="t_users" domainObjectName="Users"> <generatedKey column="id" sqlStatement="MySql" identity="true" /> </table> <table tableName="t_news" domainObjectName="News"> <generatedKey column="id" sqlStatement="MySql" identity="true" /> </table> <table tableName="t_recruit" domainObjectName="Recruit"> <generatedKey column="id" sqlStatement="MySql" identity="true" /> </table> </context> </generatorConfiguration>
接着在pom.xml里添加依赖代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.project</groupId> <artifactId>gc</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gc</name> <description>gc project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--spring依赖全家桶--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--jdbc依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--spring boot 全家桶 end--> <!--druid数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--generatorConfigurationFile的位置--> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
然后在IDEA里运行mybatis generator 插件
插件运行完后的结构为
接下来在项目里创建controller 、service 文件夹
然后在service 文件夹里创建UserService 代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.project.gc.service; import com.project.gc.dao.UsersMapper; import com.project.gc.domain.Users; import com.project.gc.domain.UsersExample; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UsersService { @Resource private UsersMapper usersMapper; public Users findById(int id){ return usersMapper.selectByPrimaryKey(id); } public Users findByUserName(String userNmae){ UsersExample example = new UsersExample(); example.or().andUsernameEqualTo(userNmae); return usersMapper.selectByExample(example); } public int addUser(Users users){ return usersMapper.insertSelective(users); } }
遇到这个错误的话,将dao/UsersMapper 里selectByExample 方法的返回值类型改成Users
接下来在controller 文件夹里创建UsersController 代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.project.gc.controller; import com.project.gc.domain.Users; import com.project.gc.service.UsersService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UsersController { @Resource private UsersService usersService; @RequestMapping(value = "get",method = RequestMethod.GET) public Users getUserById(@RequestParam(value = "id",required = true) int id){ return usersService.findById(id); } @RequestMapping(value = "username",method = RequestMethod.GET) public Users getUserByUserName(@RequestParam(value = "userName",required = true) String userName){ return usersService.findByUserName(userName); } public Users addUser(Users users){ usersService.addUser(users); return users; } }
接下配置application.properties 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 server.port=8080 #spring设置 spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false&allowPublicKeyRetrieval=true #连接驱动 spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.username=root spring.datasource.druid.password=123456 spring.datasource.druid.initial-size=10 spring.datasource.druid.max-active=50 spring.datasource.druid.min-idle=10 spring.datasource.druid.max-wait=60000 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.test-while-idle=true spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.filters=stat,wall,log4j
然后运行入口程序GcApplication ,发现报错 找不到Mapper 文件
然后在入口程序GcApplication 里添加扫描注解**@MapperScan(“com.project.gc.dao”)**
需要注意的是这个com.project.gc.dao 是resources 里的那个
然后重新运行,即可。 在浏览器地址栏输入http://127.0.0.1:8080/get?id=1 ,会接到json返回值