mybatis-plusのkotin構成.md
6893 ワード
久しぶりに分かち合いを書きました.最近ずっとkotlinでspringを書いているので、kotlinの新人として出会った穴はほとんどGoogleで着くことができて、一度書く必要はありません.今日は私が開発中にkotlinとmybatis-plusを使って出会った問題を書いて、穴に入る友达にあげます.
まず
Javaコードを書いている場合は、kotlinを使ってみてください.歴史の蓄積は失われません.kotlinの優雅さはあなたを手放せません.
本文
mybatis-plusはkotlinで使用することができてすでに完璧に使用することができて、この1篇は主にmybatis-plusコード生成器の配置(手を伸ばして党の福祉に手を伸ばす)を書いて、公式demoが比較的に古いため、甚だしきに至ってはそれはJavaコードで、これは私が受け入れにくいです.
mybatis-plusでkotlinモードを使うのは簡単で、
kotlinバージョンの完全なコードジェネレータ
テーブル名を直接書くほうが効率的で、入力を省くことができます
テンプレートファイル
entity.kt.vm
mapper.kt.vm
service.kt.vm
serviceImpl.kt.vm
コメント
XMLを拒否して、私から始めます配置中の生成XMLをオフにしました.フロントエンドエンジニア出身のプログラマーとして、XMLという醜い文法は本当に受け入れられません.XMLでできることは注釈してもいいので、統一的な開発体験のために、優雅さのために、私はそれを使用することを拒否しました.
コントロールファイルを自動的に生成しないのほとんどの場合、テーブルとインタフェース名が一致しないと、 は自動的に生成されません.
以上、kotlinを書くとき、mybatis-plusコードジェネレータはすべて構成しています.必要なら、勝手に変更すればいいです.また、基礎構成は自分でGoogleしてください.
まず
kotlin
に広告を出しますJavaコードを書いている場合は、kotlinを使ってみてください.歴史の蓄積は失われません.kotlinの優雅さはあなたを手放せません.
本文
mybatis-plusはkotlinで使用することができてすでに完璧に使用することができて、この1篇は主にmybatis-plusコード生成器の配置(手を伸ばして党の福祉に手を伸ばす)を書いて、公式demoが比較的に古いため、甚だしきに至ってはそれはJavaコードで、これは私が受け入れにくいです.
mybatis-plusでkotlinモードを使うのは簡単で、
gc.isKotlin = true
kotlinバージョンの完全なコードジェネレータ
import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine
/**
* @author spiderMan
* @since 2019-09-23
*/
fun main(args: Array) {
// ,
val tableNameList = listOf("t_user", "t_user2")
val mpg = AutoGenerator()
//
val gc = GlobalConfig()
val projectPath = System.getProperty("user.dir")
gc.outputDir = "$projectPath/src/main/kotlin"
gc.author = "spiderMan"
gc.isOpen = false
gc.isKotlin = true
gc.isSwagger2 = true
gc.entityName = "%sDO"
mpg.globalConfig = gc
//
val dsc = DataSourceConfig()
dsc.url = "jdbc:mysql://localhost:3306/t_user?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true"
dsc.driverName = "com.mysql.jdbc.Driver"
dsc.username = "root"
dsc.password = "zaq12wsx"
mpg.dataSource = dsc
//
val pc = PackageConfig()
// ,
pc.parent = "com.**.*"
mpg.packageInfo = pc
//
val templateConfig = TemplateConfig()
//
templateConfig.entityKt = "templates/entity.kt"
templateConfig.mapper = "templates/mapper.kt"
templateConfig.service = "templates/service.kt"
templateConfig.serviceImpl = "templates/serviceImpl.kt"
templateConfig.controller = null
templateConfig.xml = null
mpg.template = templateConfig
//
val strategy = StrategyConfig()
strategy.naming = NamingStrategy.underline_to_camel
strategy.columnNaming = NamingStrategy.underline_to_camel
strategy.superEntityClass = "com.mybatis.app.common.BaseEntity"
//
strategy.setSuperEntityColumns("auto_id")
strategy.setInclude(*tableNameList.toTypedArray())
strategy.isControllerMappingHyphenStyle = true
strategy.setTablePrefix(pc.moduleName + "_")
mpg.strategy = strategy
mpg.templateEngine = VelocityTemplateEngine()
mpg.execute()
}
テーブル名を直接書くほうが効率的で、入力を省くことができます
テンプレートファイル
entity.kt.vm
package ${package.Entity}
#foreach($pkg in ${table.importPackages})
import ${pkg}
#end
#if(${swagger2})
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
#end
/**
*
* $!{table.comment}
*
*
* @author ${author}
* @since ${date}
*/
#if(${table.convert})
@TableName("${table.name}")
#end
#if(${swagger2})
@ApiModel(value="${entity} ", description="$!{table.comment}")
#end
#if(${superEntityClass})
class ${entity} : ${superEntityClass}#if(${activeRecord})#end() {
#elseif(${activeRecord})
class ${entity} : Model() {
#else
class ${entity} : Serializable {
#end
## ---------- BEGIN ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${swagger2})
@ApiModelProperty(value = "${field.comment}")
#else
/**
* ${field.comment}
*/
#end
#end
#if(${field.keyFlag})
##
#if(${field.keyIdentityFlag})
@TableId(value = "${field.name}", type = IdType.AUTO)
#elseif(!$null.isNull(${idType}) && "$!idType" != "")
@TableId(value = "${field.name}", type = IdType.${idType})
#elseif(${field.convert})
@TableId("${field.name}")
#end
##
#elseif(${field.fill})
## ----- -----
#if(${field.convert})
@TableField(value = "${field.name}", fill = FieldFill.${field.fill})
#else
@TableField(fill = FieldFill.${field.fill})
#end
#elseif(${field.convert})
@TableField("${field.name}")
#end
##
#if(${versionFieldName}==${field.name})
@Version
#end
##
#if(${logicDeleteFieldName}==${field.name})
@TableLogic
#end
#if(${field.propertyType} == "Integer")
var ${field.propertyName}: Int? = null
#else
var ${field.propertyName}: ${field.propertyType}? = null
#end
#end
## ---------- END ----------
#if(${entityColumnConstant})
companion object {
#foreach($field in ${table.fields})
const val ${field.name.toUpperCase()} : String = "${field.name}"
#end
}
#end
#if(${activeRecord})
override fun pkVal(): Serializable? {
#if(${keyPropertyName})
return ${keyPropertyName}
#else
return null
#end
}
#end
override fun toString(): String {
return "${entity}{" +
#foreach($field in ${table.fields})
#if($!{foreach.index}==0)
"${field.propertyName}=" + ${field.propertyName} +
#else
", ${field.propertyName}=" + ${field.propertyName} +
#end
#end
"}"
}
}
mapper.kt.vm
package ${package.Mapper}
import ${package.Entity}.${entity}
import ${superMapperClassPackage}
/**
*
* $!{table.comment} Mapper
*
*
* @author ${author}
* @since ${date}
*/
interface ${table.mapperName} : ${superMapperClass}
service.kt.vm
package ${package.Service}
import ${package.Entity}.${entity}
import ${superServiceClassPackage}
/**
*
* $!{table.comment}
*
*
* @author ${author}
* @since ${date}
*/
interface ${table.serviceName} : ${superServiceClass}
serviceImpl.kt.vm
package ${package.ServiceImpl}
import ${package.Entity}.${entity}
import ${package.Mapper}.${table.mapperName}
import ${package.Service}.${table.serviceName}
import ${superServiceImplClassPackage}
import org.springframework.stereotype.Service
/**
*
* $!{table.comment}
*
*
* @author ${author}
* @since ${date}
*/
@Service
class ${table.serviceImplName} : ${superServiceImplClass}(), ${table.serviceName}
コメント
XMLを拒否して、私から始めます
コントロールファイルを自動的に生成しない
以上、kotlinを書くとき、mybatis-plusコードジェネレータはすべて構成しています.必要なら、勝手に変更すればいいです.また、基礎構成は自分でGoogleしてください.