当前位置:   article > 正文

gradle 中使用 mybatis-generator

mybatis-generator gradle
  1. 添加依赖
    在build.gradle中添加
  1. configurations {
  2. mybatisGenerator
  3. }

注意把前面的compile group改成mybatisGenerator

  1. mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.6'
  2. mybatisGenerator 'mysql:mysql-connector-java:5.1.45'
  3. mybatisGenerator 'tk.mybatis:mapper:3.5.2'
  1. 设置数据库信息
在 resources 下,新建 mybatis 文件夹,并新建 config.properties 和 generatorConfig.xml,文件结构如下:

  1. # JDBC 驱动类名
  2. jdbc.driverClassName=com.mysql.jdbc.Driver
  3. # JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
  4. jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  5. # JDBC 用户名及密码
  6. jdbc.username=root
  7. jdbc.password=123456
  8. # 生成实体类所在的包
  9. package.model=com.yy.boot.domain
  10. # 生成 mapper 类所在的包
  11. package.mapper=com.yy.boot.dao
  12. # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
  13. package.xml=mybatis/mapper

generator.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--MyBatis3Simple或者MyBatis3,生成的xml文件略有不同-->
  7. <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  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. <jdbcConnection driverClass="${driverClass}"
  14. connectionURL="${connectionURL}"
  15. userId="${userId}"
  16. password="${password}">
  17. </jdbcConnection>
  18. <javaTypeResolver>
  19. <property name="forceBigDecimals" value="false"/>
  20. </javaTypeResolver>
  21. <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
  22. <property name="enableSubPackages" value="true"></property>
  23. <property name="trimStrings" value="true"></property>
  24. </javaModelGenerator>
  25. <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
  26. <property name="enableSubPackages" value="true"></property>
  27. </sqlMapGenerator>
  28. <!--
  29. type=ANNOTATEDMAPPER表示不生成xml文件
  30. 这里我用XMLMAPPER
  31. -->
  32. <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
  33. <property name="enableSubPackages" value="true"/>
  34. </javaClientGenerator>
  35. <!--
  36. sql占位符,表示所有的表
  37. 这里只用city表
  38. -->
  39. <!--<table tableName="%">-->
  40. <!--<generatedKey column="id" sqlStatement="Mysql" identity="true" />-->
  41. <!--</table>-->
  42. <table tableName="city" domainObjectName="City" enableCountByExample="false" enableUpdateByExample="false"
  43. enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  44. </context>
  45. </generatorConfiguration>

在build.gradle中添加

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/307502?site
推荐阅读
相关标签
  

闽ICP备14008679号