Configure MyBatis Generators based on Gradle6.9 to reverse generate ORM code

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

Recently, all springboot related projects have been migrated to gradle and compiled through gradle. The implementation process is quite tortuous. Since the backward compatibility of gradle has always been poor, if it is the latest version of gradle7.1.1, it is very likely that the configuration in the previous build.gradle cannot be used. What needs to be explained here is that in the gradle environment, the related plugins are used in the ant way.

1. build.gradle configuration

The complete configuration is as follows:

plugins {
    id 'org.springframework.boot' version '2.5.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.springframework.experimental.aot' version '0.10.2-SNAPSHOT'
    id 'org.graalvm.buildtools.native' version '0.9.1'
    # 增加gradle Mybatisgenerator插件
    id 'com.arenagod.gradle.MybatisGenerator' version '1.4'
}

group = 'com.dhb'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

# 导入MybatisGenerator插件
apply plugin: "com.arenagod.gradle.MybatisGenerator"

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    # 增加自定义task
    mybatisGenerator
}

repositories {
    maven { url 'https://repo.spring.io/snapshot' }
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    # 自定义mybatisGenerator task需要的包
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'tk.mybatis:mapper:3.3.9'
    mybatisGenerator 'mysql:mysql-connector-java:6.0.6'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
    implementation 'io.netty:netty-all:4.1.56.Final'
    implementation 'com.google.guava:guava:23.0'
    implementation 'org.apache.httpcomponents:httpclient:4.5.13'
    implementation 'org.apache.httpcomponents:httpcore:4.4.14'
    implementation 'org.apache.httpcomponents:httpcore-nio:4.4.14'
    implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
    implementation 'com.squareup.okhttp3:okhttp:4.9.1'
    implementation 'mysql:mysql-connector-java:6.0.6'
    implementation 'com.zaxxer:HikariCP:4.0.3'
    # generator-core包
    compileOnly 'org.mybatis.generator:mybatis-generator-core:1.3.5'
}

#自定义的task mybatisGenerate
task mybatisGenerate {
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )
    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/generator/generatorConfig.xml', verbose: true)
}


test {
    useJUnitPlatform()
}

bootBuildImage {
    builder = 'paketobuildpacks/builder:tiny'
    environment = ['BP_NATIVE_IMAGE': 'true']
}

复制代码

In the above configuration, ant is executed through the custom task mybatisGenerate. The key configuration is src/main/resources/generator/generatorConfig.xml.

2. generatorConfig.xml configuration

In generatorConfig.xml, configure the tables that need to be generated by the generator. The complete configuration is as follows:

<?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>
	<!-- 设置mysql驱动路径 -->

	<context id="ParaGenconfig" targetRuntime="MyBatis3"
		defaultModelType="hierarchical">
		<property name="autoDelimitKeywords" value="true" />
		<property name="beginningDelimiter" value="`" />
		<property name="endingDelimiter" value="`" />
		<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
		<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
		<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
			<property name="suppressDate" value="true" />
		</commentGenerator>
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://192.168.162.49:3306/gts?useSSL=false"
			userId="gts" password="mysql">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="true" />
		</javaTypeResolver>
		<javaModelGenerator targetPackage="com.dhb.gts.javacourse.week5.entity" targetProject="src/main/java" />
		<sqlMapGenerator targetPackage="com.dhb.gts.javacourse.week5.mybatis" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<javaClientGenerator type="ANNOTATEDMAPPER"
			targetPackage="com.dhb.gts.javacourse.week5.mapper" targetProject="src/main/java"
			implementationPackage="com.dhb.gts.javacourse.week5.mapper.impl">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<table tableName="users" domainObjectName="Users"></table>
	</context>
</generatorConfiguration>

复制代码

The above is the complete configuration of generatorConfig.xml.

3. Execute the generator

On the right side of Gradle -> other -> mybatisGeneratic click Run to execute.mybatis generator execution

This generates the relevant code for mybatis.

In the whole process, you need to pay attention to the version of gradle. You can modify the gradle-wrapper.properties in the gradle wrapper in the project root directory. Change the version in distributionUrl to the specified version, the version in this article is 6.9.1.

Guess you like

Origin juejin.im/post/7084151620904648734