Because the Gradle project in IntelliJ idea cannot directly use the famous Mybatisgenerator plug-in, it is possible to indirectly generate an environment from an Ant Task
JDK 1.8 IntelliJ Idea 2017.1.4 gradle 3.5.0 Project Structure The project is structured as follows, which is a standard Gradle project
Add Dependencies This is mainly used for MyBatis Generator,mybatis Mapper and MySQL Java Connector dependencies, which need to be added to the build.gradle below
configurations {
Mybatisgenerator
}
Add MyBatis generator,mybatis Mapper and MySQL Java Connector dependencies
dependencies {
testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '
mybatisgenerator ' org.mybatis.generator:mybatis-generator-core:1.3.5 '
mybatisgenerator ' mysql:mysql-connector-java:5.1.40 '
mybatisgenerator ' tk.mybatis:mapper:3.3.9 '
}
Configuring database ConnectionsCreate a new Mybatisgenerator directory in the Resource directory, where new two files config.properties, Generatorconfig.xml, name can be different, but be aware of the same as the directory in the following configuration
Configure information about the database in Config.properties and the location of the package where the code is generated
# MySQL driver
jdbc.driverclassname = com.mysql.jdbc.Driver
# jdbc URL
jdbc.url =j dbc:mysql://127.0.0.1:3306/ Fullstack?useunicode:true&characterencoding:utf-8
# Database username and password
jdbc.username = root
Jdbc.password = Root
# entity class where package
Package.model = me.zbl.fullstack.entity
# Mapper Package
Package.mapper = Me.zbl.fullstack.mapper
# Mapper XML file contains the package
package.xml = mapping
Configure 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> <context id=" Mysql " Targetruntime= "Mybatis3simple" defaultmodeltype= "flat" > <commentGenerator> <property name= "Suppressallcomments" value= "true" ></property> <property name= "Suppressdate" value= "true" ></
property> <!--java file encoding format--> <property name= "javafileencoding" value= "Utf-8"/> </commentGenerator> <!--JDBC Connection information--> <jdbcconnection driverclass= "${driverclass}
"Connectionurl=" ${connectionurl} "userid=" ${userid} "
Password= "${password}" > </jdbcConnection> <javaTypeResolver> <property name= "Forcebigdecimals" value= "false"/> </javaTypeResolver> <!--entity class containing the package name-- > <javamodelgenerator targetpackage= "${modelpackage}" targetproject= "${src_main_java}" > <p Roperty name= "Enablesubpackages" value= "true" ></property> <property name= "Trimstrings" true "></property> </javaModelGenerator> <!--XML package name--> <sqlmapgene Rator targetpackage= "${sqlmapperpackage}" targetproject= "${src_main_resources}" > <property name= "EnableS"
Ubpackages "value=" true "></property> </sqlMapGenerator> <!--mapper Package name--> <javaclientgenerator targetpackage= "${mapperpackage}" targetproject= "${src_main_java}" type= "ANNOTATEDMAPPER" > <property name= "enablesubpackages" value= "true"/> </javaClientGenerator> ;! --the table to be generated--> <!-- The percent semicolon here represents the generation of all tables in the database--> <table tablename= "%" > <generatedkey column= "id" sqlstatement= " Mysql "identity=" true "/> </table> </context> </generatorConfiguration>
Add a taskAdd the following content to the Build.gradle
def getdbproperties = {def properties = new properties () file ("Src/main/resources/mybatis/jdbc.properties"). Withi Nputstream {inputstream-> properties.load (InputStream)} Properties} task Mybatisgenerate << {Def properties = getdbproperties () ant.properties[' targetproject '] = Projectdir.path ant.properties[' Driverc Lass '] = Properties.getproperty ("jdbc.driverclassname") ant.properties[' connectionurl '] = Properties.getproperty (" Jdbc.url ") ant.properties[' userId '] = Properties.getproperty (" jdbc.username ") ant.properties[' password '] = Propert Ies.getproperty ("Jdbc.password") ant.properties[' src_main_java '] = Sourcesets.main.java.srcdirs[0].path ant.proper ties[' src_main_resources '] = Sourcesets.main.resources.srcdirs[0].path ant.properties[' modelPackage '] = Properties.getproperty ("Package.model") ant.properties[' mapperpackage '] = Properties.getproperty ("Package.mapper" ) ant.properties[' sqlmapperpackage ' = PropErties.getproperty ("Package.xml") ant.taskdef (name: ' Mbgenerator ', classname: ' Org.mybatis.ge Nerator.ant.GeneratorAntTask ', Classpath:configurations.mybatisGenerator.asPath) Ant.mbgenerator (ove Rwrite:true, ConfigFile: ' Src/main/resources/mybatis/generatorconfig.xml ', verbose:true) {Propertys ET {propertyref (name: ' Targetproject ') propertyref (name: ' UserId ') propertyref (name: ' Driverclass ') propertyref (name: ' Connectionurl ') propertyref (name: ' Password ') proper Tyref (name: ' Src_main_java ') propertyref (name: ' Src_main_resources ') propertyref (name: ' Modelpacka GE ') propertyref (name: ' Mapperpackage ') propertyref (name: ' Sqlmapperpackage ')}}}
After waiting for Gradle to rebuild and index, you can see the Mybatisgenerator task in the Gradle window
Generate CodeDouble-click Mybatisgenerator to start the task, wait a moment will appear in the appropriate package file
AttentionNeed to build in an environment where network connectivity is normal, be sure to note that each directory corresponds correctly