springboot成神之——mybatis和mybatis-generator

时间:2023-06-04 18:40:20

本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成文件

项目结构

springboot成神之——mybatis和mybatis-generator

依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version> <executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<!-- 允许maven打包自动执行generator操作 -->
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions> <configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<!-- <overwrite>true</overwrite> -->
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/MBG.xml</configurationFile>
</configuration> <dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

generator配置文件

MBG.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"></properties> <context id="MYSQLTables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <javaModelGenerator targetPackage="com.springlearn.learn.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <sqlMapGenerator targetPackage="com.springlearn.learn.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.springlearn.learn.DAO" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <table tableName="test" domainObjectName="User" ></table> </context>
</generatorConfiguration>

properties配置

# ===============================
# DATABASE
# =============================== spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&zeroDateTimeBehavior=CONVERT_TO_NULL&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver server.tomcat.uri-encoding=UTF-8
server.port=20001 mybatis.mapper-locations=classpath:com/springlearn/learn/mapper/*.xml
mybatis.configuration.cache-enabled=true

生成文件

运行mvn mybatis-generator:generate

即可生成 model文件,mapper xml文件,dao接口文件

dao接口没有添加@Mapper注释,需要手动添加

使用Example

在生成的model文件中有一个Example文件,这个文件是方便生成where字句的

基本使用demo如下

@RestController
public class TestController {
@Autowired
SqlSession sqlSession; @ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public List<User> Test3(HttpServletRequest request, @RequestParam("id") Integer id){ UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// User result = mapper.selectByPrimaryKey(id); 使用生成的方法访问数据库 UserExample example = new UserExample();
example.setDistinct(true);
example.or().andAgeBetween(20, 30); // 使用Example自动生成where字句
List<User> result = mapper.selectByExample(example); return result;
} }