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> <!-- 指定数据连接驱动jar地址,需要5.x的版本,8.x的只会生成select的方法 --> <classPathEntry location="D:/repository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar" /> <!-- 一个数据库一个context --> <context id="context" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8"/> <property name="fileEncoding" value="UTF-8"/> <!-- 注释 --> <!-- <commentGenerator> <property name="javaFileEncoding" value="UTF-8"/> <property name="suppressAllComments" value="true" /> </commentGenerator> --> <!-- 使用自定义的插件 --> <commentGenerator type="com.ziyun.user.common.MybatisCommentGenerator"> <property name="javaFileEncoding" value="UTF-8"/> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test" userId="admin" password="123456"> <!-- 多个库都有相同表时,true时不重复生成代码 --> <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- model package and location --> <!-- 生成实体类的包名和位置 注意targetProject的值为实体类放在工程中具体位置的相对路径 要改--> <javaModelGenerator targetPackage="com.test.user.domain" targetProject="spring-cloud-usersrcmainjava"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapping .xml package and location --> <!-- 生成sql映射文件的包名和位置 要改--> <sqlMapGenerator targetPackage="mappers" targetProject="spring-cloud-usersrcmain esources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- dao .java package and location --> <!-- 生成mapper接口的包名和位置 要改--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.ziyun.user.dao" targetProject="spring-cloud-usersrcmainjava"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <table schema="test" tableName="voice_callback_log" domainObjectName="Voice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" selectByExampleQueryId="true" /> </context> </generatorConfiguration>
eclipse可以安装mybatis generator插件,安装好后,可以直接右键运行generatorConfig.xml
如果需要生成的实体类有注释,则要写一个根据表字段注释生成实体类字段注释的插件,需要引入mybatis-generator-core.jar
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.InnerClass; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.internal.DefaultCommentGenerator; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; /** * mybatis generator生成注释插件 * <p> */ public class MybatisCommentGenerator extends DefaultCommentGenerator { 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()); } 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(" ", " ")); field.addJavaDocLine(" */"); } public void addFieldComment(Field field, IntrospectedTable introspectedTable) { } public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { } public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { } public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { } }
完!