通过继承PluginAdapter类,开发自定义插件,生成自定义的中文注解的java实体类,dao接口和mapper映射文件.
1.pom.xml添加依赖的jar包:
mysql
mysql-connector-java
5.1.30
org.mybatis
mybatis
3.3.1
org.mybatis.generator
mybatis-generator-core
1.3.5
2.配置generatorConfig.xml:<?xml version="1.0" encoding="UTF-8"?>
以上配置完成,其实就通过generator可以生成默认的代码了,但是我们想生成我们自定义的代码,就需要继承PluginAdapter开发自定义插件来实现:
3.创建GenPlugin.java继承PluginAdapter:
1).把数据库字段的注释作为生成的实体类字段注释,创建GenCommentGenerator.java类,实现CommentGenerator.java,具体实现如下:/**
* 给字段添加数据库备注
* @param field
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
field.addJavaDocLine("//" + introspectedColumn.getRemarks());
}
}/**
* getter方法注释
* @param method
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
sb.append(" * 获取");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *");
}
sb.setLength(0);
sb.append(" * @return ");
sb.append(introspectedColumn.getActualColumnName());
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
sb.append(" - ");
sb.append(introspectedColumn.getRemarks());
}
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */");
}/**
* setter方法注释
* @param method
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
sb.append(" * 设置");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *");
}
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.append(parm.getName());
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
}
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */");
}
在GenPlugin.java重写方法setContext:// 注释生成器
private CommentGeneratorConfiguration commentCfg;
@Override
public void setContext(Context context) {
super.setContext(context);
// 设置默认的注释生成器
commentCfg = new CommentGeneratorConfiguration();
commentCfg.setConfigurationType(GenCommentGenerator.class.getCanonicalName());
context.setCommentGeneratorConfiguration(commentCfg);
// 支持oracle获取注释#114
context.getJdbcConnectionConfiguration().addProperty("remarksReporting", "true");
}
2).创建基础mapper接口BaseMapper.java,重写clientGenerated方法,让generator自动生成的接口继承BaseMapper.java:package com.xe.demo.mapper;
import java.io.Serializable;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.xe.demo.common.pojo.PageModel;
/**
* Mybatis基础DAO
* @author 蔡泽华
*/
public interface BaseMapper {
/**
* 通过ID查询
* @param id
* @return
*/
Object selectById(Serializable id);
/**
* 查询单条记录
* @param entity
* @return
*/
Object selectOne(@Param("item")Object obj);
/**
* 查询记录集合
* @param entity
* @return
*/
List> selectList(@Param("item")Object obj);
/**
* 分页查询
* @param t
* @param page
* @return
*/
List selectPage(@Param("item")Object obj, @Param("page")PageModel page);
/**
* 通用的保存方法
* @param
* @param entity
*/
void save(@Param("item")Object obj);
/**
* 批量保存
* @param list
*/
int batchSave(List> list);
/**
* 通用的修改方法
* @param
* @param entity
*/
int update(@Param("item")Object obj);
/**
* 批量更新
* @param list
* @return
*/
int batchUpdate(List> list);
/**
* 删除方法
* @param id
*/
int delById(Serializable id);
/**
* 批量删除
* @param list
* @return
*/
int delList(List> list);
/**
* 批量删除方法
* @param ids
*/
int delArray(int[] ids);
/**
* 统计查询
* @param
* @param params 查询参数
* @return 总记录条数
*/
int count(Object obj);
}private Set mappers = new HashSet();
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
String mappers = this.properties.getProperty("mappers");
for (String mapper : mappers.split(",")) {
this.mappers.add(mapper);
}
}
/**
* 生成的Mapper接口
* @param interfaze
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
// 获取实体类
FullyQualifiedJavaType entityType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
// import接口
for (String mapper : mappers) {
interfaze.addImportedType(new FullyQualifiedJavaType(mapper));
interfaze.addSuperInterface(new FullyQualifiedJavaType(mapper + ""));
}
// import实体类
interfaze.addImportedType(entityType);
return true;
}
3).重写sqlMapDocumentGenerated方法,让生成BaseMapper.java接口对应的映射文件:/**
* 拼装SQL语句生成Mapper接口映射文件
*/
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
XmlElement rootElement = document.getRootElement();
// 数据库表名
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
// 主键
IntrospectedColumn pkColumn = introspectedTable.getPrimaryKeyColumns().get(0);
// 公共字段
StringBuilder columnSQL = new StringBuilder();
// IF判断语句
StringBuilder ifSQL = new StringBuilder();
// 要插入的字段(排除自增主键)
StringBuilder saveColumn = new StringBuilder("insert into ").append(tableName).append("(\n");
// 要保存的值
StringBuilder saveValue = new StringBuilder("(\n");
// 拼装更新字段
StringBuilder updateSQL = new StringBuilder("update ").append(tableName).append(" set ").append(pkColumn.getActualColumnName())
.append(" = #{item.").append(pkColumn.getJavaProperty()).append("}\n");
// 数据库字段名
String columnName = null;
// java字段名
String javaProperty = null;
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
columnName = MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn);
javaProperty = introspectedColumn.getJavaProperty();
// 拼接字段
columnSQL.append(columnName).append(",");
// 拼接IF语句
ifSQL.append(" ");
ifSQL.append("and ").append(columnName).append(" = #{item.").append(javaProperty).append("}\n");
// 拼接SQL
if (!introspectedColumn.isAutoIncrement()) {
saveColumn.append("\t , ").append(columnName).append("\n");
saveValue.append("\t , ").append("#{item.").append(javaProperty)
.append("}\n");
updateSQL.append(" ");
updateSQL.append(", ").append(columnName).append(" = #{item.").append(javaProperty).append("}\n");
}
}
String columns = columnSQL.substring(0, columnSQL.length() - 1);
rootElement.addElement(createSql("sql_columns", columns));
String whereSQL = MessageFormat.format("\n{0}\t", ifSQL.toString());
rootElement.addElement(createSql("sql_where", whereSQL));
rootElement.addElement(createSelect("selectById", tableName, pkColumn));
rootElement.addElement(createSelect("selectOne", tableName, null));
rootElement.addElement(createSelect("selectList", tableName, null));
rootElement.addElement(createSelect("selectPage", tableName, null));
rootElement.addElement(createSql("sql_save_columns", saveColumn.append("\t) values").toString().replaceFirst(",", "")));
rootElement.addElement(createSql("sql_save_values", saveValue.append("\t)").toString().replaceFirst(",", "")));
rootElement.addElement(createSave("save", pkColumn));
rootElement.addElement(createSave("batchSave", null));
updateSQL.append("\twhere ").append(pkColumn.getActualColumnName()).append(" = #{item.").append(pkColumn.getJavaProperty()).append("}");
rootElement.addElement(createSql("sql_update", updateSQL.toString()));
rootElement.addElement(createUpdate("update"));
rootElement.addElement(createUpdate("batchUpdate"));
rootElement.addElement(createDels(tableName, pkColumn, "delArray", "array"));
rootElement.addElement(createDels(tableName, pkColumn, "delList", "list"));
return super.sqlMapDocumentGenerated(document, introspectedTable);
}
/**
* 公共SQL
* @param id
* @param sqlStr
* @return
*/
private XmlElement createSql(String id, String sqlStr) {
XmlElement sql = new XmlElement("sql");
sql.addAttribute(new Attribute("id", id));
sql.addElement(new TextElement(sqlStr));
return sql;
}
/**
* 查询
* @param id
* @param tableName
* @param pkColumn
* @return
*/
private XmlElement createSelect(String id, String tableName, IntrospectedColumn pkColumn) {
XmlElement select = new XmlElement("select");
select.addAttribute(new Attribute("id", id));
select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
StringBuilder selectStr = new StringBuilder("select from ").append(tableName);
if (null != pkColumn) {
selectStr.append(" where ").append(pkColumn.getActualColumnName()).append(" = #{").append(pkColumn.getJavaProperty()).append("}");
} else {
selectStr.append(" ");
}
if ("selectPage".equals(id)) {
selectStr.append(" limit #{page.startRow}, #{page.pageSize}");
}
select.addElement(new TextElement(selectStr.toString()));
return select;
}
/**
* 保存
* @param id
* @param pkColumn
* @return
*/
private XmlElement createSave(String id, IntrospectedColumn pkColumn) {
XmlElement save = new XmlElement("insert");
save.addAttribute(new Attribute("id", id));
if (null != pkColumn) {
save.addAttribute(new Attribute("keyProperty", "item." + pkColumn.getJavaProperty()));
save.addAttribute(new Attribute("useGeneratedKeys", "true"));
save.addElement(new TextElement(""));
} else {
StringBuilder saveStr = new StringBuilder(
"\n\t ")
.append("\n\t");
save.addElement(new TextElement(saveStr.toString()));
}
return save;
}
/**
* 更新
* @param id
* @return
*/
private XmlElement createUpdate(String id) {
XmlElement update = new XmlElement("update");
update.addAttribute(new Attribute("id", id));
if ("update".equals(id)) {
update.addElement(new TextElement(""));
} else {
update.addElement(new TextElement(
"\n\t \n\t"));
}
return update;
}
/**
* 删除
* @param tableName
* @param pkColumn
* @param method
* @param type
* @return
*/
private XmlElement createDels(String tableName, IntrospectedColumn pkColumn, String method, String type) {
XmlElement delete = new XmlElement("delete");
delete.addAttribute(new Attribute("id", method));
StringBuilder deleteStr = new StringBuilder("delete from ").append(tableName).append(" where ").append(pkColumn.getActualColumnName())
.append(" in\n\t")
.append("
.append("\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item}
");delete.addElement(new TextElement(deleteStr.toString()));
return delete;
}
4.执行GenMain.java方法:package com.xe.demo;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GenMain {
public static void main(String[] args) {
//读取文件
File configFile = new File(GenMain.class.getResource("/generatorConfig.xml").getFile());
List warnings = new ArrayList();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
//true:覆盖生成
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = null;
try {
config = cp.parseConfiguration(configFile);
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.err.println("代码成功生成!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行完刷新项目,项目结构前后对比:
下面是自动生成的代码:package com.xe.demo.model;
import java.math.BigDecimal;
public class IMemberOrders {
private Long oid;
//订单号
private String ordercode;
//昵称
private String nickname;
//商品名
private String shopname;
//购买数量
private Short buycount;
//当次花费
private BigDecimal paycount;
//购买时间
private String buytime;
/**
* @return oid
*/
public Long getOid() {
return oid;
}
/**
* @param oid
*/
public void setOid(Long oid) {
this.oid = oid;
}
/**
* 获取订单号
*
* @return ordercode - 订单号
*/
public String getOrdercode() {
return ordercode;
}
/**
* 设置订单号
*
* @param ordercode 订单号
*/
public void setOrdercode(String ordercode) {
this.ordercode = ordercode;
}
/**
* 获取昵称
*
* @return nickname - 昵称
*/
public String getNickname() {
return nickname;
}
/**
* 设置昵称
*
* @param nickname 昵称
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* 获取商品名
*
* @return shopname - 商品名
*/
public String getShopname() {
return shopname;
}
/**
* 设置商品名
*
* @param shopname 商品名
*/
public void setShopname(String shopname) {
this.shopname = shopname;
}
/**
* 获取购买数量
*
* @return buycount - 购买数量
*/
public Short getBuycount() {
return buycount;
}
/**
* 设置购买数量
*
* @param buycount 购买数量
*/
public void setBuycount(Short buycount) {
this.buycount = buycount;
}
/**
* 获取当次花费
*
* @return paycount - 当次花费
*/
public BigDecimal getPaycount() {
return paycount;
}
/**
* 设置当次花费
*
* @param paycount 当次花费
*/
public void setPaycount(BigDecimal paycount) {
this.paycount = paycount;
}
/**
* 获取购买时间
*
* @return buytime - 购买时间
*/
public String getBuytime() {
return buytime;
}
/**
* 设置购买时间
*
* @param buytime 购买时间
*/
public void setBuytime(String buytime) {
this.buytime = buytime;
}
}package com.xe.demo.mapper;
import com.xe.demo.model.IMemberOrders;
public interface IMemberOrdersMapper extends BaseMapper {
}<?xml version="1.0" encoding="UTF-8"?>
oid,ordercode,nickname,shopname,buycount,paycount,buytime
and oid = #{item.oid}
and ordercode = #{item.ordercode}
and nickname = #{item.nickname}
and shopname = #{item.shopname}
and buycount = #{item.buycount}
and paycount = #{item.paycount}
and buytime = #{item.buytime}
select from i_member_orders where oid = #{oid}
select from i_member_orders
select from i_member_orders
select from i_member_orders limit #{page.startRow}, #{page.pageSize}
insert into i_member_orders(
ordercode
, nickname
, shopname
, buycount
, paycount
, buytime
) values
(
#{item.ordercode}
, #{item.nickname}
, #{item.shopname}
, #{item.buycount}
, #{item.paycount}
, #{item.buytime}
)
update i_member_orders set oid = #{item.oid}
, ordercode = #{item.ordercode}
, nickname = #{item.nickname}
, shopname = #{item.shopname}
, buycount = #{item.buycount}
, paycount = #{item.paycount}
, buytime = #{item.buytime}
where oid = #{item.oid}
delete from i_member_orders where oid in
#{item}
delete from i_member_orders where oid in
#{item}