1、新建spring boot项目;
2、在pom.xml文件中添加对mybatis和mysql的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
3、在application.properties文件中配置数据库连接:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT&2b8&useSSL=false
spring.datasource.username=登录名
spring.datasource.password=登录密码
4、配置mybatis generator,在项目的src/main/resources目录下新建generatorConfig.xml文件,内容如下:
<?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>
<!-- 引入配置文件 -->
<properties resource="application.properties"/>
<context id="mysqlGenerator" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="false"/>
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"/>
<!-- 生成实体类 -->
<javaModelGenerator targetPackage="com.example.mbg.entity" targetProject="${mybatis.project}" />
<!-- 生成mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="${mybatis.resources}" />
<!-- 生成dao接口 -->
<javaClientGenerator targetPackage="com.example.mbg.dao" targetProject="${mybatis.project}" type="XMLMAPPER" />
<!-- 配置表信息-->
<table tableName="数据表名" domainObjectName="该表对应的实体名" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
</context>
</generatorConfiguration>
5、Maven配置mybatis generator,编辑pom.xml文件,替换build/plugins节点下的内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
</dependencies>
<configuration>
<!-- 打印执行过程 -->
<verbose>true</verbose>
<!-- 允许覆盖生成的文件 -->
<overwrite>true</overwrite>
<!-- 配置文件路径 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
6、开始生成,打开IEDA的Maven Projects窗口,双击执行plugins/mybatis-generator目录下的mybatis-generator:generate,插件执行完成;
7、在根目录下新建controller包,在该包下新建名为UserConroller的java类,内容如下:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserMapper userMapper;
@GetMapping("/{id}")
public User getSingle(@PathVariable int id){
return userMapper.selectByPrimaryKey(id);
}
}
8、在application.properties文件中配置mybatis的mapper地址:
mybatis.mapper-locations=mapper/*.xml
#dao类和实体类的位置
mybatis.project =src/main/java
#mapper文件的位置
mybatis.resources=src/main/resources
注:该路径必须与generatorConfig.xml文件中配置的 sqlMapGenerator节点下的targetPackage属性值一致。
9、修改Application入口文件,为类添加注解(指定要扫描的Mapper类的包的路径):
@MapperScan("com.example.test.dao")
注:该包名必须与generatorConfig.xml文件中配置的 javaClientGenerator节点下的targetPackage属性值一致。
10、运行应用,完成。