注意以下5点配置:
Gradle配置文件参考:
plugins {
id'org.springframework.boot' version'2.1.6.RELEASE'
id'java'
//gradle提供的mybatis generator插件
id"com.arenagod.gradle.MybatisGenerator" version"1.4"
}
applyplugin:'io.spring.dependency-management'
//引入 mybatis-generator 插件
applyplugin:"com.arenagod.gradle.MybatisGenerator"
group ='com.XXXX.base.auth'
version ='0.0.1-SNAPSHOT'
sourceCompatibility ='1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFromdevelopmentOnly
}
// remove default logger
all*.excludegroup:'org.springframework.boot',module:'spring-boot-starter-logging'
// 引入mybatisGenerator插件
mybatisGenerator
}
repositories {
// 使用阿里云的MAVEN仓库地址
maven {
url'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenCentral()
}
dependencies {
implementation'org.springframework.boot:spring-boot-starter-freemarker'
implementation'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation'org.springframework.boot:spring-boot-starter-security'
implementation'org.springframework.boot:spring-boot-starter-web'
implementation'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.0'
developmentOnly'org.springframework.boot:spring-boot-devtools'
implementation'org.springframework.boot:spring-boot-starter-aop'
implementation'org.springframework.boot:spring-boot-starter-data-redis'
implementation'org.springframework.boot:spring-boot-starter-log4j2'
runtimeOnly'mysql:mysql-connector-java'
testImplementation'org.springframework.boot:spring-boot-starter-test'
testImplementation'org.springframework.security:spring-security-test'
testImplementation'org.mybatis.generator:mybatis-generator-core:1.3.2'
// ………………此处省略N个配置………………
}
// mybatis-generator.xml 配置路径
//这里会遇到个问题:MyBatis Generator 通过xml生成,有日志但是没有生成文件成功的问题,
//原因:mac下是找不到 ./src 路径的,需要全路径,如下配置。windows则为src/main/resources/generator.xml
mybatisGenerator {
verbose =true
configFile ='src/main/resources/conf/generator.xml'
}
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>
<context id="my" targetRuntime="MyBatis3">
<!--自动实现Serializable接口-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库基本信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/XXXX"
userId="root"
password="admin">
</jdbcConnection>
<!--生成实体类的位置以及包的名字-->
<!--同样Mac用户:targetProject 为全路径(/Users/murasakiseifu/mybatisGenerator/src/main/java)-->
<javaModelGenerator targetPackage="com.XXXX.base.auth.entity" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置-->
<!--同样Mac用户:targetProject 为全路径(/Users/murasakiseifu/mybatisGenerator/src/main/resources")-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成Dao类存放位置,mapper接口生成的位置-->
<!--同样Mac用户:targetProject 为全路径(/Users/murasakiseifu/mybatisGenerator/src/main/java)-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.XXXX.base.auth.dao" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 -->
<table schema="hisp" tableName="human_users"
domainObjectName="human_users" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
刷新Gradle后可以双击mbGenerator运行(前提是要配置好自己的generator.xml哦)
参考,感谢:
MurasakiSeiFu
https://segmentfault.com/a/1190000013534059