引入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

使用MyBatis Generator

添加Maven插件

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>

这里需要加上mysql驱动的依赖,我第一次没有加,然后自动生成就一直报错。

添加配置文件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> <context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator> <!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssmweb?useSSL=false&amp;serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection> <!--是否应强制对DECIMAL和NUMERIC字段使用java.math.BigDecimal-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!--配置自动生成Model(entity)-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\java">
<!--<property name="enableSubPackages" value="true" />-->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!--配置自动生成Mapper映射文件-->
<sqlMapGenerator targetPackage="mapper" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\resources"> </sqlMapGenerator> <!--配置自动生成dao-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\java"> </javaClientGenerator> <!--配置表信息及类信息-->
<table schema="ssmweb" tableName="emp" domainObjectName="Emp" >
<generatedKey column="emp_id" sqlStatement="MySql" identity="true" />
</table> </context>
</generatorConfiguration>

这里注意:在上面pom.xml中配置的mybatis-generator-maven-plugin插件,是默认加载/src/main/resources/目录下名为generatorConfig.xml。

springboot整合mybatis(使用MyBatis Generator)-LMLPHP

运行插件

双击插件即可运行。

springboot整合mybatis(使用MyBatis Generator)-LMLPHP

运行后目录:

springboot整合mybatis(使用MyBatis Generator)-LMLPHP

正式整合mybatis

配置application.yml

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssmweb?useSSL=false&serverTimezone=UTC
username: root
password: 123456 #mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml

需要配置mapper映射文件位置,不然映射类无法找到映射文件位置,运行会报错。

扫描映射类

在springboot启动类上添加@MapperScan注解

@MapperScan(basePackages = {"com.example.demo.dao"})
@SpringBootApplication
public class SpringbootmybatisdemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisdemoApplication.class, args);
}
}

测试

service:

@Service
public class EmpService {
@Autowired
private EmpMapper empMapper; public List<Emp> getAll(){
return empMapper.selectByExample(null);
}
}

controller:

@RestController
public class EmpController { @Autowired
private EmpService empService; @GetMapping("/emps")
public List<Emp> getAll(){
return empService.getAll();
} }

结果:

springboot整合mybatis(使用MyBatis Generator)-LMLPHP

05-06 17:16