添加依赖,配置mybatis-generator
build.gradle
// 配置阿里云的源
buildscript {
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/groups/public"
}
}
// 构建时候的一些依赖
dependencies {
}
}
// 编辑打包jar包时需要用到的依赖
plugins {
// id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
// // lombok使用打包时需加上这个 否则打包时候会报无法找到 getter 等一些方法
id("io.freefair.lombok") version "3.1.4"
}
jar.enabled=true
group = 'com.drink'
version = '1.0'
sourceCompatibility = '1.8'
apply plugin: 'java'
//1、添加
configurations {
mybatisGenerator
}
dependencies {
// springboot 的基本依赖
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.1.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.1.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-solr', version: '2.1.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '2.1.4.RELEASE'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.1'
compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.5'
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2'
mybatisGenerator 'mysql:mysql-connector-java:5.1.21'
mybatisGenerator 'tk.mybatis:mapper:3.3.1'
// mysql
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
//3、添加任务
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/mybatis/db-mysql.properties").withInputStream { inputStream ->
properties.load(inputStream)
}
properties;
}
// 生成mapper
task mybatisGenerate {
def properties = getDbProperties()
ant.properties['targetProject'] = projectDir.path
ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
ant.properties['userId'] = properties.getProperty("jdbc.user")
ant.properties['password'] = properties.getProperty("jdbc.pass")
ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
ant.properties['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.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: "${rootDir}/drink-model/src/main/resources/mybatis/generatorConfig.xml", verbose: true) {
propertyset {
propertyref(name: 'targetProject')
propertyref(name: 'userId')
propertyref(name: 'driverClass')
propertyref(name: 'connectionURL')
propertyref(name: 'password')
propertyref(name: 'src_main_java')
propertyref(name: 'src_main_resources')
propertyref(name: 'modelPackage')
propertyref(name: 'mapperPackage')
propertyref(name: 'sqlMapperPackage')
}
}
}
再resource下添加配置文件
db-mysql.properties
# JDBC 驱动类名
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://localhost:8889/bar-local
# JDBC 用户名及密码
jdbc.user=root
jdbc.pass=root
jdbc.driverClassName=com.mysql.jdbc.Driver
# 生成实体类所在的包
package.model=com.drink.model
# 生成 mapper 类所在的包
package.mapper=com.drink.model.mapper
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper
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="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 配置内置的或者自定义的Plugin -->
<!--<plugin type="plugin.MysqlPaginationPlugin"/>-->
<plugin type="org.mybatis.generator.plugins.MySQLPaginationPlugin"></plugin>
<!-- 为模型生成序列化方法-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 为生成的Java模型创建一个toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- <commentGenerator type="com.drink.model.CommentGenerator">-->
<!-- <!– 是否去除自动生成的注释 true:是 : false:否 –>-->
<!-- <property name="suppressAllComments" value="true"/>-->
<!-- <property name="suppressDate" value="true"/>-->
<!-- <property name="addRemarkComments" value="true"/>-->
<!-- </commentGenerator>-->
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
<!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
<!-- sql占位符,表示所有的表 -->
<table tableName="%" enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>