Эх сурвалжийг харах

代码生成器搞定,重定义Base

NorthLan 7 жил өмнө
parent
commit
be947a3b7e

+ 2 - 2
zen-api/src/main/kotlin/com/gxzc/zen/sys/dao/TSysUserDao.kt → zen-api/src/main/kotlin/com/gxzc/zen/sys/dao/TSysUserMapper.kt

@@ -1,8 +1,8 @@
 package com.gxzc.zen.sys.dao
 
-import com.gxzc.zen.persistence.ZenBaseDao
+import com.gxzc.zen.persistence.ZenBaseMapper
 import com.gxzc.zen.sys.model.TSysUser
 import org.springframework.stereotype.Repository
 
 @Repository
-interface TSysUserDao : ZenBaseDao<TSysUser>
+interface TSysUserMapper : ZenBaseMapper<TSysUser>

+ 2 - 5
zen-api/src/main/kotlin/com/gxzc/zen/sys/service/TSysUserService.kt

@@ -1,12 +1,9 @@
 package com.gxzc.zen.sys.service
 
 import com.gxzc.zen.persistence.ZenBaseService
-import com.gxzc.zen.sys.dao.TSysUserDao
-import com.gxzc.zen.sys.dao.TestDao
+import com.gxzc.zen.sys.dao.TSysUserMapper
 import com.gxzc.zen.sys.model.TSysUser
-import com.gxzc.zen.sys.model.Test
-import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Service
 
 @Service
-class TSysUserService:ZenBaseService<TSysUser, TSysUserDao>()
+class TSysUserService:ZenBaseService<TSysUser, TSysUserMapper>()

+ 1 - 1
zen-api/src/main/resources/com/gxzc/zen/mapping/sys/TSysUserMapper.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="com.gxzc.zen.sys.dao.TSysUserDao">
+<mapper namespace="com.gxzc.zen.sys.dao.TSysUserMapper">
 
     <select id="findList" resultType="com.gxzc.zen.sys.model.TSysUser">
         SELECT *

+ 13 - 0
zen-common/src/main/kotlin/com/gxzc/zen/persistence/BaseMapper.kt

@@ -0,0 +1,13 @@
+package com.gxzc.zen.persistence
+
+import com.baomidou.mybatisplus.mapper.BaseMapper
+
+/**
+ * 通用的DAO
+ * 提供一些通用的特殊查询方法(除了Mybatis-plus提供的自动crud)
+ * 对应的Mapper.xml方法要自己写入
+ * @author NorthLan
+ * @date 2018/1/24
+ * @url https://noahlan.com
+ */
+interface BaseMapper<T> : BaseMapper<T>

+ 47 - 0
zen-common/src/main/kotlin/com/gxzc/zen/persistence/BaseModel.kt

@@ -0,0 +1,47 @@
+package com.gxzc.zen.persistence
+
+import com.baomidou.mybatisplus.annotations.TableField
+import com.baomidou.mybatisplus.annotations.TableId
+import com.baomidou.mybatisplus.annotations.TableLogic
+import com.baomidou.mybatisplus.enums.FieldFill
+import com.baomidou.mybatisplus.enums.FieldStrategy
+import com.baomidou.mybatisplus.enums.IdType
+import io.swagger.annotations.ApiModelProperty
+import java.util.*
+
+/**
+ * 通用实体 包含通用字段
+ * @author NorthLan at 2017/12/8
+ */
+//@JsonInclude(JsonInclude.Include.NON_NULL) // jackson 忽略空值
+abstract class BaseModel {
+    // 主键
+    @TableId(type = IdType.AUTO)
+    private var id: Long? = null
+
+    // 创建时间
+    @TableField(fill = FieldFill.INSERT, strategy = FieldStrategy.NOT_NULL)
+    @ApiModelProperty(hidden = true)
+    private var createTime: Date? = null
+
+    // 创建人id
+    @TableField(fill = FieldFill.INSERT, strategy = FieldStrategy.NOT_NULL)
+    private var createBy: Long? = null
+
+    // 更新时间
+    @TableField(fill = FieldFill.UPDATE, strategy = FieldStrategy.NOT_NULL)
+    private var updateTime: Date? = null
+
+    // 更新人id
+    @TableField(fill = FieldFill.UPDATE, strategy = FieldStrategy.NOT_NULL)
+    private var updateBy: Long? = null
+
+//    @TableField(exist = false)
+//    private var createdByName: String? = null
+
+//    @TableField(exist = false)
+//    private var updatedByName: String? = null
+
+    @TableLogic(value = "true", delval = "false")
+    private var enable: Boolean? = null
+}

+ 13 - 0
zen-common/src/main/kotlin/com/gxzc/zen/persistence/BaseService.kt

@@ -0,0 +1,13 @@
+package com.gxzc.zen.persistence
+
+import com.baomidou.mybatisplus.service.IService
+
+/**
+ * 通用Service
+ * 提供一些通用的特殊查询方法(除了Mybatis-plus提供的自动crud)
+ * 对应的Mapper.xml方法要自己写入
+ * @author NorthLan
+ * @date 2018/1/24
+ * @url https://noahlan.com
+ */
+interface BaseService<T> : IService<T>

+ 0 - 12
zen-common/src/main/kotlin/com/gxzc/zen/persistence/ZenBaseDao.kt

@@ -1,12 +0,0 @@
-package com.gxzc.zen.persistence
-
-import com.baomidou.mybatisplus.mapper.BaseMapper
-import org.apache.ibatis.annotations.Param
-
-/**
- * Created by mgr on 2018/1/23.
- */
-interface ZenBaseDao<T>: BaseMapper<T> {
-    fun finds(@Param("cm") params:Map<String, Any?>): List<Long>
-    fun findList(@Param("cm") params:Map<String, Any?>): List<T>
-}

+ 12 - 0
zen-common/src/main/kotlin/com/gxzc/zen/persistence/ZenBaseMapper.kt

@@ -0,0 +1,12 @@
+package com.gxzc.zen.persistence
+
+import com.baomidou.mybatisplus.mapper.BaseMapper
+import org.apache.ibatis.annotations.Param
+
+/**
+ * Created by mgr on 2018/1/23.
+ */
+interface ZenBaseMapper<T> : BaseMapper<T> {
+    fun finds(@Param("cm") params: Map<String, Any?>): List<Long>
+    fun findList(@Param("cm") params: Map<String, Any?>): List<T>
+}

+ 10 - 10
zen-common/src/main/kotlin/com/gxzc/zen/persistence/ZenBaseModel.kt

@@ -11,19 +11,19 @@ import java.util.*
 /**
  * Created by mgr on 2018/1/23.
  */
-abstract class ZenBaseModel: Serializable {
+abstract class ZenBaseModel : Serializable {
     @JsonSerialize(using = ToStringSerializer::class)
     @TableId(type = IdType.ID_WORKER)
-    var id:Long?=null
-    var createBy:String?=null
-    var createTime: Date?=null
-    var updateBy:String?=null
-    var updateTime: Date?=null
-    var remark:String?=null
-    var enable:Int?=null
+    var id: Long? = null
+    var createBy: String? = null
+    var createTime: Date? = null
+    var updateBy: String? = null
+    var updateTime: Date? = null
+    var remark: String? = null
+    var enable: Int? = null
 
     @TableField(exist = false)
-    var map:Map<String,Any?>?=null
+    var map: Map<String, Any?>? = null
 
-    open fun preHandle(){}
+    open fun preHandle() {}
 }

+ 2 - 2
zen-common/src/main/kotlin/com/gxzc/zen/persistence/ZenBaseService.kt

@@ -4,7 +4,6 @@ import com.baomidou.kisso.SSOHelper
 import com.baomidou.kisso.security.token.SSOToken
 import com.baomidou.mybatisplus.plugins.Page
 import com.baomidou.mybatisplus.service.IService
-import com.gxzc.zen.plug.IdGen
 import org.apache.commons.lang3.StringUtils
 import org.springframework.beans.factory.annotation.Autowired
 import java.util.*
@@ -13,7 +12,8 @@ import javax.servlet.http.HttpServletRequest
 /**
  * Created by mgr on 2018/1/23.
  */
-abstract class ZenBaseService<T: ZenBaseModel,D:ZenBaseDao<T>> {
+//interface ZenBaseService<T> : IService<T>
+abstract class ZenBaseService<T: ZenBaseModel,D: ZenBaseMapper<T>> {
 
     @Autowired
     protected val dao:D?=null

+ 60 - 42
zen-orm/src/main/kotlin/com/gxzc/zen/Generator.kt

@@ -3,6 +3,7 @@ package com.gxzc.zen
 import com.baomidou.mybatisplus.generator.AutoGenerator
 import com.baomidou.mybatisplus.generator.config.DataSourceConfig
 import com.baomidou.mybatisplus.generator.config.GlobalConfig
+import com.baomidou.mybatisplus.generator.config.PackageConfig
 import com.baomidou.mybatisplus.generator.config.StrategyConfig
 import com.baomidou.mybatisplus.generator.config.rules.DbType
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
@@ -13,50 +14,67 @@ import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
  * @date 2018/1/24
  * @url https://noahlan.me
  */
-class Generator {
+fun main(args: Array<String>) {
+    val mpg = AutoGenerator()
 
-    fun main(args: Array<String>) {
-        val mpg = AutoGenerator()
+    // 全局配置
+    val gc = GlobalConfig()
+    with(gc) {
+        outputDir = "D://Test"
+        isFileOverride = true
+        isActiveRecord = false
+        isEnableCache = false
+        isOpen = true
+        author = "NorthLan"
+        isKotlin = true
+        isBaseResultMap = true
+        mapperName = "%sDao"
+        xmlName = "%sDao"
+        serviceName = "I%sService"
+        serviceImplName = "%sServiceImpl"
+    }
+    mpg.globalConfig = gc
 
-        // 全局配置
-        val gc = GlobalConfig()
-        with(gc) {
-            outputDir = "D://Test"
-            isFileOverride = true
-            isActiveRecord = false
-            isEnableCache = false
-            isOpen = true
-            author = "NorthLan"
-            isKotlin = true
-            isBaseResultMap = true
-            mapperName = "%sDao"
-            xmlName = "%sDao"
-            serviceName = "I%sService"
-            serviceImplName = "%sServiceImpl"
-        }
-        mpg.globalConfig = gc
+    // 数据源
+    val dataSource = DataSourceConfig()
+    with(dataSource) {
+        dbType = DbType.MYSQL
+        // typeConvert
+        driverName = "com.mysql.jdbc.Driver"
+        username = "root"
+        password = "root"
+        url = "jdbc:mysql://127.0.0.1:3306/rest?characterEncoding=utf8"
+    }
+    mpg.dataSource = dataSource
 
-        // 数据源
-        val dataSource = DataSourceConfig()
-        with(dataSource) {
-            dbType = DbType.MYSQL
-            // typeConvert
-            driverName = "com.mysql.jdbc.Driver"
-            username = "root"
-            password = "root"
-            url = "jdbc:mysql://127.0.0.1:3306/rest?characterEncoding=utf8"
-        }
-        mpg.dataSource = dataSource
+    // 策略配置
+    val strategy = StrategyConfig()
+    with(strategy) {
+        setDbColumnUnderline(true)
+        isCapitalMode = false
+        naming = NamingStrategy.underline_to_camel
+        //            setTablePrefix()
+        superEntityClass = "com.gxzc.zen.persistence.BaseModel"
+        setSuperEntityColumns("id", "createBy", "createTime", "updateBy", "updateTime", "remark", "enable")
+        superMapperClass = "com.gxzc.zen.persistence.BaseMapper"
+        superServiceClass = "com.gxzc.zen.persistence.BaseService"
+        superServiceImplClass = "com.baomidou.mybatisplus.service.impl.ServiceImpl"
+//            superControllerClass = ""
+        setInclude("user")
+        isEntityBooleanColumnRemoveIsPrefix = true
+        logicDeleteFieldName = "enable"
+//            setExclude("")
+    }
+    mpg.strategy = strategy
 
-        // 策略配置
-        val strategyConfig = StrategyConfig()
-        with(strategyConfig) {
-            setDbColumnUnderline(true)
-            //            setTablePrefix()
-            naming = NamingStrategy.underline_to_camel
-            setInclude("user")
-            setExclude("")
-            setSuperEntityClass("com.gxzc.zen.persistence.ZenBaseDao")
-        }
+    // 包配置
+    val pc = PackageConfig()
+    with(pc) {
+        parent = "com.gxzc.zen.persistence"
+//            moduleName = "test"
+        entity = "model"
     }
-}
+    mpg.packageInfo = pc
+
+    mpg.execute()
+}