package com.gxzc.zen import com.baomidou.mybatisplus.generator.AutoGenerator import com.baomidou.mybatisplus.generator.InjectionConfig import com.baomidou.mybatisplus.generator.config.* import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert import com.baomidou.mybatisplus.generator.config.po.TableInfo import com.baomidou.mybatisplus.generator.config.rules.DbColumnType import com.baomidou.mybatisplus.generator.config.rules.DbType import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy import java.io.File /** * 代码生成器 * @author NorthLan * @date 2018/1/24 * @url https://noahlan.me */ fun main(args: Array) { generate(true, "NorthLan", "bus", DataSourceConfig().also { it.dbType = DbType.MYSQL it.typeConvert = object : MySqlTypeConvert() { override fun processTypeConvert(fieldType: String?): DbColumnType { return super.processTypeConvert(fieldType) } } it.driverName = "com.mysql.jdbc.Driver" it.username = "archives" it.password = "archives" it.url = "jdbc:mysql://192.168.1.124:3307/archives_mgr?characterEncoding=utf8" // it.url = "jdbc:mysql://127.0.0.1:3306/archives_mgr?characterEncoding=utf8" // it.username = "root" // it.password = "root" }) } /** * 代码生成器具体执行方法
* @param isKotlin 是否生成kotlin代码 * @param author 作者 * @param pkgType 包类别 (sys/bus) * @param dataSourceConfig 数据源配置 */ fun generate(isKotlin: Boolean, author: String, pkgType: String, dataSourceConfig: DataSourceConfig) { val mpg = AutoGenerator() val path = File("zen-api").absolutePath // 全局配置 mpg.globalConfig = GlobalConfig().also { it.outputDir = if (isKotlin) { "$path/src/main/kotlin" } else { "$path/src/main/java" } it.isFileOverride = true it.isActiveRecord = false it.isEnableCache = false it.isOpen = false it.author = author it.isKotlin = isKotlin it.isBaseResultMap = true it.isBaseColumnList = false it.mapperName = "%sMapper" it.xmlName = "%sMapper" it.serviceName = "I%sService" it.serviceImplName = "%sServiceImpl" } // 数据源 mpg.dataSource = dataSourceConfig // 策略配置 mpg.strategy = StrategyConfig().also { it.setDbColumnUnderline(true) it.isCapitalMode = false it.naming = NamingStrategy.underline_to_camel // setTablePrefix() it.superEntityClass = "com.gxzc.zen.common.base.BaseModel" it.setSuperEntityColumns("id", "create_by", "create_time", "update_by", "update_time", "remark", "enable") it.superMapperClass = "com.gxzc.zen.common.base.BaseMapper" it.superServiceClass = "com.gxzc.zen.common.base.BaseService" it.superServiceImplClass = "com.baomidou.mybatisplus.service.impl.ServiceImpl" // superControllerClass = "" // setInclude("user") it.isEntityBooleanColumnRemoveIsPrefix = true it.logicDeleteFieldName = "enable" // setExclude("") } // 包配置 mpg.packageInfo = PackageConfig().also { it.parent = "com.gxzc.zen.api.$pkgType" it.entity = "model" } val cfg = object : InjectionConfig() { override fun initMap() { } }.also { it.fileOutConfigList = ArrayList().also { it.add(object : FileOutConfig("/templates/mapper.xml.vm") { override fun outputFile(tableInfo: TableInfo?): String { return "$path/src/main/resources/mapping/$pkgType/${tableInfo?.mapperName}.xml" } }) } } mpg.cfg = cfg mpg.template = TemplateConfig().also { it.controller = null // 不生成controller it.xml = null } mpg.execute() }