当前位置:   article > 正文

Gradle项目使用MyBatis-generator自动生成MyBatis映射代码_gradle mybatis generator

gradle mybatis generator

Gradle项目使用MyBatis-generator自动生成MyBatis映射代码

目录

引用:

1,方式一:使用类 MyBatisGenerator

准备的xml: generatorConfig.xml

类:MybatisGeneratorUtil

2,使用build任务生成:

Xml:generatorConfigMb.xml

Build.gradle:

生成结果:

3.使用ant的方式:

1,GeneratorAntTask

类:MybatisAntGeneratorUtil

2,使用build任务ant方式(失败)

Builde.gradle:

数据库文件:jdbc.properties

xml 文件:generator-config.xml

刷新项目并执行:

报错:

总结:


引用:

   Maven库  (PS: 找jar包,用它基本就够了,当然前提是知道哪个jar包,比如ant包)

dependency.gradle:

  1. //统一依赖定义
  2. ext.libraries = [
  3. mybatisGenerators: [
  4.         'org.mybatis.generator:mybatis-generator-core:1.4.0',
  5.         'mysql:mysql-connector-java:5.1.48',
  6.         'org.mybatis:mybatis:3.5.1',
  7.         'org.apache.ant:ant:1.10.5'
  8. ]
  9. ]
 

buidle.gradle 添加dependecy.gradle的引用

apply from: 'dependency.gradle'

1,方式一:使用类 MyBatisGenerator

准备的xml: generatorConfig.xml
 

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE generatorConfiguration PUBLIC
  3.         "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  5. <generatorConfiguration>
  6.     <context id="context" targetRuntime="MyBatis3">
  7.         <!--覆盖生成XML文件 -->
  8.         <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
  9.         <commentGenerator>
  10.             <property name="suppressAllComments" value="true"/>
  11.             <property name="suppressDate" value="true"/>
  12.         </commentGenerator>
  13.         <!-- 数据库的相关配置 -->
  14.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  15.                         connectionURL="jdbc:mysql://localhost:3306/yan?useSSL=false" userId="yan" password="yan.123"/>
  16.         <javaTypeResolver>
  17.             <property name="forceBigDecimals" value="false"/>
  18.         </javaTypeResolver>
  19.         <!-- 实体类生成的位置 -->
  20.         <javaModelGenerator targetPackage="com.report.entity.po" targetProject="src/main/java">
  21.             <property name="enableSubPackages" value="false"/>
  22.             <property name="trimStrings" value="true"/>
  23.         </javaModelGenerator>
  24.         <!-- *Mapper.xml 文件的位置 -->
  25.         <sqlMapGenerator targetPackage="report" targetProject=".\src\main\resources\mybatis">
  26.             <property name="enableSubPackages" value="false"/>
  27.         </sqlMapGenerator>
  28.         <!-- Mapper 接口文件的位置 -->
  29.         <javaClientGenerator targetPackage="com.report.repository" targetProject="src/main/java" type="XMLMAPPER">
  30.             <property name="enableSubPackages" value="false"/>
  31.         </javaClientGenerator>
  32.         <!-- 相关表的配置 -->
  33.         <table tableName="sys_report_class" domainObjectName="SysReportClass" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  34.         <table tableName="sys_report_param" domainObjectName="SysReportParam" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  35.             </context>
  36. </generatorConfiguration>

  <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
注意: 使用类生成的时候,要添加这个,不然会追加生成

类:MybatisGeneratorUtil

  1. public class MybatisGeneratorUtil {
  2.     public static void main(String[] args) {
  3.          generateMybatisInfo();
  4.     }
  5.     private static void generateMybatisInfo() {
  6.         try {
  7.             System.out.println("start generator ...");
  8.             List<String> warnings = new ArrayList<>();
  9.             File configFile = new File("src/main/resources/mybatis/config/generatorConfig.xml");
  10.             ConfigurationParser cp = new ConfigurationParser(warnings);
  11.             Configuration config = cp.parseConfiguration(configFile);
  12.             DefaultShellCallback callback = new DefaultShellCallback(true);
  13.             MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  14.             myBatisGenerator.generate(null);
  15.             System.out.println("end generator!");
  16.         } catch (IOException | XMLParserException | InterruptedException | SQLException | InvalidConfigurationException e) {
  17.             e.printStackTrace();
  18.         }
  19.     }
  20. }

 如果上面lib引用没效果,build.gradle 里面加:

  1. implementation 'org.mybatis.generator:mybatis-generator:1.4.0'
  2. implementation 'org.mybatis.generator:mybatis-generator-core:1.4.0'

2,使用build任务生成:

Xml:generatorConfigMb.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE generatorConfiguration PUBLIC
  3.         "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  5. <generatorConfiguration>
  6.     <context id="context" targetRuntime="MyBatis3">
  7.         <!--覆盖生成XML文件  使用gradle任务的方式会读取不到-->
  8. <!--<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />-->
  9.         <commentGenerator>
  10.             <property name="suppressAllComments" value="true"/>
  11.             <property name="suppressDate" value="true"/>
  12.         </commentGenerator>
  13.         <!-- 数据库的相关配置 -->
  14.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  15.                         connectionURL="jdbc:mysql://localhost:3306/yan?useSSL=false" userId="yan" password="yan.123"/>
  16.         <javaTypeResolver>
  17.             <property name="forceBigDecimals" value="false"/>
  18.         </javaTypeResolver>
  19.         <!-- 实体类生成的位置 -->
  20.         <javaModelGenerator targetPackage="com.report.entity.po" targetProject="src/main/java">
  21.             <property name="enableSubPackages" value="false"/>
  22.             <property name="trimStrings" value="true"/>
  23.         </javaModelGenerator>
  24.         <!-- *Mapper.xml 文件的位置 -->
  25.         <sqlMapGenerator targetPackage="report" targetProject=".\src\main\resources\mybatis">
  26.             <property name="enableSubPackages" value="false"/>
  27.         </sqlMapGenerator>
  28.         <!-- Mapper 接口文件的位置 -->
  29.         <javaClientGenerator targetPackage="com.report.repository" targetProject="src/main/java" type="XMLMAPPER">
  30.             <property name="enableSubPackages" value="false"/>
  31.         </javaClientGenerator>
  32.         <!-- 相关表的配置 -->
  33.         <table tableName="sys_report_class" domainObjectName="SysReportClass" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  34.         <table tableName="sys_report_param" domainObjectName="SysReportParam" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  35.             </context>
  36. </generatorConfiguration>

注意: 使用任务的生成的方式,不会覆盖。

<!--<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />-->
这边就注释了。不然会报找不到的错误

Build.gradle:

  1. plugins {
  2. id "com.arenagod.gradle.MybatisGenerator" version "1.4" //gradle提供的mybatis generator插件
  3. }
  4. mybatisGenerator {
  5.     verbose = true
  6.     configFile = 'src/main/resources/mybatis/config/generatorConfigMb.xml'
  7. }
 

点击更新,然后点击:mbGenerator

生成结果:

3.使用ant的方式:

1,GeneratorAntTask

Xml: generatorConfig.xml 同方法一的xml

类:MybatisAntGeneratorUtil

  1. public class MybatisAntGeneratorUtil {
  2.     public static void main(String[] args) {
  3.         generateMybatisXml();
  4.     }
  5.     public static void generateMybatisXml() {
  6.         try {
  7.             System.out.println("start mybatis generator ...");
  8.             GeneratorAntTask task = new GeneratorAntTask();
  9.             String xmlPath = "D:/project/report/src/main/resources/mybatis/config/generatorConfig.xml";
  10.             task.setConfigfile(xmlPath);    //(配置文件具体path)
  11.             task.execute();
  12.             System.out.println("end mybatis generator!");
  13.         } catch (Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.     }
  17. }

使用类GeneratorAntTask必须要引用 ant的包: 'org.apache.ant:ant:1.10.5'

2,使用build任务ant方式(失败)

Builde.gradle:

  1. plugins {
  2.         id "com.arenagod.gradle.MybatisGenerator" version "1.4" //gradle提供的mybatis generator插件
  3. }
  4. def getDbProperties = {
  5.     def properties = new Properties()
  6.     file("src/main/resources/mybatis/config/jdbc.properties").withInputStream { inputStream ->
  7.         properties.load(inputStream)
  8.     }
  9.     properties
  10. }
  11. task mybatisGenerate << {
  12.     def properties = getDbProperties()
  13.     ant.properties['targetProject'] = projectDir.path
  14.     ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
  15.     ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
  16.     ant.properties['userId'] = properties.getProperty("jdbc.username")
  17.     ant.properties['password'] = properties.getProperty("jdbc.password")
  18.     ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
  19.     ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
  20.     ant.properties['modelPackage'] = properties.getProperty("modelPackage")
  21.     ant.properties['mapperPackage'] = properties.getProperty("mapperPackage")
  22.     ant.properties['sqlMapperPackage'] = properties.getProperty("sqlMapperPackage")
  23.     ant.taskdef(
  24.             name: 'mybatisGenerator',
  25.             classname: 'org.mybatis.generator.ant.GeneratorAntTask',
  26.             classpath: configurations.mybatisGenerator.asPath
  27.     )
  28.     ant.mybatisGenerator(overwrite: true,
  29.             configfile: 'src/main/resources/mybatis/config/generator-config.xml', verbose: true) {
  30.         propertyset {
  31.             propertyref(name: 'targetProject')
  32.             propertyref(name: 'userId')
  33.             propertyref(name: 'driverClass')
  34.             propertyref(name: 'connectionURL')
  35.             propertyref(name: 'password')
  36.             propertyref(name: 'src_main_java')
  37.             propertyref(name: 'src_main_resources')
  38.             propertyref(name: 'modelPackage')
  39.             propertyref(name: 'mapperPackage')
  40.             propertyref(name: 'sqlMapperPackage')
  41.         }
  42.     }
  43. }

数据库文件:jdbc.properties

  1. jdbc.url=jdbc:mysql://localhost:3306/yan?useSSL=false
  2. jdbc.username=yan
  3. jdbc.password=yan.123
  4. jdbc.driverClassName=com.mysql.jdbc.Driver
  5. #生成实体类所在的包
  6. modelPackage=com.ffcs.iod.ms.workflow.report.entity
  7. #生成的mapper接口类所在包
  8. mapperPackage=com.ffcs.iod.ms.workflow.report.repository
  9. #生成的mapper xml文件所在包,默认存储在resources目录下
  10. sqlMapperPackage=src/main/resources/mybatis/report
 

xml 文件:generator-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE generatorConfiguration PUBLIC
  3.         "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
  5. <generatorConfiguration>
  6.     <context id="context" targetRuntime="MyBatis3">
  7.         <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
  8.         <commentGenerator>
  9.             <property name="suppressAllComments" value="true"></property>
  10.             <property name="suppressDate" value="true"></property>
  11.             <property name="javaFileEncoding" value="utf-8"/>
  12.         </commentGenerator>
  13.         <!-- 数据库的相关配置 -->
  14.         <jdbcConnection driverClass="${driverClass}"
  15.                         connectionURL="${connectionURL}"
  16.                         userId="${userId}"
  17.                         password="${password}">
  18.         </jdbcConnection>
  19.         <javaTypeResolver>
  20.             <property name="forceBigDecimals" value="false"/>
  21.         </javaTypeResolver>
  22.         <!-- 实体类生成的位置 -->
  23.         <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
  24.             <property name="enableSubPackages" value="true"></property>
  25.             <property name="trimStrings" value="true"></property>
  26.         </javaModelGenerator>
  27.         <!-- *Mapper.xml 文件的位置 -->
  28.         <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
  29.             <property name="enableSubPackages" value="true"></property>
  30.         </sqlMapGenerator>
  31.         <!-- Mapper 接口文件的位置 -->
  32.         <!--
  33.         type=ANNOTATEDMAPPER表示不生成xml文件
  34.         这里我用XMLMAPPER
  35.         -->
  36.         <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
  37.             <property name="enableSubPackages" value="true"/>
  38.         </javaClientGenerator>
  39.         <!-- 相关表的配置 -->
  40.         <table tableName="sys_report_class" domainObjectName="SysReportClass" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  41.         <table tableName="sys_report_param" domainObjectName="SysReportParam" enableCountByExample="false"  enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
  42.     </context>
  43. </generatorConfiguration>
 

刷新项目并执行:

报错:

* What went wrong:

Execution failed for task ':mybatisGenerate'.

> taskdef class org.mybatis.generator.ant.GeneratorAntTask cannot be found

   using the classloader AntClassLoader[]

网上查了下,没找到解决方式就放弃了。这种方式还麻烦。

总结:

 使用方法2最简单,方法1会简单些,方法3还有引用ant的jar包。 方式4网上很多案例都是那样,但是报错了,后面有机会碰到再更新。

  使用mybatis-generator生成,减少一些工作。

生成实体带注释:《生成的实体带注释​​​​​​​》

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号