mybatis可以通过数据库表直接替我们生成模型类和mappper文件。
这里说一下怎么配置,尤其是如何加上中文注释。当前前提是数据表和字段有中文注释。
比如这样的:
下面配置自动生成模型类和mapper类还有mapper的xml文件。
网上关于如何使用mybatis-generator生成Java对象的文章很多,这里不详细说了。大家可以到百度上so一下,比如http://www.cnblogs.com/smileberry/p/4145872.html,http://www.cnblogs.com/yjmyzz/p/4210554.html
假设我们已经设计好了表结构,表和字段都有自己的注释(其实中英文倒无所谓)。
下面是反向生成的过程:
在类路径下新建generatorConfig.xml,配置如下
<?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> <!--指定特定数据库的jdbc驱动jar包的位置--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="true"/> </javaTypeResolver> <javaModelGenerator targetPackage="comxxx.model" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="constructorBased" value="true"/> <property name="trimStrings" value="true"/> <property name="immutable" value="false"/> </javaModelGenerator> <sqlMapGenerator targetPackage="com.xxx.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator targetPackage="com.xxx.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <table tableName="import_goods_quantity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!--<generatedKey identity="true" sqlStatement="MySql" column="id"/>--> </table> </context> </generatorConfiguration>
generatorConfiguration 有一个子标签
<properties resource="xxx.properties"></properties>
可以引入配置变量。
配置好以后就可以使用插件mybatis-generator-maven-plugin生成了。
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
我们这里使用的是1.3.5版本。
现在用mybatis-generator:generate就可以生成Java类和xml文件到指定目录了。
这样生成的类注释是自动的、毫无意义的时间描述,类似于
@mbg.generated
我们希望数据库里的注释能够拿过来,怎么搞呢?
新建一个maven工程,坐标比如是
<groupId>com.xxx</groupId> <artifactId>comment_generator</artifactId> <version>1.0-SNAPSHOT</version>
引入依赖
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
新建注释配置类
public class MybatisCommentGenerator implements CommentGenerator { private Properties properties; private Properties systemPro; private boolean suppressDate; private boolean suppressAllComments; private String currentDateStr; public MybatisCommentGenerator() { super(); properties = new Properties(); systemPro = System.getProperties(); suppressDate = false; suppressAllComments = false; currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()); } /** * Adds properties for this instance from any properties configured in the * CommentGenerator configuration. * <p> * This method will be called before any of the other methods. * * @param properties All properties from the configuration */ 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)); } /** * * @param field the field * @param introspectedTable the introspected table * @param introspectedColumn */ public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString()); addJavadocTag(field, false); field.addJavaDocLine(" */"); } /** * Adds the field comment. * * @param field the field * @param introspectedTable */ public void addFieldComment(Field field, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedTable.getFullyQualifiedTable()); field.addJavaDocLine(sb.toString()); field.addJavaDocLine(" */"); } public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); topLevelClass.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedTable.getRemarks()); sb.append(" "); sb.append(introspectedTable.getTableType()); sb.append(" "); sb.append(getDateString()); topLevelClass.addJavaDocLine(sb.toString()); topLevelClass.addJavaDocLine(" */"); } /** * Adds the inner class comment. * * @param innerClass the inner class * @param introspectedTable */ public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerClass.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedTable.getFullyQualifiedTable()); sb.append(" "); sb.append(getDateString()); innerClass.addJavaDocLine(sb.toString()); innerClass.addJavaDocLine(" */"); } /** * Adds the inner class comment. * * @param innerClass the inner class * @param introspectedTable the introspected table * @param markAsDoNotDelete */ public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerClass.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedTable.getFullyQualifiedTable()); innerClass.addJavaDocLine(sb.toString()); sb.setLength(0); sb.append(" * @author "); sb.append(systemPro.getProperty("user.name")); sb.append(" "); sb.append(currentDateStr); addJavadocTag(innerClass, markAsDoNotDelete); innerClass.addJavaDocLine(" */"); } /** * Adds the enum comment. * * @param innerEnum the inner enum * @param introspectedTable */ public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerEnum.addJavaDocLine("/**"); addJavadocTag(innerEnum, false); sb.append(" * "); sb.append(introspectedTable.getFullyQualifiedTable()); innerEnum.addJavaDocLine(sb.toString()); innerEnum.addJavaDocLine(" */"); } /** * Adds the getter comment. * * @param method the method * @param introspectedTable the introspected table * @param introspectedColumn */ public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } method.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); sb.setLength(0); sb.append(" * @return "); sb.append(introspectedColumn.getActualColumnName()); sb.append(" "); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); } /** * Adds the setter comment. * * @param method the method * @param introspectedTable the introspected table * @param introspectedColumn */ public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } method.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); sb.append(parm.getName()); sb.append(" "); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); } /** * Adds the general method comment. * * @param method the method * @param introspectedTable */ public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } method.addJavaDocLine("/**"); // addJavadocTag(method, false); StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(MergeConstants.NEW_ELEMENT_TAG); String s = method.getName(); sb.append(' '); sb.append(s); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" */"); } public void addJavaFileComment(CompilationUnit compilationUnit) { } public void addComment(XmlElement xmlElement) { } public void addRootComment(XmlElement rootElement) { } 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.addJavaDocLine(sb.toString()); } protected String getDateString() { String result = null; if (!suppressDate) { result = currentDateStr; } return result; } }
将这个工程install一下。(可以关闭了,除非你要继续修改)
回到前面需要反向生成的工程。
在generatorConfig.xml中,修改
commentGenerator
元素为
<commentGenerator type="com.xxx.mybatis.MybatisCommentGenerator"> <property name="suppressDate" value="false"/> <property name="suppressAllComments" value="false"/> </commentGenerator>
其中type指向刚才工程里的注释配置类。suppressDate依个人喜好可有可无。
修改generator插件,引入刚才新建的工程依赖:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>com.xxx</groupId> <artifactId>comment_generator</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </plugin>
再次运行插件,可以看到模型类上有了我们在数据库里写好的注释。