mybatis plus 3.3.2 kotlin代码自动生成

1 pom

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-generatorartifactId>
    <version>3.3.2version>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-freemarkerartifactId>
dependency>

2 show your code

不需要代码生成时,即代码生成后注意要在generator ut方法上加@Ignore注解

package com.xx.xx.xx.generator

import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.core.toolkit.StringPool
import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine
import org.junit.Ignore
import org.junit.Test
import org.springframework.boot.test.context.SpringBootTest
import java.util.ArrayList

/**
 * @program: xxx-yyyyyy
 *
 * @description: GeneratorTest
 *
 * @author: loulvlin
 *
 * @create: 2020-05-26 17:26
 **/
@SpringBootTest
class GeneratorTest {

    @Ignore
    @Test
    fun userdir(){
        val projectPath = System.getProperty("user.dir")
        println(projectPath)
    }

    /**
    * @Description: 只有需要生成代码时才放开此单元测试方法
    * @Param: 
    * @return: 
    * @Author: loulvlin
    * @Date: 2020/5/27 上午8:57
    */
//    @Ignore
    @Test
    fun generator(){
        println("begin generating")
        val projectPath = System.getProperty("user.dir")

        var mpg = AutoGenerator()

        var globalConfig = GlobalConfig();
        globalConfig.isKotlin = true
        globalConfig.author = "xxxxxx"
        globalConfig.isOpen = false
        globalConfig.outputDir = System.getProperty("user.dir") + "/src/main/kotlin"
        globalConfig.isFileOverride = true
        globalConfig.idType = IdType.AUTO
        globalConfig.xmlName = "%sMapper"
        globalConfig.mapperName = "%sMapper"
        globalConfig.serviceImplName = "%sServiceImpl"
        globalConfig.controllerName = "%sController"
        globalConfig.isBaseColumnList = true
        globalConfig.isBaseResultMap = true

        mpg.globalConfig = globalConfig

        var dsc = com.baomidou.mybatisplus.generator.config.DataSourceConfig()
        dsc.dbType = DbType.MYSQL
        dsc.driverName = "com.mysql.cj.jdbc.Driver"
        dsc.url = "jdbc:mysql://ip:port/aos-center?useUnicode=true&characterEncoding=utf8"
        dsc.username = "xxxxx"
        dsc.password = "xxxxx"

        mpg.dataSource = dsc


        // 自定义配置
        val cfg: InjectionConfig = object : InjectionConfig() {
            override fun initMap() {
                // to do nothing
            }
        }
        val focList: MutableList<FileOutConfig> = ArrayList()
        focList.add(object : FileOutConfig("/templates/mapper.xml.ftl") {
            override fun outputFile(tableInfo: TableInfo): String? {
                // 自定义输入文件名称
                return (projectPath.toString() + "/src/main/resources/mapper/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML)
            }
        })

        // config xml template and outpath
        cfg.fileOutConfigList = focList
        mpg.cfg = cfg

        mpg.template = TemplateConfig().setXml(null)
//                .setController("")
//                .setService("")
//                .setServiceImpl("")


        var sc = StrategyConfig()
        sc.columnNaming = NamingStrategy.underline_to_camel
        sc.setTablePrefix("sys")
        sc.setInclude("sys_xxx_yyyy")
        sc.naming = NamingStrategy.underline_to_camel
        sc.isEntityLombokModel = true
        sc.isRestControllerStyle = true
        mpg.strategy = sc

        var pkConfig = PackageConfig()
        pkConfig.parent = "com.xxxx.xxxxx.xxxx"
        pkConfig.controller = "controller"
        pkConfig.entity = "entity"
        pkConfig.service = "service"
        pkConfig.mapper = "dao"
        mpg.packageInfo = pkConfig

        mpg.templateEngine = FreemarkerTemplateEngine()
        mpg.execute();
    }
}

3 说明

  • 数据源配置需要换成自己的,dsc.dbType = DbType.MYSQL按需切换
  • globalConfig.author = “loulvlin” author需要换成自己的名字(for 注释)
  • 如果不想生成xml,只需要注释下面两行代码:
        // config xml template and outpath
        cfg.fileOutConfigList = focList
        mpg.cfg = cfg
  • 如果不想生成controller service serviceImpl,只需要配置TemplateConfig相应的set方法为""即可,如下
        mpg.template = TemplateConfig().setXml(null)
                .setController("")
                .setService("")
                .setServiceImpl("")
  • 配置需要自动生成代码的表和前缀,如配置了前缀,生成的entity名称中不会出现前缀,不配置前缀也可以
        sc.setTablePrefix("sys")
        sc.setInclude("sys_case_dict")
  • 自定义service或controller的基类(按需配置)
        sc.superControllerClass = "com.aos.base.controller.BaseController"
        sc.superServiceClass = "com.aos.base.service.BaseService"
  • 生成的entity需要在类前加个open keyword,for redis序列化
  • 生成的entity 字段类型 LocalDateTime改为 Date(java.util)
@TableName("sys_case_dict")
open class Xxxxxx : Serializable {


    /**
     * 主键(通过自增生成)
     */
    @TableId(value = "id", type = IdType.AUTO)
    var id: Int? = null
    
    /**
     * 创建时间
     */
    var createTime: Date? = null

    /**
     * 修改时间
     */
    var modifyTime: Date? = null


}

你可能感兴趣的:(mybatis,mybatis,plus,代码自动生成,kotlin,不生成controller,不生成serviceImpl)