0%

Mybatis-Generator-1.4 使用 MyBatis3DynamicSql

依赖与插件

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
<?xml version="1.0" encoding="UTF-8"?>
<project>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.dynamic-sql</groupId>
<artifactId>mybatis-dynamic-sql</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--启用覆盖-->
<overwrite>true</overwrite>
<!--自动生成配置 如果名字是generatorConfig.xml可以省略配置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

这里我把其它的东西删掉了,需要添加的是上面的依赖与插件,因为使用的是MyBatis3DynamicSql,所以要mybatis-dynamic-sql依赖。

代码生成器配置

新建src/main/resources/generatorConfig.xml

src/main/resources/generatorConfig.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
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--targetRuntime的选择请看官网-->
<context id="jdbc" targetRuntime="MyBatis3DynamicSql">
<!--注释覆盖,自己试一下多生成几次就知道了-->
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--jdbc连接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/lost_and_found?serverTimezone=CST&amp;useSSL=false"
userId="root" password="root">
<!--MySQL高版本专属,想要生成某个库的所有表就要把这个加上-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>

<!--类型解析器-->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<!--是否试用jdk8时间类-->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<!--实体生成位置-->
<javaModelGenerator targetPackage="top.lenjon.generator.model" targetProject="src/main/java"/>
<!--dao位置-->
<javaClientGenerator targetPackage="top.lenjon.generator.mapper" targetProject="src/main/java"/>
<!--选表,一开始学习的时候选一张表就够了-->
<!-- <table tableName="tableName"/>-->
<!--生成所有表-->
<table tableName="%"/>
</context>
</generatorConfiguration>

生成

image-20210305122115726

双击它

使用示例

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
package top.lenjon.generator;

import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import top.lenjon.generator.mapper.RootMapper;
import top.lenjon.generator.model.Root;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static top.lenjon.generator.mapper.RootDynamicSqlSupport.*;

@SpringBootTest
@AutoConfigureMockMvc
class GeneratorApplicationTests {
@Autowired
RootMapper rootMapper;


@Test
void contextLoads() {
SelectStatementProvider selectStatement = select(id)
.from(root)
.where(type, isEqualTo(1))
.and(deleteTime, isNull()).limit(10).offset(4 * 10)
.build()
.render(RenderingStrategies.MYBATIS3);
List<Root> roots = rootMapper.selectMany(selectStatement);
roots.forEach(System.out::println);
}
}

自定义代码注释

TODO

-----------看到底线啦 感谢您的阅读-------------