代码生成

代码生成配置文件位于resources/config/mybatis-generator.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
<?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="oracle" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<!-- 该配置会使生产的 Mapper自动继承 MyMapper -->
<property name="mappers" value="cc.mrbird.common.config.MyMapper" />
<!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为 true -->
<property name="caseSensitive" value="false"/>
</plugin>
<commentGenerator>
<property name="javaFileEncoding" value="UTF-8"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!-- 数据库链接地址账号密码 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mrbird"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- 生成Model类存放位置,即 src/main/resources下的 cc.mrbird.job.domian -->
<javaModelGenerator targetPackage="cc.mrbird.job.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- 生成映射文件存放位置,即 src/main/resources/mapper -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!-- 生成 Dao类存放位置,即 src/main/java下的 cc.mrbird.job.dao -->
<!-- 客户端代码,生成易于使用的针对 Model对象和 XML配置文件的代码
type="ANNOTATEDMAPPER",生成 Java Model 和基于注解的 Mapper对象
type="XMLMAPPER",生成 SQL Map XML文件和独立的 Mapper 接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cc.mrbird.job.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- 配置需要生成的表 -->
<!--
tableName="T_JOB_LOG" 对应数据库表名称;
domainObjectName="JobLog" 对应生成的实体类名称
-->
<table tableName="T_JOB_LOG" domainObjectName="JobLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<generatedKey column="id" sqlStatement="mysql" identity="true"/>
</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
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/config/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>

然后点击:

输入mybatis-generator:generate后点击Execute即可:

生成的代码如下所示:

JobLog.java:

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
@Table(name = "t_job_log")
public class JobLog {

@Id
@GeneratedValue(generator = "JDBC")
@Column(name = "LOG_ID")
private Long logId;

@Column(name = "JOB_ID")
private Long jobId;

@Column(name = "BEAN_NAME")
private String beanName;

@Column(name = "METHOD_NAME")
private String methodName;

@Column(name = "PARAMS")
private String params;

@Column(name = "STATUS")
private String status;

@Column(name = "ERROR")
private String error;

@Column(name = "TIMES")
private Long times;

@Column(name = "CREATE_TIME")
private Date createTime;

// get,set略
}

JobLogMapper.java:

1
2
3
4
5
import cc.mrbird.common.config.MyMapper;
import cc.mrbird.job.domain.JobLog;

public interface JobLogMapper extends MyMapper<JobLog> {
}

JobLogMapper.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cc.mrbird.job.dao.JobLogMapper">
<resultMap id="BaseResultMap" type="cc.mrbird.job.domain.JobLog">
<!--
WARNING - @mbg.generated
-->
<id column="LOG_ID" jdbcType="DECIMAL" property="logId" />
<result column="JOB_ID" jdbcType="DECIMAL" property="jobId" />
<result column="BEAN_NAME" jdbcType="VARCHAR" property="beanName" />
<result column="METHOD_NAME" jdbcType="VARCHAR" property="methodName" />
<result column="PARAMS" jdbcType="VARCHAR" property="params" />
<result column="STATUS" jdbcType="CHAR" property="status" />
<result column="ERROR" jdbcType="VARCHAR" property="error" />
<result column="TIMES" jdbcType="DECIMAL" property="times" />
<result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
</mapper>

代码生成后,最好将pom.xml中刚刚开启的插件重新注释掉。因为在项目打包的时候会自动运行该插件,这样如果生成的代码后续有修改的话,将会被覆盖掉。所以通常的做法是只有在需要生成代码的时候才将该插件开启。


© 2017 - 2018  MrBird & Hosted by Coding Pages