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> <classPathEntry location="/Users/kanpiaoxue/develop_tools/maven/maven_repository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar" /> <context id="context1"> <property name="javaFileEncoding" value="UTF-8" /> <!-- 将下面的 com.example.work.mybatisgenerator.MyCommentGenerator 放在 classpath即可使用 --> <commentGenerator type="com.example.work.mybatisgenerator.MyCommentGenerator"> <property name="suppressAllComments" value="false" /> </commentGenerator> <jdbcConnection connectionURL="jdbc:mysql://127.0.0.1:3306/beidou" driverClass="com.mysql.jdbc.Driver" password="root" userId="root" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- model package and location --> <javaModelGenerator targetPackage="org.kanpiaoxuie.beidou.commons.bean.entity" targetProject="work/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapping package and location --> <sqlMapGenerator targetPackage="org.kanpiaoxuie.beidou.dao.mapper" targetProject="work/src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- dao package and location --> <javaClientGenerator type="XMLMAPPER" targetPackage="org.kanpiaoxuie.beidou.dao" targetProject="work/src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="tb_task" domainObjectName="Task" mapperName="TaskDAO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
需要在pom.xml 中添加如下依赖进行扩展:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>
/** * Copyright kanpiaoxue.org [2017-2018] */ package com.example.work.mybatisgenerator; import static org.mybatis.generator.internal.util.StringUtility.isTrue; 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.FullyQualifiedJavaType; 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.XmlElement; import org.mybatis.generator.config.PropertyRegistry; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import java.util.Set; /** * MyCommentGenerator.java * * @author kanpiaoxue * @version 1.0 * Create Time 2018年10月27日 下午8:37:40 * Description : */ public class MyCommentGenerator implements CommentGenerator { private Properties properties; private Properties systemPro; private boolean suppressDate; private boolean suppressAllComments; private String currentDateStr; public MyCommentGenerator() { super(); properties = new Properties(); systemPro = System.getProperties(); 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 = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE)); suppressAllComments = isTrue(properties.getProperty(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) { return; } /** * 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 */ 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.append(introspectedTable.getFullyQualifiedTable()); sb.append(" "); sb.append(getDateString()); innerClass.addJavaDocLine(sb.toString().replace("\n", " ")); innerClass.addJavaDocLine(" */"); } public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerEnum.addJavaDocLine("/**"); sb.append(" * "); sb.append(introspectedTable.getFullyQualifiedTable()); innerEnum.addJavaDocLine(sb.toString().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.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString().replace("\n", " ")); field.addJavaDocLine(" */"); } 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().replace("\n", " ")); field.addJavaDocLine(" */"); } public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } String methodName = method.getName(); String nameDoc = ""; if ("deleteByPrimaryKey".equals(methodName)) { nameDoc = "根据主键删除记录"; } else if ("insert".equals(methodName)) { nameDoc = "新增记录"; } else if ("insertSelective".equals(methodName)) { nameDoc = "根据字段新增记录"; } else if ("selectByPrimaryKey".equals(methodName)) { nameDoc = "根据主键得到记录"; } else if ("updateByPrimaryKeySelective".equals(methodName)) { nameDoc = "根据字段使用主键更新记录"; } else if ("updateByPrimaryKey".equals(methodName)) { nameDoc = "使用主键更新记录"; } else { nameDoc = ""; } method.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" *\n"); sb.append(" * "); sb.append(nameDoc).append("\n"); method.addJavaDocLine(sb.toString()); StringBuilder sb1 = new StringBuilder(); for (Parameter parm : method.getParameters()) { sb1.append(" * @param "); sb1.append(parm.getName()); sb1.append(" "); method.addJavaDocLine(sb1.toString().replace("\n", " ")); } StringBuilder sb2 = new StringBuilder(); sb2.append(" * @return 影响记录数量"); method.addJavaDocLine(sb2.toString()); method.addJavaDocLine(" */"); } 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().replace("\n", " ")); sb.setLength(0); sb.append(" * @return "); sb.append(introspectedColumn.getActualColumnName()); sb.append(" "); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString().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.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString().replace("\n", " ")); 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().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.append(introspectedTable.getFullyQualifiedTable()); innerClass.addJavaDocLine(sb.toString().replace("\n", " ")); sb.setLength(0); sb.append(" * @author "); sb.append(systemPro.getProperty("user.name")); sb.append(" "); sb.append(currentDateStr); innerClass.addJavaDocLine(" */"); } /* * (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addModelClassComment(org. * mybatis.generator.api.dom.java.TopLevelClass, * org.mybatis.generator.api.IntrospectedTable) */ @Override public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } topLevelClass.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" * 数据表: "); sb.append(introspectedTable.getFullyQualifiedTable()); topLevelClass.addJavaDocLine(sb.toString().replace("\n", " ")); topLevelClass.addJavaDocLine(" */"); } /* * (non-Javadoc) * @see * org.mybatis.generator.api.CommentGenerator#addGeneralMethodAnnotation(org * .mybatis.generator.api.dom.java.Method, * org.mybatis.generator.api.IntrospectedTable, java.util.Set) */ @Override public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { } /* * (non-Javadoc) * @see * org.mybatis.generator.api.CommentGenerator#addGeneralMethodAnnotation(org * .mybatis.generator.api.dom.java.Method, * org.mybatis.generator.api.IntrospectedTable, * org.mybatis.generator.api.IntrospectedColumn, java.util.Set) */ @Override public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { } /* * (non-Javadoc) * @see * org.mybatis.generator.api.CommentGenerator#addFieldAnnotation(org.mybatis * .generator.api.dom.java.Field, * org.mybatis.generator.api.IntrospectedTable, java.util.Set) */ @Override public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { } /* * (non-Javadoc) * @see * org.mybatis.generator.api.CommentGenerator#addFieldAnnotation(org.mybatis * .generator.api.dom.java.Field, * org.mybatis.generator.api.IntrospectedTable, * org.mybatis.generator.api.IntrospectedColumn, java.util.Set) */ @Override public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { } /* * (non-Javadoc) * @see * org.mybatis.generator.api.CommentGenerator#addClassAnnotation(org.mybatis * .generator.api.dom.java.InnerClass, * org.mybatis.generator.api.IntrospectedTable, java.util.Set) */ @Override public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { } }
相关推荐
这是 IntelliJ IDEA Maven Mybatis generator 自动生成代码 的完整源码 教程地址:http://blog.csdn.net/fenglailea/article/details/78291522
MyBatis Generator(MBG)是一款强大的工具,用于自动生成MyBatis框架的SQL映射文件、Java模型类以及DAO接口。它极大地提高了开发效率,减少了手动编写这些基础代码的工作量,使开发者能够更专注于业务逻辑的实现。...
Mybatis Generator是一款强大的工具,它能够自动生成Mybatis的映射文件、Model类以及Mapper接口,极大地提高了开发效率。在本示例中,我们将深入探讨如何利用Mybatis Generator与MySQL数据库配合,实现代码的自动化...
总结来说,MyBatis Generator是MyBatis框架的重要辅助工具,它能够根据数据库表结构自动生成Java实体类、Mapper接口和XML配置文件,极大地提高了开发效率,降低了手动编写代码的错误率。使用MBG时,需要正确配置...
Mybatis Generator是一款强大的工具,它能够自动生成Mybatis框架所需的SQL映射文件、Mapper接口以及实体类,大大简化了开发工作,提高了效率。通过配置XML文件,我们可以指定数据库连接信息、表名、生成的文件类型等...
标题 "mybatis-generator自动生成代码[增加Service的生成]" 指的是MyBatis Generator(MBG)这一工具,它能够根据数据库表结构自动产生Java持久层代码,大大减轻了开发人员的手动编码工作。在描述中提到"增加Service...
mybatis-generator 自动生成mybatis dao model层代码
现在,当我们在命令行运行`mvn mybatis-generator:generate`时,Mybatis Generator将根据`generatorConfig.xml`中的配置自动生成相应的代码。这包括Mapper接口、XML映射文件以及实体类。 在生成的代码中,Mapper...
首先,MyBatis的代码生成器(MyBatis Generator,简称MBG)能够自动生成Mapper接口、Mapper XML文件、实体类以及对应的DAO层代码,大大减少了手动编写这些基础代码的时间。通过自定义MBG的配置文件,我们可以定制...
MyBatis Generator(MBG)是一款强大的代码生成工具,它能够根据数据库表结构自动生成Java实体类、Mapper接口与XML配置文件,以及Service和Controller层的代码,极大地提高了开发效率。在使用MBG时,首先需要确保你...
MyBatis Generator(MBG)是一款强大的工具,用于在Java环境下自动生成MyBatis的映射文件、DAO接口以及实体类。它极大地提高了开发效率,减少了手动编写这些基础代码的工作量,使开发者能够更专注于业务逻辑的实现。...
MyBatis Generator(MBG)是一款强大的自动化代码生成工具,专为MyBatis框架设计,能够自动生成DAO层、实体Bean以及Mapper XML文件,极大地提高了开发效率。在使用MBG时,用户只需要进行简单的配置,就可以自动生成...
使用 MyBatis Generator 自动生成 MyBatis 代码 MyBatis Generator 是一个流行的开源工具,用于根据数据库表结构自动生成 MyBatis 的映射文件、实体类、DAO 层和 Service 层等代码。下面是使用 MyBatis Generator ...
不过,需要注意的是,由于MBG是根据数据库表结构自动生成代码,所以当数据库表结构发生变化时,需要重新运行MBG来更新代码。 总的来说,MyBatis Generator是Java开发中一个实用的自动化工具,尤其对于使用MyBatis...
Mybatis Generator是一款强大的工具,它能够自动生成Java实体类(Entity)、Mapper接口和XML配置文件,极大地提高了开发效率。在"Maven"环境下,我们可以轻松地集成这个插件,实现自动化代码生成,尤其对于处理带有...
MyBatis Generator是一款强大的自动化工具,它能够帮助开发者自动生成Java实体类、DAO接口以及Mapping映射文件,极大地提高了开发效率。在SSM(Spring、SpringMVC、MyBatis)框架中,MyBatis Generator作为一款必备...
IDEA Maven Mybatis generator 自动生成代码实例讲解 IDEA Maven Mybatis generator 是一种基于 IDEA 的 Maven 项目插件,使用 Mybatis generator 自动生成代码。下面是 IDEA Maven Mybatis generator 自动生成代码...
Mybatis Generator 是...总结来说,Mybatis Generator 是一个高效实用的工具,通过简单的配置就能自动生成与数据库交互的代码,适用于使用 MySQL 的项目。使用它可以极大地提高开发效率,降低出错概率,并且易于维护。
通过mybatis-generator-...连接数据库,通过配置文件自动生成对应表的mybatis的配置文件,a.bat是通过cmd命令运行可执行jar mybatis-generator-core-1.3.1.jar ,通过 generatorConfig.xml的配置生成代码到相应位置.
MyBatis Generator是一款强大的工具,它能够自动生成MyBatis框架所需的SQL映射文件、实体类和DAO接口,极大地提高了开发效率。通过重写MyBatis Generator的源码,我们可以进一步定制化生成的代码,使其更符合项目...