再新建一个Maven项目,然后把mybatis-generator-core-1.3.5解压,复制org目录到eclipse中去,完整的项目如下:
log4j
log4j
1.2.7
org.apache.ant
ant
1.9.0
com.oracle
ojdbc6
6.0
mysql
mysql-connector-java
5.1.38
里面主要有以下方法:
addJavaFileComment(CompilationUnit compilationUnit) Java文件加注释
addComment(XmlElement xmlElement) Mybatis的Mapper.xml文件里面的注释
addRootComment(XmlElement rootElement) 为根元素的第一个子节点添加注释
addJavadocTag(JavaElement javaElement,boolean markAsDoNotDelete) 添加自定义javadoc标签
addClassComment(InnerClass innerClass,IntrospectedTable introspectedTable) 类注释
addModelClassComment(TopLevelClass topLevelClass,IntrospectedTable introspectedTable) 模型类添加注释
addEnumComment(InnerEnum innerEnum,IntrospectedTable introspectedTable) 枚举注释
addFieldComment(Field field,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) 字段注释
addFieldComment(Field field, IntrospectedTable introspectedTable) 字段注释
addGeneralMethodComment(Method method,IntrospectedTable introspectedTable) 普通方法注释,mapper接口中方法
addGetterComment(Method method,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) Getter方法注释
addSetterComment(Method method,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) Setter方法注释
addClassComment(InnerClass innerClass,IntrospectedTable introspectedTable, boolean markAsDoNotDelete) 类注释
Mybatis-generator源码学习和修改: http://blog.csdn.net/wu6660563/article/details/52093364
大象修改博客:http://www.blogjava.net/bolo/archive/2015/03/25/423826.html
Mybatis Generator的model生成中文注释,支持oracle和mysql(通过修改源码的方式来实现)
修改mybatis-generator源码生成中文注释
我就按照上面的步骤操作,做了一些修改,仅供参考;
1、Mapper.xml空格改成四个空格
修改详情:
org.mybatis.generator.api.dom.OutputUtilities
OutputUtilities 这个类,它里面有个 xmlIndent 方法是用来控制生成的 xml 文件中空格的缩进,默认是两个空格在 sb.append( " " ) 里面增加两个空格就可以了。
2、修改dao包下的名称,由原来的XXXMapper改成XXXDao
修改详情:
org.mybatis.generator.api.IntrospectedTable
calculateJavaClientAttributes方法(大概1335行)将sb.append("Mapper");注释掉改成 sb.append("Dao");
3、修改Mybatis生成Model的字段注释内容:
1)generatorConfig.xml配置里面设置成:
代码修改详情:
org.mybatis.generator.internal.DefaultCommentGenerator里面的addFieldComment方法添加注释代码去掉,改成
field.addJavaDocLine("/**");
field.addJavaDocLine(" * @Fields "+field.getName()+" "+introspectedColumn.getRemarks());
field.addJavaDocLine(" */");
4、修改XXXDao.java(XXXMapper.java)的接口方法的Javadoc注释
代码修改详情:
搜索org.mybatis.generator.internal.DefaultCommentGenerator如下方法,并注释方法里面内容
public void addGeneralMethodComment(Method method,IntrospectedTable introspectedTable)
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
sb.append(" * ");
if (method.isConstructor()) {
sb.append(" 构造查询条件");
}
String method_name = method.getName();
if ("setOrderByClause".equals(method_name)) {
sb.append(" 设置排序字段");
} else if ("setDistinct".equals(method_name)) {
sb.append(" 设置过滤重复数据");
} else if ("getOredCriteria".equals(method_name)) {
sb.append(" 获取当前的查询条件实例");
} else if ("isDistinct".equals(method_name)) {
sb.append(" 是否过滤重复数据");
} else if ("getOrderByClause".equals(method_name)) {
sb.append(" 获取排序字段");
} else if ("createCriteria".equals(method_name)) {
sb.append(" 创建一个查询条件");
} else if ("createCriteriaInternal".equals(method_name)) {
sb.append(" 内部构建查询条件对象");
} else if ("clear".equals(method_name)) {
sb.append(" 清除查询条件");
} else if ("countByExample".equals(method_name)) {
sb.append(" 根据指定的条件获取数据库记录数");
} else if ("deleteByExample".equals(method_name)) {
sb.append(" 根据指定的条件删除数据库符合条件的记录");
} else if ("deleteByPrimaryKey".equals(method_name)) {
sb.append(" 根据主键删除数据库的记录");
} else if ("insert".equals(method_name)) {
sb.append(" 新写入数据库记录");
} else if ("insertSelective".equals(method_name)) {
sb.append(" 动态字段,写入数据库记录");
} else if ("selectByExample".equals(method_name)) {
sb.append(" 根据指定的条件查询符合条件的数据库记录");
} else if ("selectByPrimaryKey".equals(method_name)) {
sb.append(" 根据指定主键获取一条数据库记录");
} else if ("updateByExampleSelective".equals(method_name)) {
sb.append(" 动态根据指定的条件来更新符合条件的数据库记录");
} else if ("updateByExample".equals(method_name)) {
sb.append(" 根据指定的条件来更新符合条件的数据库记录");
} else if ("updateByPrimaryKeySelective".equals(method_name)) {
sb.append(" 动态字段,根据主键来更新符合条件的数据库记录");
} else if ("updateByPrimaryKey".equals(method_name)) {
sb.append(" 根据主键来更新符合条件的数据库记录");
}
sb.append(":");
sb.append(introspectedTable.getFullyQualifiedTable());
method.addJavaDocLine(sb.toString());
final List parameterList = method.getParameters();
if (!parameterList.isEmpty()) {
method.addJavaDocLine(" *");
if ("or".equals(method_name)) {
sb.append(" 增加或者的查询条件,用于构建或者查询");
}
} else {
if ("or".equals(method_name)) {
sb.append(" 创建一个新的或者查询条件");
}
}
String paramterName;
for (Parameter parameter : parameterList) {
sb.setLength(0);
sb.append(" * @param "); //$NON-NLS-1$
paramterName = parameter.getName();
sb.append(paramterName);
if ("orderByClause".equals(paramterName)) {
sb.append(" 排序字段"); //$NON-NLS-1$
} else if ("distinct".equals(paramterName)) {
sb.append(" 是否过滤重复数据");
} else if ("criteria".equals(paramterName)) {
sb.append(" 过滤条件实例");
}
method.addJavaDocLine(sb.toString());
}
method.addJavaDocLine(" */");
5、修改Model里面Getter和Setter方法的注释
代码修改详情:
搜索org.mybatis.generator.internal.DefaultCommentGenerator修改其中的addGetterComment,addSetterComment方法
addGetterComment修改成如下:
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * 获取 "); //$NON-NLS-1$
sb.append(introspectedColumn.getRemarks()).append(" 字段:");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
sb.setLength(0);
sb.append(" * @return "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
sb.append(", ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * 设置 "); //$NON-NLS-1$
sb.append(introspectedColumn.getRemarks()).append(" 字段:");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param "); //$NON-NLS-1$
sb.append(parm.getName());
sb.append(" the value for "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
sb.append(", ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
代码修改详情:
搜索org.mybatis.generator.internal.DefaultCommentGenerator如下方法:
public void addComment(XmlElement xmlElement)
全部注释掉
7、XML里面每个SQL增加一个换行
代码修改详情:
org.mybatis.generator.api.dom.xml.XmlElement里面getFormattedContent里面加上:
if(indentLevel == 1) {
//每个insert/update/select之间插入一个空行
OutputUtilities.newLine(sb);
}
8、修改mybatis的mapper.xml文件里面insert和update不根据字段判断的非动态SQL
代码修改详情:
org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator
里面的getSqlMapElement方法
注释掉:
addInsertElement(answer);
addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
9、添加Java文件注释,版权信息
代码修改详情:
找到
org.mybatis.generator.internal.DefaultCommentGenerator
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
compilationUnit.addFileCommentLine("/*");
compilationUnit.addFileCommentLine("* "+compilationUnit.getType().getShortName()+".java");
compilationUnit.addFileCommentLine("* Copyright(C) 2017-2020 fendo公司");
compilationUnit.addFileCommentLine("* @date "+sdf.format(new Date())+"");
compilationUnit.addFileCommentLine("*/");
10、添加类注释
在
org.mybatis.generator.internal.DefaultCommentGenerator
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");
innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getRemarks());
innerClass.addJavaDocLine(" * @version 1.0");
innerClass.addJavaDocLine(" * @Author fendo");
innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
innerClass.addJavaDocLine(" */");
然后修改org.mybatis.generator.internal.db.DatabaseIntrospector的calculateIntrospectedTables方法添加以下代码
//设置数据库表的备注信息
Statement stmt = this.databaseMetaData.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(new StringBuilder().append("SHOW TABLE STATUS LIKE '").append(atn.getTableName()).append("'").toString());
while (rs.next())
//将数据库表得备注信息设置到remark字段
table.setRemark(rs.getString("COMMENT"));
closeResultSet(rs);
完整的DefaultCommentGenerator文件如下:
package org.mybatis.generator.internal;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
/**
* MyBatis的默认的注释生成器
*
* @author Jeff Butler
*/
public class DefaultCommentGenerator implements CommentGenerator {
/** 配置文件. */
private Properties properties;
/** 父类的时间. */
private boolean suppressDate;
/** 父类的所有注释. */
private boolean suppressAllComments;
/** 添加表格的注释。如果suppressAllComments为true,则忽略此选项*/
private boolean addRemarkComments;
/** 时间格式 **/
private SimpleDateFormat dateFormat;
/**
* 构造方法
*/
public DefaultCommentGenerator() {
super();
properties = new Properties();
suppressDate = false;
suppressAllComments = false;
addRemarkComments = false;
}
/*
* Java文件加注释
*/
public void addJavaFileComment(CompilationUnit compilationUnit) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
compilationUnit.addFileCommentLine("/*");
compilationUnit.addFileCommentLine("* "+compilationUnit.getType().getShortName()+".java");
compilationUnit.addFileCommentLine("* Copyright(C) 2017-2020 fendo公司");
compilationUnit.addFileCommentLine("* @date "+sdf.format(new Date())+"");
compilationUnit.addFileCommentLine("*/");
}
/**
* Mybatis的Mapper.xml文件里面的注释。
*
* @param xmlElement
* the xml element
*/
public void addComment(XmlElement xmlElement) {
if (suppressAllComments) {
return;
}
}
/*
* 调用此方法为根元素的第一个子节点添加注释。 此方法可用于添加
* 一般文件注释(如版权声明)。 但是,请注意,XML文件合并功能不会处理
* 这个注释。 如果反复运行生成器,则只保留初始运行的注释。
*/
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
}
/*
* 从properties配置文件中添加此实例的属性
*
*/
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
if (StringUtility.stringHasValue(dateFormatString)) {
dateFormat = new SimpleDateFormat(dateFormatString);
}
}
/**
* 此方法用于添加自定义javadoc标签。 如果您不希望包含Javadoc标签,您可能不会执行任何操作 -
* 但是,如果不包含Javadoc标签,那么eclipse插件的Java合并功能就会中断。
*
* @param javaElement
* the java element
* @param markAsDoNotDelete
* the mark as do not delete
*/
protected void addJavadocTag(JavaElement javaElement,
boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" * "); //$NON-NLS-1$
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$
}
String s = getDateString();
if (s != null) {
sb.append(' ');
sb.append(s);
}
javaElement.addJavaDocLine(sb.toString());
}
/**
* 获取时间
* @return a string representing the current timestamp, or null
*/
protected String getDateString() {
if (suppressDate) {
return null;
} else if (dateFormat != null) {
return dateFormat.format(new Date());
} else {
return new Date().toString();
}
}
/*
* 类注释
*/
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");
innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getRemarks());
innerClass.addJavaDocLine(" * @version 1.0");
innerClass.addJavaDocLine(" * @Author fendo");
innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
innerClass.addJavaDocLine(" */");
}
/*
* 实体类注释
*
* 为模型类添加注释。 Java代码合并应该
* 通知不要删除整个class,万一有任何class
* 已经做出了改变。 所以这个方法会永远使用“不要删除”注释。
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments || !addRemarkComments) {
return;
}
}
/*
* 枚举注释
*/
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
innerEnum
.addJavaDocLine(" * This enum was generated by MyBatis Generator."); //$NON-NLS-1$
sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString());
addJavadocTag(innerEnum, false);
innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
}
/*
* 字段注释
*/
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
field.addJavaDocLine("/**");
field.addJavaDocLine(" * @Fields "+field.getName()+" "+introspectedColumn.getRemarks());
field.addJavaDocLine(" */");
}
/*
* 字段注释
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
}
/*
* 普通方法注释,mapper接口中方法
*/
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");
sb.append(" * ");
if (method.isConstructor()) {
sb.append(" 构造查询条件");
}
String method_name = method.getName();
if ("setOrderByClause".equals(method_name)) {
sb.append(" 设置排序字段");
} else if ("setDistinct".equals(method_name)) {
sb.append(" 设置过滤重复数据");
} else if ("getOredCriteria".equals(method_name)) {
sb.append(" 获取当前的查询条件实例");
} else if ("isDistinct".equals(method_name)) {
sb.append(" 是否过滤重复数据");
} else if ("getOrderByClause".equals(method_name)) {
sb.append(" 获取排序字段");
} else if ("createCriteria".equals(method_name)) {
sb.append(" 创建一个查询条件");
} else if ("createCriteriaInternal".equals(method_name)) {
sb.append(" 内部构建查询条件对象");
} else if ("clear".equals(method_name)) {
sb.append(" 清除查询条件");
} else if ("countByExample".equals(method_name)) {
sb.append(" 根据指定的条件获取数据库记录数");
} else if ("deleteByExample".equals(method_name)) {
sb.append(" 根据指定的条件删除数据库符合条件的记录");
} else if ("deleteByPrimaryKey".equals(method_name)) {
sb.append(" 根据主键删除数据库的记录");
} else if ("insert".equals(method_name)) {
sb.append(" 新写入数据库记录");
} else if ("insertSelective".equals(method_name)) {
sb.append(" 动态字段,写入数据库记录");
} else if ("selectByExample".equals(method_name)) {
sb.append(" 根据指定的条件查询符合条件的数据库记录");
} else if ("selectByPrimaryKey".equals(method_name)) {
sb.append(" 根据指定主键获取一条数据库记录");
} else if ("updateByExampleSelective".equals(method_name)) {
sb.append(" 动态根据指定的条件来更新符合条件的数据库记录");
} else if ("updateByExample".equals(method_name)) {
sb.append(" 根据指定的条件来更新符合条件的数据库记录");
} else if ("updateByPrimaryKeySelective".equals(method_name)) {
sb.append(" 动态字段,根据主键来更新符合条件的数据库记录");
} else if ("updateByPrimaryKey".equals(method_name)) {
sb.append(" 根据主键来更新符合条件的数据库记录");
}
sb.append(":");
sb.append(introspectedTable.getFullyQualifiedTable());
method.addJavaDocLine(sb.toString());
final List parameterList = method.getParameters();
if (!parameterList.isEmpty()) {
method.addJavaDocLine(" *");
if ("or".equals(method_name)) {
sb.append(" 增加或者的查询条件,用于构建或者查询");
}
} else {
if ("or".equals(method_name)) {
sb.append(" 创建一个新的或者查询条件");
}
}
String paramterName;
for (Parameter parameter : parameterList) {
sb.setLength(0);
sb.append(" * @param "); //$NON-NLS-1$
paramterName = parameter.getName();
sb.append(paramterName);
if ("orderByClause".equals(paramterName)) {
sb.append(" 排序字段"); //$NON-NLS-1$
} else if ("distinct".equals(paramterName)) {
sb.append(" 是否过滤重复数据");
} else if ("criteria".equals(paramterName)) {
sb.append(" 过滤条件实例");
}
method.addJavaDocLine(sb.toString());
}
method.addJavaDocLine(" */");
}
/*
* Getter方法注释
*/
public void addGetterComment(Method method,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * 获取 "); //$NON-NLS-1$
sb.append(introspectedColumn.getRemarks()).append(" 字段:");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
sb.setLength(0);
sb.append(" * @return "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
sb.append(", ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
/**
* Setter方法注释
*/
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * 设置 "); //$NON-NLS-1$
sb.append(introspectedColumn.getRemarks()).append(" 字段:");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" *"); //$NON-NLS-1$
Parameter parm = method.getParameters().get(0);
sb.setLength(0);
sb.append(" * @param "); //$NON-NLS-1$
sb.append(parm.getName());
sb.append(" the value for "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
sb.append(", ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
/**
* 类注释
*/
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
innerClass.addJavaDocLine("/**");
innerClass.addJavaDocLine(" * @Title "+introspectedTable.getFullyQualifiedTable()+"表的实体类");
innerClass.addJavaDocLine(" * @Description "+introspectedTable.getFullyQualifiedTable().getRemarks());
innerClass.addJavaDocLine(" * @version 1.0");
innerClass.addJavaDocLine(" * @Author fendo");
innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
innerClass.addJavaDocLine(" */");
}
}
新建测试类StartUp
package com.fendo.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
* @Title: StartUp.java
* @Package com.fendo.mybatis_generator_plus
* @Description: TODO
* @author fendo
* @date 2017年10月5日 下午3:53:17
* @version V1.0
*/
public class StartUp {
public static void main(String[] args) throws URISyntaxException {
try {
System.out.println("--------------------start generator-------------------");
List warnings = new ArrayList();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("--------------------end generator-------------------");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
}
}
运行StartUp生成的文件如下:
Test文件
Dao文件
Mapper文件
4)打包
在POM.XML文件中加入以下依赖:
maven-assembly-plugin
2.6
false
org.mybatis.generator.api.ShellRunner
jar-with-dependencies
make-assembly
package
single
mvn clean package
然后复制到一个文件下,测试下
可以自己新建cmd命令,或通过以下命令执行:
java -jar mybatis-generator-plus.jar -configfile generatorConfig.xml
java -jar mybatis-generator-plus.jar -configfile generatorConfig.xml -overwrite
java -cp mybatis-generator-plus.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml
java -cp mybatis-generator-plus.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml -overwrite
完整项目:
http://download.csdn.net/download/u011781521/10009090
有时会报莫名其妙的错误如下:
java.net.MalformedURLException
at java.net.URL.(URL.java:627)
at java.net.URL.(URL.java:490)
at java.net.URL.(URL.java:439)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1304)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1270)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:264)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1161)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1045)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:959)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:140)
at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:113)
at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:105)
at org.mybatis.generator.api.ShellRunner.main(ShellRunner.java:107)
Caused by: java.lang.NullPointerException
at java.net.URL.(URL.java:532)
... 20 more
这是由于在编译的时候,没有把以下两个文件编译进来,所有会提示上面的错误,debug了半天才发现了问题所在:
解决方法就是在POM.XML中加入以下内容:
mybatis-generator-plus
src/main/resources
**/*.properties
**/*.xml
**/*.tld
**/*.dtd
false
src/main/java
**/*.properties
**/*.xml
**/*.tld
**/*.dtd
false
5)注意
1、Mybatis Generator 获取不到字段注释
Oracle 数据库
connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&useInformationSchema=true"
2、mybatis generator 不能生成类注释
在源代码里找到 org.mybatis.generator.internal.DefaultCommentGenerator类,这个类中addClassComment这个方法 是标注中文注释的。为了使addClassComment生效,还得找到org.mybatis.generator.codegen.mybatis3.model.BaseRecordGenerator类,在大约60行的地方,在
commentGenerator.addJavaFileComment(topLevelClass);
后加一句:
commentGenerator.addClassComment(topLevelClass, introspectedTable,false);