赞
踩
文件结构预览图:
在build.gradle文件中引入如下代码:
- configurations{
- mybatisGenerator
- }
-
- dependencies {
- testCompile group: 'junit', name: 'junit', version: '4.12'
- compile group: 'org.mybatis', name: 'mybatis', version: '3.5.1'
-
- mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.7'
- mybatisGenerator 'tk.mybatis:mapper:4.0.3'
- mybatisGenerator 'mysql:mysql-connector-java:5.1.46'
- }
-
- apply from: "generator.gradle"
将generator.gradle文件apply加入build.gradle中,generator.gradle全代码如下:
- def getDbProperties = {
- def properties = new Properties()
- file("/src/main/resources/mybatisGenerator.properties").withInputStream { inputStream ->
- properties.load(inputStream)
- }
- properties
- }
-
- task mybatisGenerate << {
- def dbProperties = getDbProperties()
- ant.properties['targetProject'] = projectDir.path
- ant.properties['jdbc_driver'] = dbProperties.getProperty("jdbc_driver")
- ant.properties['jdbc_url'] = dbProperties.getProperty("jdbc_url")
- ant.properties['jdbc_user'] = dbProperties.getProperty("jdbc_user")
- ant.properties['jdbc_password'] = dbProperties.getProperty("jdbc_password")
- //ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
- ant.properties['src_main_java'] = dbProperties.getProperty("project")
- //ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
- ant.properties['src_main_resources'] = dbProperties.getProperty("resources")
- ant.properties['modelPackage'] = dbProperties.getProperty("package_model")
- ant.properties['mapperPackage'] = dbProperties.getProperty("package_mapper")
- ant.properties['sqlMapperPackage'] = dbProperties.getProperty("package_xml")
-
- ant.taskdef(
- name: 'mbgenerator',
- classname: 'org.mybatis.generator.ant.GeneratorAntTask',
- classpath: configurations.mybatisGenerator.asPath
- )
-
- ant.mbgenerator(overwrite: true,
- configfile: '..\\generator\\src\\main\\resources\\generatorConfig.xml', verbose: true) {
- propertyset {
- propertyref(name: 'targetProject')
- propertyref(name: 'jdbc_user')
- propertyref(name: 'jdbc_driver')
- propertyref(name: 'jdbc_url')
- propertyref(name: 'jdbc_password')
- propertyref(name: 'src_main_java')
- propertyref(name: 'src_main_resources')
- propertyref(name: 'modelPackage')
- propertyref(name: 'mapperPackage')
- propertyref(name: 'sqlMapperPackage')
- }
- }
- }
generator.gradle代码分析:
1.定义了getDbProperties方法用来加载mybatisGenerator.properties配置文件;
2.定义了task任务,将步骤一加载到的配置文件结合generatorConfig.xml实现代码自动生成;
在gradle-tasks下的other会生成一个mybatisGenerate同名任务,一键任务自动生成。
mybatisGenerator.properties全代码如下,定义了数据库配置和生产文件路径等:
- #Mybatis Generator configuration
- project =src/main/java
- resources=src/main/resources
-
- jdbc_driver=com.mysql.jdbc.Driver
- jdbc_url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
- jdbc_user=root
- jdbc_password=123456
-
- # 生成实体类所在的包
- package_model=com.core
- # 生成 mapper 类所在的包
- package_mapper=com.core
- # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
- package_xml=mappers
generatorConfig.xml文件与mybatis的自动生成代码文件一样,全代码如下:
- <?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:数据库的JDBC驱动,换成自己的驱动位置 可选 -->
- <!--<classPathEntry location="D:\generator_mybatis\mysql-connector-java-5.1.24-bin.jar" /> -->
-
- <!-- 一个数据库一个context -->
- <!--defaultModelType="flat" 大数据字段,不分表 -->
- <context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
- <property name="autoDelimitKeywords" value="true" />
- <property name="beginningDelimiter" value="`" />
- <property name="endingDelimiter" value="`" />
- <property name="javaFileEncoding" value="utf-8" />
- <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
- <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
-
- <!-- 注释 -->
- <commentGenerator >
- <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
- <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
- </commentGenerator>
-
- <!-- jdbc连接 -->
- <jdbcConnection driverClass="${jdbc_driver}"
- connectionURL="${jdbc_url}"
- userId="${jdbc_user}"
- password="${jdbc_password}" />
- <!-- 类型转换 -->
- <javaTypeResolver>
- <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
-
- <!-- 生成实体类地址 -->
- <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}" >
- <property name="enableSubPackages" value="false"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成map.xml文件 -->
- <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}" >
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
- <!-- 生成map.xml对应client,也就是接口dao -->
- <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER" >
- <property name="enableSubPackages" value="false" />
- </javaClientGenerator>
-
- <table tableName="t_user" enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false"
- enableUpdateByExample="false" selectByExampleQueryId="true"/>
- </context>
- </generatorConfiguration>
文件结构预览图:
在build.gradle文件中引入如下代码:
引入MybatisGenerator插件,定义插件的属性,用该插件来自动生成代码。
- plugins {
- id 'java'
- id "com.arenagod.gradle.MybatisGenerator" version "1.4"
-
- }
-
- configurations {
- mybatisGenerator
- }
- mybatisGenerator {
- verbose = true
- configFile = 'src/main/resources/mybatis-generator.xml'
- }
-
- dependencies {
- testCompile group: 'junit', name: 'junit', version: '4.12'
- compile group: 'org.mybatis', name: 'mybatis', version: '3.5.1'
-
- mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.7'
- mybatisGenerator 'tk.mybatis:mapper:4.0.3'
- mybatisGenerator 'mysql:mysql-connector-java:5.1.46'
- }
在插件定义configFile,指向mybatis-generator.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包的全路径 -->
- <!--<classPathEntry location="F:\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>-->
-
- <context id="context" targetRuntime="MyBatis3Simple">
- <!--java代码编码格式-->
- <property name="javaFileEncoding" value="UTF-8"/>
- <!--格式化java代码-->
- <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
- <!--格式化xml代码-->
- <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
-
- <commentGenerator>
- <!-- 是否取消注释 -->
- <property name="suppressAllComments" value="true"/>
- <!-- 是否生成注释代时间戳-->
- <property name="suppressDate" value="true"/>
- </commentGenerator>
-
- <!-- 数据库的相关配置 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true"
- userId="root"
- password="123456"/>
- <javaTypeResolver>
- <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
-
- <!-- 实体类生成的位置 -->
- <javaModelGenerator targetPackage="com.core" targetProject="src/main/java">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false"/>
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
-
- <!-- *Mapper.xml 文件的位置 -->
- <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
-
- <!-- Mapper 接口文件的位置,注意type的值这里是熟悉的xml格式 -->
- <!--
- type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
- 1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
- 2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
- 3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
- 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
- -->
- <javaClientGenerator targetPackage="com.core" targetProject="src/main/java" type="XMLMAPPER">
- <property name="enableSubPackages" value="false"/>
- </javaClientGenerator>
-
- <!-- 相关表的配置 -->
- <table tableName="t_user" enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false"
- enableUpdateByExample="false" selectByExampleQueryId="true"/>
- </context>
- </generatorConfiguration>
在gradle-tasks下的other会生成一个mbGenerate任务,一键任务自动生成。
以上两种方法都实测可用,第二种方法比第一种方法代码量少,配置简单,推荐第二种方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。