0%

MYBATIS-自动生成代码(SSM)

1:jar包

1
2
3
4
5
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>

配置环境

1
2
3
4
5
6
7
8
9
10
11
<!– 配置自动生成代码 –>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/conf/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

2:配置文件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
<?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>
<!– 配置mysql 驱动jar包路径.用了绝对路径 –>
<classPathEntry location=“D:\Maven\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar” />

<context id=“DB2Tables” targetRuntime=“MyBatis3”>

<!– 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制 –>
<commentGenerator>
<property name=“suppressAllComments” value=“true” />
<property name=“suppressDate” value=“true” />
</commentGenerator>
<!– 注释控制完毕 –>

<!– 数据库连接 –>
<jdbcConnection driverClass=“com.mysql.jdbc.Driver”
connectionURL=“jdbc:mysql://localhost:3306/db_blog”
userId=“root”
password=“root”>
</jdbcConnection>

<javaTypeResolver >
<property name=“forceBigDecimals” value=“false” />
</javaTypeResolver>

<!– 数据表对应的model 层 –>
<javaModelGenerator targetPackage=“net.zdsoft.whb.model” targetProject=“E:\workspace\Blog\src\main\java”>
<property name=“enableSubPackages” value=“true” />
<property name=“trimStrings” value=“true” />
</javaModelGenerator>

<!– sql mapper 隐射配置文件 –>
<sqlMapGenerator targetPackage=“net.zdsoft.whb.mapping” targetProject=“E:\workspace\Blog\src\main\java”>
<property name=“enableSubPackages” value=“true” />
</sqlMapGenerator>

<!– 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 –>
<javaClientGenerator type=“XMLMAPPER” targetPackage=“net.zdsoft.whb.dao” targetProject=“E:\workspace\Blog\src\main\java”>
<property name=“enableSubPackages” value=“true” />
</javaClientGenerator>

<!– 要对那些数据表进行生成操作,必须要有一个. –>
<table tableName=“t_blog” schema=“untodo” domainObjectName=“blog”
enableCountByExample=“false” enableUpdateByExample=“false
enableDeleteByExample=“false” enableSelectByExample=“false
selectByExampleQueryId=“false”>
</table>
<!–
<table tableName=”user” alias=”T” domainObjectName=”User”
enableCountByExample=”false” enableUpdateByExample=”false
enableDeleteByExample=”false” enableSelectByExample=”false
selectByExampleQueryId=”false”>
</table>
–>
</context>
</generatorConfiguration>

3:maven 使用 mybatis-generator:generate