mybatis-generator配置及问题

想试试mybatis逆向工程来生成简单sql语句,mybatis-generator真是帮了大忙

生成的文件

  • insert
  • update by primary key
  • update by example (using a dynamic where clause)
  • delete by primary key
  • delete by example (using a dynamic where clause)
  • select by primary key
  • select by example (using a dynamic where clause)
  • count by example

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
62
63
64
65
66
67
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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="mysql" targetRuntime="MyBatis3" defaultModelType="hierarchical">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="utf-8"/>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="useActualColumnNames" value="false"/>
<!--&lt;!&ndash; 分页插件 &ndash;&gt;-->
<!--<plugin type="van.yzt.stock.common.mybatis.ForceMybatisGeneratorPlugin">
</plugin>-->
<!-- 注释生成器 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection
connectionURL="jdbc:mysql://localhost:3306/ssm"
driverClass="com.mysql.jdbc.Driver" password="135799" userId="root">
</jdbcConnection>

<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>

<!-- 生成模型的包名和位置 -->
<!-- 模型放在对应的模块下DAL层 -->
<javaModelGenerator targetPackage="org.spring.springboot.dal.model"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- mapper manager放在对应层 -->
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- mapper接口(Dao)生成的位置 -->
<javaClientGenerator targetPackage="org.spring.springboot.dal.mapper"
targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成哪些表,生成完毕后请注释掉,否则会覆盖他人配置 -->
<!--这里要注意的是一开始没有加schema,生成的代码重复,添加schema问题解决,
Schma即数据库模式-->
<table tableName="t_user" domainObjectName="TUser" schema="xxxx">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="t_login" domainObjectName="TLogin" schema="xxx">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
<table tableName="t_date" domainObjectName="TDate" schema="xxxxx">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>

使用代码方式使用mybatis-generator生成代码

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
public class GeneratorSqlmap {

public void generator() throws Exception {

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
//要注意的是new File()中只能填写src/generatorConfig.xml否则会报错
//java.io.FileNotFoundException: generatorConfig.xml (No such file or directory)
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);

}

public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}

最后项目结构

dal.mapper dal.model resources.mapper都是自动生成的,只要自己写serviceserviceimpl即可

赏个🍗吧
0%