maven插件生成

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.mybatis.generator</groupId>
  5. <artifactId>mybatis-generator-maven-plugin</artifactId>
  6. <version>1.3.7</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>8.0.15</version>
  12. </dependency>
  13. </dependencies>
  14. <executions>
  15. <execution>
  16. <id>Generate MyBatis Artifacts</id>
  17. <goals>
  18. <goal>generate</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. <configuration>
  23. <!-- 输出详细信息 -->
  24. <verbose>true</verbose>
  25. <!-- 覆盖生成文件 -->
  26. <overwrite>true</overwrite>
  27. <!-- 定义配置文件 -->
  28. <configurationFile>${basedir}/src/main/resources/generator-configuration.xml</configurationFile>
  29. </configuration>
  30. </plugin>
  31. </plugins>
  32. </build>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  4. <!-- 配置生成器 -->
  5. <generatorConfiguration>
  6. <context id="testTables" targetRuntime="MyBatis3">
  7. <property name="beginningDelimiter" value="`"/>
  8. <property name="endingDelimiter" value="`"/>
  9. <property name="javaFileEncoding" value="UTF-8"/>
  10. <!-- 生成注释配置 -->
  11. <commentGenerator>
  12. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  13. <property name="suppressAllComments" value="true"/>
  14. <property name="javaFileEncoding" value="UTF8"/>
  15. <!--生成的注释包含时间戳(避免重复提交SVN,设为true)-->
  16. <property name="suppressDate" value="true"/>
  17. </commentGenerator>
  18. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  19. <!--
  20. <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
  21. connectionURL="jdbc:oracle:thin:@150.16.17.22:1521/wsbs" userId="hr"
  22. password="hr123">
  23. </jdbcConnection>-->
  24. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
  25. connectionURL="jdbc:mysql://localhost:3306/fzs?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC"
  26. userId="root"
  27. password="123456">
  28. </jdbcConnection>
  29. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  30. NUMERIC 类型解析为java.math.BigDecimal -->
  31. <javaTypeResolver>
  32. <property name="forceBigDecimals" value="false"/>
  33. </javaTypeResolver>
  34. <!-- targetProject:生成PO类的位置 -->
  35. <javaModelGenerator targetPackage="com.hikktn.domain"
  36. targetProject="src\main\java">
  37. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  38. <property name="enableSubPackages" value="false"/>
  39. <!-- 从数据库返回的值被清理前后的空格 -->
  40. <property name="trimStrings" value="true"/>
  41. </javaModelGenerator>
  42. <!-- targetProject:mapper.xml映射文件生成的位置 -->
  43. <sqlMapGenerator targetPackage="mapper"
  44. targetProject="src\main\resources">
  45. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  46. <property name="enableSubPackages" value="false"/>
  47. </sqlMapGenerator>
  48. <!-- targetPackage:mapper.java接口生成的位置 -->
  49. <javaClientGenerator type="XMLMAPPER"
  50. targetPackage="com.hikktn.dao"
  51. targetProject="src\main\java">
  52. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  53. <property name="enableSubPackages" value="false"/>
  54. </javaClientGenerator>
  55. <!-- 指定数据库表 -->
  56. <!-- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
  57. enableSelectByExample="false" selectByExampleQueryId="false"-->
  58. <!--不生成example-->
  59. <!--<table tableName="items"></table> -->
  60. <table schema="distribute" tableName="distribute_lock" domainObjectName="DistributeLock"
  61. enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
  62. enableSelectByExample="false" selectByExampleQueryId="false">
  63. <!--下划线转驼峰命名-->
  64. <property name="useActualColumnNames" value="false"/>
  65. <!--指定自动生成主键的属性-->
  66. <generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
  67. <!--元素从将某些属性默认计算的值更改为指定的值。-->
  68. <!--<columnOverride column="message_content" javaType="List&lt;Teacher&gt;"></columnOverride >-->
  69. <!--忽略字段-->
  70. <!--<ignoreColumn column="file_id"></ignoreColumn>-->
  71. <!--<ignoreColumn column="lyric_id"></ignoreColumn>-->
  72. </table>
  73. </context>
  74. </generatorConfiguration>

测试类生成

  1. <!--自动生成bean-->
  2. <dependency>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-core</artifactId>
  5. <version>1.3.6</version>
  6. </dependency>

生成类

  1. package org.lanqiao.test;
  2. import org.mybatis.generator.api.MyBatisGenerator;
  3. import org.mybatis.generator.config.Configuration;
  4. import org.mybatis.generator.config.xml.ConfigurationParser;
  5. import org.mybatis.generator.internal.DefaultShellCallback;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * @ClassName TestGenerator
  11. * @Description TODO
  12. * @Author lisonglin
  13. * @Date 2018/12/5 17:00
  14. * @Version 1.0
  15. */
  16. public class TestGenerator {
  17. /***
  18. * 功能描述:
  19. * 调用generatorConfig.xml
  20. * @param: []
  21. * @return: void
  22. * @auther: 李松林
  23. * @date: 2018/12/8 19:50
  24. */
  25. public void generator(){
  26. List<String> warnings=new ArrayList<String>();
  27. //更新数据库时,请修改项目的xml配置绝对路径
  28. File file=new File("E:\\MyJAVA\\SpringMakeup\\src\\main\\conf\\generatorConfig.xml");
  29. ConfigurationParser cp = new ConfigurationParser(warnings);
  30. Configuration config = null;
  31. try {
  32. config = cp.parseConfiguration(file);
  33. DefaultShellCallback shellCallback = new DefaultShellCallback(true);
  34. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
  35. myBatisGenerator.generate(null);
  36. System.out.println(warnings);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. /**
  42. *
  43. * @param args
  44. * @throws Exception
  45. */
  46. public static void main(String[] args) throws Exception {
  47. try {
  48. TestGenerator generatorSqlmap = new TestGenerator();
  49. generatorSqlmap.generator();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }

生成配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  4. <!-- 配置生成器 -->
  5. <generatorConfiguration>
  6. <context id="testTables" targetRuntime="MyBatis3" defaultModelType="flat">
  7. <property name="beginningDelimiter" value="`"/>
  8. <property name="endingDelimiter" value="`"/>
  9. <property name="javaFileEncoding" value="UTF-8"/>
  10. <!-- 生成注释配置 -->
  11. <commentGenerator>
  12. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  13. <property name="suppressAllComments" value="true"/>
  14. <property name="javaFileEncoding" value="UTF8"/>
  15. <!--生成的注释包含时间戳(避免重复提交SVN,设为true)-->
  16. <property name="suppressDate" value="true"/>
  17. </commentGenerator>
  18. <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
  19. <!--
  20. <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
  21. connectionURL="jdbc:oracle:thin:@150.16.17.22:1521/wsbs" userId="hr"
  22. password="hr123">
  23. </jdbcConnection>-->
  24. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  25. connectionURL="jdbc:mysql://localhost:3306/sys?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"
  26. userId="root"
  27. password="123">
  28. </jdbcConnection>
  29. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  30. NUMERIC 类型解析为java.math.BigDecimal -->
  31. <javaTypeResolver>
  32. <property name="forceBigDecimals" value="false"/>
  33. </javaTypeResolver>
  34. <!-- targetProject:生成PO类的位置 -->
  35. <javaModelGenerator targetPackage="org.lanqiao.model"
  36. targetProject="E:/MyJAVA/SpringMakeup/src/main/java">
  37. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  38. <property name="enableSubPackages" value="false"/>
  39. <!-- 从数据库返回的值被清理前后的空格 -->
  40. <property name="trimStrings" value="true"/>
  41. </javaModelGenerator>
  42. <!-- targetProject:mapper.xml映射文件生成的位置 -->
  43. <sqlMapGenerator targetPackage="org.lanqiao.mapper"
  44. targetProject="E:/MyJAVA/SpringMakeup/src/main/conf">
  45. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  46. <property name="enableSubPackages" value="false"/>
  47. </sqlMapGenerator>
  48. <!-- targetPackage:mapper.java接口生成的位置 -->
  49. <javaClientGenerator type="XMLMAPPER"
  50. targetPackage="org.lanqiao.mapper"
  51. targetProject="E:/MyJAVA/SpringMakeup/src/main/java">
  52. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  53. <property name="enableSubPackages" value="false"/>
  54. </javaClientGenerator>
  55. <!-- 指定数据库表 -->
  56. <!-- enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
  57. enableSelectByExample="false" selectByExampleQueryId="false"-->
  58. <!--不生成example-->
  59. <!--<table tableName="items"></table> -->
  60. <table tableName="hobbys" mapperName="HobbysMapper" domainObjectName="Hobbys"
  61. enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
  62. enableSelectByExample="false" selectByExampleQueryId="false">
  63. <!--下划线转驼峰命名-->
  64. <property name="useActualColumnNames" value="false"/>
  65. <!--指定自动生成主键的属性-->
  66. <generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
  67. <!--元素从将某些属性默认计算的值更改为指定的值。-->
  68. <!--<columnOverride column="message_content" javaType="List&lt;Teacher&gt;"></columnOverride >-->
  69. <!--忽略字段-->
  70. <!--<ignoreColumn column="file_id"></ignoreColumn>-->
  71. <!--<ignoreColumn column="lyric_id"></ignoreColumn>-->
  72. </table>
  73. </context>
  74. </generatorConfiguration>