表字段注释,自动生成数据库文档

master
pan 4 years ago
parent 1139824098
commit 741d966170
  1. 47
      src/Dao.kt
  2. 5
      src/MySQL.kt
  3. 52
      src/Util.kt
  4. 33
      test/ApplicationTest.kt

@ -8,22 +8,16 @@ import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.`java-time`.datetime import org.jetbrains.exposed.sql.`java-time`.datetime
import java.time.LocalDateTime import java.time.LocalDateTime
/**
* 用户 @TableComment("用户")
*/
object Users: IntIdTable(){ object Users: IntIdTable(){
/** @TableComment("学号")
* 学号
*/
val studentId:Column<String> = varchar(name="student_id",length = 8).uniqueIndex() val studentId:Column<String> = varchar(name="student_id",length = 8).uniqueIndex()
/**
* 姓名 @TableComment("姓名")
*/
val name:Column<String> = varchar(name="name",length = 10) val name:Column<String> = varchar(name="name",length = 10)
/** @TableComment("密码")
* 密码,hash加密
*/
val password:Column<String> = varchar(name="password",length = 32) val password:Column<String> = varchar(name="password",length = 32)
} }
@ -34,29 +28,22 @@ class User(id:EntityID<Int>):IntEntity(id){
var password by Users.password var password by Users.password
} }
/** @TableComment("用户授权令牌")
* 用户授权令牌
*/
object UserTokens: IntIdTable(){ object UserTokens: IntIdTable(){
/**
* 授权学号 @TableComment("授权学号")
*/
val studentId:Column<String> = reference("student_id",Users.studentId) val studentId:Column<String> = reference("student_id",Users.studentId)
/**
* 令牌 @TableComment("令牌")
*/
val token:Column<String> = varchar(name="token",length = 32) val token:Column<String> = varchar(name="token",length = 32)
/**
* 授权ip地址 @TableComment("授权ip地址")
*/
val ip:Column<String> = varchar(name="ip",length = 32) val ip:Column<String> = varchar(name="ip",length = 32)
/**
* 令牌创建时间 @TableComment("令牌创建时间")
*/
val createTime:Column<LocalDateTime> = datetime("create_time").default(LocalDateTime.now()) val createTime:Column<LocalDateTime> = datetime("create_time").default(LocalDateTime.now())
/**
* 授权设备 @TableComment("授权设备")
*/
val device:Column<String> = varchar(name="device",length = 256) val device:Column<String> = varchar(name="device",length = 256)
} }

@ -40,7 +40,8 @@ fun Application.MySQL(testing: Boolean = false){
fun initTable(){ fun initTable(){
transaction { transaction {
SchemaUtils.createMissingTablesAndColumns(Users) val tableList= arrayOf(Users,UserTokens)
SchemaUtils.createMissingTablesAndColumns(UserTokens) SchemaUtils.createMissingTablesAndColumns(*tableList)
updateComment(*tableList)
} }
} }

@ -1,5 +1,10 @@
package com.gyf.csams package com.gyf.csams
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import java.math.BigInteger import java.math.BigInteger
import java.security.MessageDigest import java.security.MessageDigest
@ -19,3 +24,50 @@ fun String.md5(): String {
return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0') return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0')
} }
@Target(AnnotationTarget.CLASS,AnnotationTarget.FIELD)
annotation class TableComment(val comment:String)
/**
* 表字段添加注释
*
* @param T
* @param tables
* @return
*/
fun <T : IntIdTable> updateComment(vararg tables:T){
transaction {
if (currentDialect is MysqlDialect) {
tables.forEach {
val tableClass = it::class.java
var statementList: List<String> = listOf<String>()
.run {
val comment = tableClass.getAnnotation(TableComment::class.java)?.comment ?: ""
plus("ALTER TABLE ${it.tableName} COMMENT '$comment'")
}
tableClass.declaredFields.filter { it.name != "INSTANCE" }
.forEach {
it.isAccessible = true
val column = it.get(null)
if (column is Column<*>) {
val ddl = column.ddl
if (ddl.size == 1) {
val comment = it.getAnnotation(TableComment::class.java)?.comment ?: ""
statementList = statementList.plus(
ddl[0].plus(" COMMENT '$comment'")
.replace("ADD", "MODIFY")
.replace("PRIMARY KEY", "")
)
}
}
}
statementList.forEach {
exec(it)
}
}
}
}
}

@ -62,17 +62,28 @@ class ApplicationTest {
} }
@Test
fun testUpdateComment(){
initApp {
// updateComment(Users,UserTokens)
transaction {
UserToken.new {
studentId = "6666"
token = "22"
ip = "sdf"
device = "hahha"
}
}
}
}
@Test @Test
fun testEntryId() { fun testEntryId() {
initApp { initApp {
transaction { transaction {
// User.new { val c=updateComment(UserTokens)
// studentId="20210101" println(c)
// name="222"
// password="12345678".md5()
// }
// alter table Users comment '123';
exec("alter table Users comment '6666'")
} }
} }
} }
@ -88,6 +99,7 @@ class ApplicationTest {
@Test @Test
fun documentGeneration() { fun documentGeneration() {
initApp { initApp {
val fileName="数据库设计文档"
//生成配置 //生成配置
val engineConfig = EngineConfig.builder() val engineConfig = EngineConfig.builder()
//生成文件路径 //生成文件路径
@ -95,11 +107,11 @@ class ApplicationTest {
//打开目录 //打开目录
.openOutputDir(true) .openOutputDir(true)
//文件类型 //文件类型
.fileType(EngineFileType.HTML) .fileType(EngineFileType.WORD)
//生成模板实现 //生成模板实现
.produceType(EngineTemplateType.freemarker) .produceType(EngineTemplateType.freemarker)
//自定义文件名称 //自定义文件名称
.fileName("777").build(); .fileName(fileName).build();
//忽略表 //忽略表
// val ignoreTableName = ArrayList<String>() // val ignoreTableName = ArrayList<String>()
@ -123,7 +135,7 @@ class ApplicationTest {
//配置 //配置
val config: Configuration = Configuration.builder() //版本 val config: Configuration = Configuration.builder() //版本
.version("1.0.0") //描述 .version("1.0.0") //描述
.description("数据库设计文档生成") //数据源 .description("${fileName}生成") //数据源
.dataSource(MySQL.getInstance(null).dataSource) //生成配置 .dataSource(MySQL.getInstance(null).dataSource) //生成配置
.engineConfig(engineConfig) //生成配置 .engineConfig(engineConfig) //生成配置
.produceConfig(processConfig) .produceConfig(processConfig)
@ -136,3 +148,4 @@ class ApplicationTest {
} }
} }

Loading…
Cancel
Save