初始化项目
增加项目依赖
增加Kotlin依赖
//build.gradle
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
}
dependencies {
......
......
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
......
......
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
增加Lombok依赖
see https://kotlinlang.org/docs/lombok.html
//build.gradle
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
id 'org.jetbrains.kotlin.plugin.lombok' version '1.7.10'
id 'io.freefair.lombok' version '5.3.0'
}
增加MybatisPlus依赖
//build.gradle
dependencies {
......
......
implementation 'mysql:mysql-connector-java'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.2'
testImplementation 'com.baomidou:mybatis-plus-generator:3.5.3'
testImplementation 'org.apache.velocity:velocity-engine-core:2.3'
}
配置MybatisPlus生成器
package com.example.boot.demo
import cn.hutool.core.date.DateUtil
import com.baomidou.mybatisplus.generator.FastAutoGenerator
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.rules.DateType
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import java.util.*
import kotlin.io.path.Path
import kotlin.io.path.pathString
@SpringBootTest
class MybatisPlusCodeGenTest {
private val path = "XXX/Projects/boot-demo/src/main"
@Test
fun index() {
FastAutoGenerator.create(url, username, password)
.globalConfig { builder: GlobalConfig.Builder ->
builder.author("scz") // 设置作者
.enableSpringdoc()
.dateType(DateType.ONLY_DATE)
.commentDate { DateUtil.date().year().toString() }
// .commentDate("yyyy-MM-dd HH:mm:ss")
.disableOpenDir()
.fileOverride()
.outputDir(Path(path, "java").pathString) // 指定输出目录
}
.packageConfig { builder: PackageConfig.Builder ->
builder.parent("com.example.boot.demo") // 设置父包名
.pathInfo(
Collections.singletonMap(
OutputFile.xml,
Path(path, "resources", "mapper").pathString
)
) // 设置mapperXml生成路径
}
.templateConfig { builder ->
builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICE_IMPL)
}
.strategyConfig { builder: StrategyConfig.Builder ->
builder.addExclude(listOf())
builder.entityBuilder()
.disableSerialVersionUID()
.enableColumnConstant()
.enableLombok()
.enableFileOverride()
.enableTableFieldAnnotation()
.addIgnoreColumns()
builder.mapperBuilder()
.enableMapperAnnotation()
.formatMapperFileName("%sMapper")
.formatXmlFileName("%sMapper")
}
.templateEngine(VelocityTemplateEngine())
.execute()
}
}
配置MyBatis-Plus分页
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//还可以添加i他的拦截器
return interceptor;
}
}