MybatisGenerator⽣成数据库⾃带的中⽂注释1、相关jar包
<!-- mybatis⽣成 jar包 -->
<dependency>
<groupId&ator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- oracle jar包 -->
<dependency>
<groupId&acle</groupId>
<artifactId>ojdbc6</artifactId>
mysql的jar包下载<version>6.0</version>
</dependency>
<!-- mysql jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
2、⾃定义的CommentGenerator类
ator;
import ator.internal.util.StringUtility.isTrue;
SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
ator.api.CommentGenerator;
ator.api.IntrospectedColumn;
ator.api.IntrospectedTable;
ator.api.dom.java.CompilationUnit;
ator.api.dom.java.Field;
ator.api.dom.java.InnerClass;
ator.api.dom.java.InnerEnum;
ator.api.dom.java.JavaElement;
ator.api.dom.java.Method;
ator.api.dom.java.Parameter;
ator.l.XmlElement;
fig.MergeConstants;
fig.PropertyRegistry;
/**
* 描述:
* @since 1.9.10
* @version 1.9.10
* @作者:niexiaohui
* @创建时间:2016年11⽉22⽇
* @修改记录:
*/
public class MyCommentGenerator implements CommentGenerator{
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String currentDateStr;
public MyCommentGenerator() {
public MyCommentGenerator() {
super();
properties = new Properties();
systemPro = Properties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
public void addJavaFileComment(CompilationUnit compilationUnit) {
// add no file level comments by default
return;
}
/**
* Adds a suitable comment to warn users that the element was generated, and
* when it was generated.
*/
public void addComment(XmlElement xmlElement) {
return;
}
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
return;
}
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = Property(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = Property(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS)); }
/**
* This method adds the custom javadoc tag for. You may do nothing if you do
* not wish to include the Javadoc tag - however, if you do not include the
* Javadoc tag then the Java merge capability of the eclipse plugin will
* break.
*
* @param javaElement
* the java element
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge");
}
String s = getDateString();
if (s != null) {
sb.append(' ');
sb.append(s);
}
javaElement.String());
}
/**
* This method returns a formated date string to include in the Javadoc tag
* and XML comments. You may return null if you do not want the date in
* these documentation elements.
*
* @return a string representing the current timestamp, or null
* @return a string representing the current timestamp, or null
*/
protected String getDateString() {
String result = null;
if (!suppressDate) {
result = currentDateStr;
}
return result;
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.FullyQualifiedTable());
sb.append(" ");
sb.append(getDateString());
innerClass.String().replace("\n", " "));
innerClass.addJavaDocLine(" */");
}
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**");
sb.append(" * ");
sb.FullyQualifiedTable());
innerEnum.String().replace("\n", " "));
innerEnum.addJavaDocLine(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.Remarks());
field.String().replace("\n", " "));
field.addJavaDocLine(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.FullyQualifiedTable());
field.String().replace("\n", " "));
field.addJavaDocLine(" */");
}
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
method.addJavaDocLine("/**");
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.Remarks());
method.String().replace("\n", " "));
sb.setLength(0);
sb.append(" * @return ");
sb.ActualColumnName());
sb.append(" ");
sb.Remarks());
method.String().replace("\n", " "));
method.addJavaDocLine(" */");
}
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.Remarks());
method.String().replace("\n", " "));
Parameter parm = Parameters().get(0);
sb.setLength(0);
sb.append(" * @param ");
sb.Name());
sb.append(" ");
sb.Remarks());
method.String().replace("\n", " "));
method.addJavaDocLine(" */");
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.FullyQualifiedTable());
innerClass.String().replace("\n", " "));
sb.setLength(0);
sb.append(" * @author ");
sb.Property("user.name"));
sb.append(" ");
sb.append(currentDateStr);
innerClass.addJavaDocLine(" */");
}
}
⽣成注释相关类
3、⼊⼝,执⾏类⽂件
ator;
import java.io.IOException;
import java.io.InputStream;
import java.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
ator.api.MyBatisGenerator;
fig.Configuration;
l.ConfigurationParser;
ption.InvalidConfigurationException;
ption.XMLParserException;
ator.internal.DefaultShellCallback;
/**
* 描述:
* @since 1.9.10
* @version 1.9.10
* @作者:niexiaohui
* @创建时间:2016年11⽉22⽇
* @修改记录:
*/
public class StartUp {
public static void main(String[] args) throws URISyntaxException {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = ResourceAsStream("l");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); ate(null);
} 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();
}
}
}
main⽅法,执⾏⼊⼝
4、配置相关的xml⽂件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- ojdbc6.jar已经直接放到项⽬中,⽆需指定了,同时mysql驱动包也⼀样,这⾥同样⽆需指定 --> <!-- <classPathEntry location="ojdbc6.jar" /> -->
<context id="context1" targetRuntime="MyBatis3">
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论