简单的理解,MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件。
mybatis需要编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),提高工作效率。
命令:
mvn mybatis-generator:generate
项目结构:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<? 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 = "mysqlgenerator" targetRuntime = "MyBatis3" > < property name = "autoDelimitKeywords" value = "true" /> <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错--> < property name = "beginningDelimiter" value = "`" /> < property name = "endingDelimiter" value = "`" /> <!-- 自动生成toString方法 --> < plugin type = "org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 自动生成equals方法和hashcode方法 --> < plugin type = "org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <!-- 非官方插件 https://github.com/itfsw/mybatis-generator-plugin --> <!-- 查询单条数据插件 --> < plugin type = "com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin" /> <!-- 查询结果选择性返回插件 --> < plugin type = "com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin" /> <!-- Example Criteria 增强插件 --> < plugin type = "com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin" /> <!-- 数据Model属性对应Column获取插件 --> < plugin type = "com.itfsw.mybatis.generator.plugins.ModelColumnPlugin" /> <!-- 逻辑删除插件 --> < plugin type = "com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin" > <!-- 这里配置的是全局逻辑删除列和逻辑删除值,当然在table中配置的值会覆盖该全局配置 --> <!-- 逻辑删除列类型只能为数字、字符串或者布尔类型,数据库中用tinyint(1) --> < property name = "logicalDeleteColumn" value = "deleted" /> <!-- 逻辑删除-已删除值 --> < property name = "logicalDeleteValue" value = "1" /> <!-- 逻辑删除-未删除值 --> < property name = "logicalUnDeleteValue" value = "0" /> </ plugin > < commentGenerator > < property name = "suppressDate" value = "true" /> <!--<property name="suppressAllComments" value="true"/>--> </ commentGenerator > <!--数据库连接信息--> < jdbcConnection driverClass = "com.mysql.jdbc.Driver" connectionURL = "jdbc:mysql://192.168.1.100:3306/theorydance?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false" userId = "root" password = "123456" /> < javaTypeResolver > < property name = "useJSR310Types" value = "true" /> </ javaTypeResolver > < javaModelGenerator targetPackage = "demo.theorydance.db.domain" targetProject = "src/main/java" /> < sqlMapGenerator targetPackage = "demo.theorydance.db.dao" targetProject = "src/main/resources" /> < javaClientGenerator type = "XMLMAPPER" targetPackage = "demo.theorydance.db.dao" targetProject = "src/main/java" /> <!--表名--> < table tableName = "student" ></ table > </ context > </ generatorConfiguration > |
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
|
< build > < plugins > < plugin > < groupId >org.mybatis.generator</ groupId > < artifactId >mybatis-generator-maven-plugin</ artifactId > < version >1.3.7</ version > < configuration > < configurationFile > mybatis-generator/generatorConfig.xml </ configurationFile > < overwrite >true</ overwrite > < verbose >true</ verbose > </ configuration > < dependencies > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.46</ version > </ dependency > < dependency > < groupId >com.itfsw</ groupId > < artifactId >mybatis-generator-plugin</ artifactId > < version >1.2.12</ version > </ dependency > </ dependencies > </ plugin > </ plugins > </ build > |