新项目开始需要根据表生成代码,实现逆向工程因为使用的是Mybatis3决定使用Mybatis-generator来生成代码

学习过程

  1. maven工程 查找依赖jar包(正常)
  2. 发现github上有genertator源码,决定下一份学习(非常不错
  3. 找到配置文件,找到触发生成代码部分(正常)
  4. 做一个测试(正常)
  5. 调试生成代码(正常)
  6. 发现问题,和自己想象生成代码有很大的区别(怀疑代码不能用,继续查找相关配置文件信息及其相关元素配置)(非常错误,因祸得福学习了好多配置的意义写法)
  7. 认识到代码没问题。学习Mybatis的新特性(找到问题的关键进行学习)
  8. 结束

具体过程

通过网页搜索找到一篇基础配置。
generatorConfig.xml位置位于src/main/resources具体内容如下,
重要的是对于table标签的设置、context中targetRuntime、javaClientGenerator中type。

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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>
<classPathEntry location="doc\mysql-connector-java-5.1.22-bin.jar" />
<context id="MysqlTables" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!-- import mapper -->
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="fileName" value="GeneratedMapperConfig.xml" />
</plugin>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码, -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://119.29.115.225:3306/pmpl" userId="pmpl"
password="pmpl!@#">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.cninsure.payment.entity"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<!-- generate dao -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.cninsure.payment.dao" implementationPackage="com.cninsure.payment.dao.impl"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<property name="methodNameCalculator" value="extended" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="pmappchannel" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmappchannellmit" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmappplatform" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmappplatformuserbankcard"
enableCountByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmapprate" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmbankcard" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmimage" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pminterfacelog" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmpaychannel" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmpaychannellmit" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmpayment" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmpaymentplatform" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmratefee" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
<table tableName="pmsettle" enableCountByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false">
<generatedKey column="id" sqlStatement="Mysql" />
</table>
</context>
</generatorConfiguration>

POM文件具体内容运行maven命令mvn mybatis-generator:generate
自动读取src/main/resources下配置文件生成代码。

更加详细的用法查看官方文档http://generator.sturgeon.mopaas.com/running/runningWithMaven.html

1
2
3
4
5
6
7
8
9
10
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

学习文章http://blog.csdn.net/isea533/article/details/42102297

但是官方文档明显给出的解释更加详细。
以后需要注意的地方是具体细节的查看直接看文档能省很多时间。

对于生成的代码存在疑问时,

去搜索mybatis3新特性http://www.cnblogs.com/xdp-gacl/p/4271627.html

这篇文章很详细所有的细节都已经有了。
比较关键的地方是在spring配置文件地方要加一个扫描dao接口的设置

最后的部分

学习一下generator源码