好久沒有寫分享了,由於最近一直在用 kotlin 寫 spring,做爲 kotlin 新人遇到的坑基本都能 Google 到,也就不必在寫一次了。今天將我在開發過程當中使用 kotlin 加 mybatis-plus 遇到的問題寫出來,給即將入坑的朋友。前端
kotlin
打個廣告若是你正在寫 Java 代碼,能夠嘗試使用 kotlin,歷史的積累不會失去,kotlin 的優雅會讓你愛不釋手。mysql
mybatis-plus 在 kotlin 中使用已經能夠完美使用了,這一篇主要寫 mybatis-plus 代碼生成器的配置(伸手黨福利),由於官方demo比較老了,甚至它仍是Java代碼,這是我難以接受的。程序員
在 mybatis-plus 中使用 kotlin 模式很簡單,只需spring
gc.isKotlin = true
kotlin 版本完整的代碼生成器sql
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<String>) { // 表名,多個繼續寫 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() }
表名直接寫更有效率,省去輸入環節前端工程師
package ${package.Entity} #foreach($pkg in ${table.importPackages}) import ${pkg} #end #if(${swagger2}) import io.swagger.annotations.ApiModel import io.swagger.annotations.ApiModelProperty #end /** * <p> * $!{table.comment} * </p> * * @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})<${entity}>#end() { #elseif(${activeRecord}) class ${entity} : Model<${entity}>() { #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 "}" } }
package ${package.Mapper} import ${package.Entity}.${entity} import ${superMapperClassPackage} /** * <p> * $!{table.comment} Mapper 接口 * </p> * * @author ${author} * @since ${date} */ interface ${table.mapperName} : ${superMapperClass}<${entity}>
package ${package.Service} import ${package.Entity}.${entity} import ${superServiceClassPackage} /** * <p> * $!{table.comment} 服務類 * </p> * * @author ${author} * @since ${date} */ interface ${table.serviceName} : ${superServiceClass}<${entity}>
package ${package.ServiceImpl} import ${package.Entity}.${entity} import ${package.Mapper}.${table.mapperName} import ${package.Service}.${table.serviceName} import ${superServiceImplClassPackage} import org.springframework.stereotype.Service /** * <p> * $!{table.comment} 服務實現類 * </p> * * @author ${author} * @since ${date} */ @Service class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName}
以上就是在我在寫kotlin時,mybatis-plus代碼生成器所有配置,若是你須要,只要拿去隨便改改就能夠了,另外基礎配置請自行Google。mybatis