引用地址:http://arccode.net/2015/02/07/MyBatis-Generator%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/
最近使用MyBatis开发项目,为了快速开发,发现了一个可快速生成mapper类和mapper配置文件及Model的插件-MyBatis-Generator,总结下该插件的使用及最佳实践.
1> 建表-teacher
| 
 1 
2 
3 
4 
5 
6 
 | 
 CREATE TABLE `test`.`teacher` ( 
`id` bigint NOT NULL DEFAULT 0 COMMENT ‘主键id‘, 
`name` varchar(40) NOT NULL DEFAULT ‘‘ COMMENT ‘名称‘, 
`age` smallint NOT NULL DEFAULT 0 COMMENT ‘年龄‘, 
PRIMARY KEY (`id`) 
) COMMENT=‘教师表‘; 
 | 
2> 配置properties常量
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
 | 
 # 数据库驱动jar 路径 
drive.class.path=/Users/arccode/repo/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar 
# 数据库连接参数 
jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 
jdbc.username=mysql 
jdbc.password=mysqlpwd 
# 包路径配置 
model.package=com.arccode.web.model 
dao.package=com.arccode.web.dao 
xml.mapper.package=com.arccode.web.dao 
target.project=src/main/java 
 | 
3> 配置文件-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 
 | 
 <?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> 
<!-- 配置文件路径 --> 
<properties url="${mybatis.generator.generatorConfig.properties}"/> 
<!--数据库驱动包路径 --> 
<classPathEntry location="${drive.class.path}"/> 
<context id="MySQLTables" targetRuntime="MyBatis3"> 
<!--关闭注释 --> 
<commentGenerator>  
<property name="suppressDate" value="true"/> 
</commentGenerator> 
<!--数据库连接信息 --> 
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" 
password="${jdbc.password}"> 
</jdbcConnection> 
<!--生成的model 包路径 --> 
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}"> 
<property name="enableSubPackages" value="ture"/> 
<property name="trimStrings" value="true"/> 
</javaModelGenerator> 
<!--生成xml mapper文件 路径 --> 
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}"> 
<property name="enableSubPackages" value="ture"/> 
</sqlMapGenerator> 
<!-- 生成的Dao接口 的包路径 --> 
<javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}"> 
<property name="enableSubPackages" value="ture"/> 
</javaClientGenerator> 
<!--对应数据库表名 --> 
<table tableName="teacher"> 
</table> 
</context> 
</generatorConfiguration> 
 | 
4> 运行maven - Run As Maven build
Goals 参数 : mybatis-generator:generate -Dmybatis.generator.overwrite=true
修改配置文件-generatorConfig.xml, 将table标签修改如下
| 
 1 
2 
3 
4 
 | 
 <table tableName="teacher" enableCountByExample="false"  
enableUpdateByExample="false" enableDeleteByExample="false"  
enableSelectByExample="false" selectByExampleQueryId="false"> 
</table> 
 | 
修改mybatis-generator源码
位置: mybatis-generator-core/src/main/java/org/mybatis/generator/internal/DefaultCommentGenerator.java
修改该类的方法: addFieldComment
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
 | 
 public void addFieldComment(Field field, 
IntrospectedTable introspectedTable, 
IntrospectedColumn introspectedColumn) { 
if (suppressAllComments) { 
return; 
} 
StringBuilder sb = new StringBuilder(); 
field.addJavaDocLine("/**"); //$NON-NLS-1$ 
// field.addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$ 
sb.append(" * "); //$NON-NLS-1$ 
sb.append(introspectedColumn.getRemarks()); 
sb.append(",所属表字段为"); 
sb.append(introspectedTable.getFullyQualifiedTable()); 
sb.append(‘.‘); 
sb.append(introspectedColumn.getActualColumnName()); 
field.addJavaDocLine(sb.toString()); 
// addJavadocTag(field, false); 
field.addJavaDocLine(" */"); //$NON-NLS-1$ 
} 
 | 
如果不想修改源码, 可以下载mybatis生成中文注释项目, maven本地安装后在pom中配置version即可.
使用git克隆github项目(mybatis-generator源项目)
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
 // 克隆 parent 
git clone https://github.com/mybatis/parent.git 
// 将该项目安装到本地maven库, mybatis-generator依赖于该项目 
mvn clean install -Dmaven.test.skip=true 
// 克隆 mybatis-generator 
git clone https://github.com/mybatis/generator.git 
// 将该jar安装到本地, 之后项目中使用, 时间有点长, 可以喝杯咖啡 
mvn clean install -Dmaven.test.skip=true 
 | 
碰到的问题:
平台: Mac
Jdk: 自带的jdk_1.6.0
解决办法: 安装jdk1.7, 该版本带有tools.jar
在以上三点需求完成后, 还可做以下修改使得开发更快更敏捷
| 
 1 
2 
3 
 | 
 <commentGenerator> 
<property name="suppressDate" value="false"/> 
</commentGenerator> 
 | 
| 
 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 
 | 
 public class Teacher { 
/** 
* 主键id,所属表字段为teacher.id 
*/ 
private Long id; 
/** 
* 名称,所属表字段为teacher.name 
*/ 
private String name; 
/** 
* 年龄,所属表字段为teacher.age 
*/ 
private Short age; 
/** 
* 性别,所属表字段为teacher.sex 
*/ 
private String sex; 
/** 
* 获取 主键id 字段:teacher.id 
* 
* @return teacher.id, 主键id 
*/ 
public Long getId() { 
return id; 
} 
/** 
* 设置 主键id 字段:teacher.id 
* 
* @param id teacher.id, 主键id 
*/ 
public void setId(Long id) { 
this.id = id; 
} 
/** 
* 获取 名称 字段:teacher.name 
* 
* @return teacher.name, 名称 
*/ 
public String getName() { 
return name; 
} 
/** 
* 设置 名称 字段:teacher.name 
* 
* @param name teacher.name, 名称 
*/ 
public void setName(String name) { 
this.name = name == null ? null : name.trim(); 
} 
/** 
* 获取 年龄 字段:teacher.age 
* 
* @return teacher.age, 年龄 
*/ 
public Short getAge() { 
return age; 
} 
/** 
* 设置 年龄 字段:teacher.age 
* 
* @param age teacher.age, 年龄 
*/ 
public void setAge(Short age) { 
this.age = age; 
} 
/** 
* 获取 性别 字段:teacher.sex 
* 
* @return teacher.sex, 性别 
*/ 
public String getSex() { 
return sex; 
} 
/** 
* 设置 性别 字段:teacher.sex 
* 
* @param sex teacher.sex, 性别 
*/ 
public void setSex(String sex) { 
this.sex = sex == null ? null : sex.trim(); 
} 
} 
 | 
为Maven指定tools.jar ,解决Missing artifact com.sun:tools:jar:1.5.0错误
Maven Frequently Asked Technical Questions
原文:http://www.cnblogs.com/AloneSword/p/4883472.html