idea数据库自动生产实体类
2021/6/3 2:27:48
本文主要是介绍idea数据库自动生产实体类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
/*
* Available context bindings:
* SELECTION Iterable
packageName = "com.hongkun.bean.hkt;"
typeMapping = [
(~/(?i)int/) : "Integer",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp|date/) : "Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def classConment = table.getComment()
def fields = calcFields(table)
def dateStr = new Date().toLocaleString();
new File(dir, className + ".java").withPrintWriter("UTF-8") { out -> generate(out, className, classConment, fields,dateStr) }
}
def generate(out, className, classConment, fields,dateStr) {
out.println "package $packageName"
out.println ""
out.println "import io.swagger.annotations.ApiModel;"
out.println "import io.swagger.annotations.ApiModelProperty;"
out.println "import lombok.Data;"
out.println ""
out.println "import java.io.Serializable;"
out.println "import java.util.Date;"
out.println ""
out.println "/**"
out.println " * Description: ${classConment}"
out.println " *"
out.println " * @author fengxiaoyang"
out.println " * @date ${dateStr}"
out.println " */"
out.println "@Data"
out.println "@ApiModel(\"${classConment}\")"
out.println "public class $className implements Serializable {"
out.println ""
// out.println "\tprivate static final long serialVersionUID = " + Math.abs(new Random().nextLong()) + "L;"
// out.println ""
fields.each() {
// if (isNotEmpty(it.comment)) out.println "\t/** ${it.comment} */"
if (it.annos != "") out.println " ${it.annos}"
out.println "\tprivate ${it.type} ${it.name};"
out.println ""
}
out.println ""
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
def comment = col.getComment()
fields += [[
name : fieldName(col.getName(), false),
type : typeStr,
comment: comment,
annos : "\t@ApiModelProperty(value = \"" + comment + "\" )"]]
}
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
/**
* 我的字段会给字典字段和外键字段增加标识头,这里转属性时去掉
* @param str
* @param capitalize
* @return
*/
def fieldName(str, capitalize) {
//去除开头的标识
if (str.startsWith("dict_")) str = str.substring(5)
if (str.startsWith("fk_")) str = str.substring(3)
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
这篇关于idea数据库自动生产实体类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略