tonglin0325的个人主页

Mybatis学习笔记——mybatis-generator

通用mabatis-generator可以由mysql表自动生成model类,mapper映射文件和mapper接口,参考:MyBatis通用Mapper和PageHelper

1.依赖

1
2
3
4
5
6
7
8
9
10
11
12
<!-- mybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>

2.插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- mybatis-generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>src/main/resources/generator-config.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
</dependencies>
</plugin>

3.在application.properties中配置数据库相关参数

1
2
3
4
5
6
# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=utf-8&amp;useLegacyDatetimeCode=false&amp;serverTimezone=Hongkong&amp;zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=xxxx
spring.datasource.driver=com.mysql.jdbc.Driver

4.配置generator-config.xml,其中通用MyMapper参考:Mybatis学习笔记——通用mapper

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
<?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="context" targetRuntime="MyBatis3Simple">
<!-- MapperPlugin -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<!-- 该配置会使生产的Mapper自动继承MyMapper -->
<property name="mappers" value="com.example.demo.core.mapper.MyMapper" />
<!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
<property name="caseSensitive" value="false"/>
</plugin>
<!-- 去掉生成出来的代码的注解 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 数据库信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="${spring.datasource.url}"
userId="root"
password="xxxx">
</jdbcConnection>
<!-- 生成Model类的包名和位置 -->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成Mapper映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper接口的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成代码的表 tableName=%时为所有表生成 表名字段名都有默认规则生成 -->
<table tableName="%">
<!-- 指定生成的主键属性名 生成SQL语句的类型 -->
<generatedKey column="id" sqlStatement="MySql"/>
</table>
</context>

</generatorConfiguration>

5.生成,点击plugin中的mabatis-generator:generate

6.自动生成model,mapper和xml

7.添加
 @MapperScan(basePackages = “com.example.demo.mapper”) 注解

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

  1. 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class UserMapperTest {

@Resource
private UserMapper mapper;

@Test
public void UserMapper() {
User user = mapper.selectByPrimaryKey(1L);
System.out.println(user.getId());
}

}

如果在需要添加除了自定义SQL到xml文件中的话,需要在application.properties中添加如下配置

1
2
3
4
# mybatis
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:mapper/**/*.xml

否则可能会报如下错误

1
2
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)