步骤1:打开Database窗格,新建数据库连接
数据库连接默认只是当前工程使用,想要所有IDEA窗口共享
步骤2:编辑groovy脚本
步骤3:选择一张或多张表,生成代码
生成效果
附:groovy脚本
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
config = [
impSerializable : true,
extendBaseEntity : false,
extendBaseService: true,
useLombok : true, // 使用注解,不生成get、set方法
ModelNotNULL : true, // 空值不返回注解
ModelDate : true, // 日期类格式注解
baseMethods : true, // ServiceImpl生成基础方法
generateItem : [
"Entity",
"Service",
"ServiceImpl",
"Repository",
"RepositoryCustom",
"RepositoryImpl",
"Controller",
"SOA",
"Constants",
"Config",
]
]
baseEntityProperties = ["id", "CreatedOn", "LastChangedOn", "version"]
typeMapping = [
(~/(?i)bool|boolean|tinyint/) : "Boolean",
(~/(?i)bigint/) : "Long",
(~/int/) : "Integer",
(~/(?i)float|double|decimal|real|numeric/): "java.math.BigDecimal",
(~/(?i)datetime|timestamp/) : "java.util.Date",
(~/(?i)date/) : "java.util.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter {
it instanceof DasTable && it.getKind() == ObjectKind.TABLE
}.each {
generate(it, dir)
}
}
// 生成对应的文件
def generate(table, dir) {
def entityPath = "${dir.toString()}\\entity",
servicePath = "${dir.toString()}\\service",
serviceImplPath = "${dir.toString()}\\service\\impl",
repPath = "${dir.toString()}\\dao",
controllerPath = "${dir.toString()}\\controller",
constantsPath = "${dir.toString()}\\constants",
configPath = "${dir.toString()}\\config"
mkdirs([entityPath, servicePath, serviceImplPath, repPath, controllerPath, constantsPath, configPath])
System.out.println(table.getName())
def entityName = javaName(table.getName(), true)
def fields = calcFields(table)
def basePackage = clacBasePackage(dir)
if (isGenerate("Entity")) {
genUTF8FileEntity(entityPath, "${entityName}.java").withPrintWriter { out -> genEntity(out, table, entityName, fields, basePackage) }
}
if (isGenerate("Service")) {
// genUTF8FileOtherEntity(servicePath, "${entityName}Service.java").withPrintWriter { out -> genService(out, table, entityName, fields, basePackage) }
}
if (isGenerate("ServiceImpl")) {
// genUTF8FileOtherEntity(serviceImplPath, "${entityName}ServiceImpl.java").withPrintWriter { out -> genServiceImpl(out, table, entityName, fields, basePackage) }
}
if (isGenerate("Repository")) {
genUTF8FileOtherEntity(repPath, "${entityName}Repository.java").withPrintWriter { out -> genRepository(out, table, entityName, fields, basePackage) }
}
if (isGenerate("Controller")) {
//genUTF8FileOtherEntity(controllerPath, "${entityName}Controller.java").withPrintWriter { out -> genController(out, table, entityName, fields, basePackage) }
}
if (isGenerate("Constants")) {
// genUTF8FileOtherEntity(controllerPath, "${entityName}Controller.java").withPrintWriter { out -> genConstants(out, table, entityName, fields, basePackage) }
}
if (isGenerate("Config")) {
//genUTF8FileOtherEntity(controllerPath, "${entityName}Controller.java").withPrintWriter { out -> genSOA(out, table, entityName, fields, basePackage) }
}
}
// 是否需要被生成
def isGenerate(itemName) {
config.generateItem.contains(itemName)
}
// 指定文件编码方式,防止中文注释乱码
def genUTF8FileEntity(dir, fileName) {
new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, fileName)), "utf-8"))
}
def genUTF8FileOtherEntity(dir, fileName) {
new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, fileName)), "utf-8"))
}
// 生成每个字段
def genProperty(out, field) {
out.println ""
if (field.comment != null || field.default != null) {
out.println " /**"
if (field.comment != null) {
out.println " * ${field.comment}"
}
if (field.default != null) {
out.println " * Default Values: ${field.default}"
}
out.println " */"
}
// 默认表的第一个字段为主键
if (field.name == "id") {
// 不继承父类
out.println " @Id"
//out.println " @GeneratedValue(generator =\"uuid\")"
//out.println " @GenericGenerator(name = \"uuid\", strategy = \"uuid2\")"
}
if (field.colum == "CreatedOn") {
out.println " @Temporal(TemporalType.TIMESTAMP)"
out.println " @org.hibernate.annotations.CreationTimestamp"
}
if (field.colum == "LastChangedOn") {
out.println " @Temporal(TemporalType.TIMESTAMP)"
out.println " @org.hibernate.annotations.UpdateTimestamp"
}
if (field.colum == "Version") {
out.println " @Temporal(TemporalType.TIMESTAMP)"
out.println " @org.hibernate.annotations.UpdateTimestamp"
}
// 日期类加上注解
if (field.dataType == "datetime" || field.dataType == "timestamp") {
if (config.ModelDate) {
out.println " @JsonFormat(pattern=\"yyyy-MM-dd HH:mm:ss\")"
}
}
if (field.type == "String") {
if (field.dataType == "varchar") {
// 枚举不需要长度
out.println " @Column(name = \"${field.colum}\", nullable = ${!field.isNotNull}${field.dataType == "enum" ? "" : field.dataType == "datetime" || field.dataType == "timestamp" ? "" : ", length = $field.len"})"
} else if (field.dataType == "varchar2") {
// 枚举不需要长度
def varchen = field.varlen.replaceAll("VARCHAR2", "").replaceAll(/[^\d]/, "");
out.println " @Column(name = \"${field.colum}\", nullable = ${!field.isNotNull}${field.dataType == "enum" ? "" : field.dataType == "datetime" || field.dataType == "timestamp" ? "" : ", length = $varchen"})"
} else {
// 枚举不需要长度
out.println " @Column(name = \"${field.colum}\", nullable = ${!field.isNotNull}${field.dataType == "enum" ? "" : field.dataType == "datetime" || field.dataType == "timestamp" ? "" : ", length = $field.len"})"
}
} else {
out.println " @Column(name = \"${field.colum}\", nullable = ${!field.isNotNull})"
}
out.println " private ${field.type} ${field.name};"
}
// 生成get、get方法
def genGetSet(out, field) {
// get
out.println " "
out.println " public ${field.type} get${field.name.substring(0, 1).toUpperCase()}${field.name.substring(1)}() {"
out.println " return this.${field.name};"
out.println " }"
// set
out.println " "
out.println " public void set${field.name.substring(0, 1).toUpperCase()}${field.name.substring(1)}(${field.type} ${field.name}) {"
out.println " this.${field.name} = ${field.name};"
out.println " }"
}
// 生成实体类
def genEntity(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.entity;"
out.println ""
if (config.extendBaseEntity) {
out.println "import $baseEntityPackage;"
}
if (config.useLombok) {
out.println "import lombok.Data;"
out.println ""
}
if (config.impSerializable) {
out.println "import java.io.Serializable;"
out.println ""
}
out.println "import javax.persistence.*;"
if (config.ModelNotNULL) {
out.println "import com.fasterxml.jackson.annotation.JsonInclude;"
}
if (config.ModelDate) {
out.println "import com.fasterxml.jackson.annotation.JsonFormat;"
}
out.println "import org.hibernate.annotations.GenericGenerator;"
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${table.getName()}表Entity类"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
if (config.useLombok) {
out.println "@Data"
}
if (config.ModelNotNULL) {
out.println "@JsonInclude(JsonInclude.Include.NON_NULL)"
}
out.println "@Entity"
out.println "@Table(name = \"${table.getName()}\")"
out.println "public class $entityName${config.extendBaseEntity ? " extends BaseEntity" : ""}${config.impSerializable ? " implements Serializable" : ""} {"
if (config.extendBaseEntity) {
fields = fields.findAll { it ->
!baseEntityProperties.any { it1 -> it1 == it.name }
}
}
fields.each() {
genProperty(out, it)
}
if (!config.useLombok) {
fields.each() {
genGetSet(out, it)
}
}
out.println "}"
}
// 生成Service
def genService(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.service;"
out.println ""
// if (config.extendBaseService) {
// out.println "import $baseServicePackage;"
out.println "import ${basePackage}.entity.$entityName;"
// }
out.println ""
out.println ""
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${entityName} Service"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "@Repository"
out.println "public interface ${entityName}Service {"
out.println ""
out.println ""
out.println "}"
}
// 生成ServiceImpl
def genServiceImpl(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.service.impl;"
out.println ""
out.println "import ${basePackage}.repository.${entityName}Repository;"
out.println "import ${basePackage}.service.${entityName}Service;"
out.println "import ${basePackage}.entity.$entityName;"
if (config.baseMethods) {
out.println "import java.util.List;"
out.println "import org.springframework.transaction.annotation.Transactional;"
out.println "import org.springframework.data.domain.*;"
out.println "import java.util.Optional;"
}
out.println "import org.springframework.stereotype.Service;"
out.println "import java.util.Optional;"
out.println "import javax.annotation.Resource;"
out.println ""
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${entityName}Service实现类"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "@Service"
out.println "public class ${entityName}ServiceImpl implements ${entityName}Service {"
out.println ""
out.println " @Resource"
out.println " private ${entityName}Repository rep;"
out.println ""
out.println ""
out.println "}"
}
// 生成Repository
def genRepository(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}Repository.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.dao;"
out.println ""
out.println "import ${basePackage}.entity.$entityName;"
out.println "import org.springframework.data.jpa.repository.JpaRepository;"
out.println "import org.springframework.stereotype.Repository;"
out.println ""
out.println "/** "
out.println " * @ClassName: ${entityName}Repository"
out.println " * @Description: ${entityName} Repository"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "@Repository"
out.println "public interface ${entityName}Repository extends JpaRepository<$entityName, String> {}"
out.println ""
}
// 生成RepositoryCustom
def genRepositoryCustom(out, entityName, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.dao;"
out.println ""
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${entityName} RepositoryCustom"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "public interface ${entityName}RepositoryCustom {"
out.println ""
out.println "}"
}
// 生成RepositoryImpl
def genRepositoryImpl(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.repository.impl;"
out.println ""
out.println "import ${basePackage}.repository.${entityName}RepositoryCustom;"
out.println "import org.springframework.stereotype.Repository;"
out.println ""
out.println "import javax.persistence.EntityManager;"
out.println "import javax.persistence.PersistenceContext;"
out.println ""
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${entityName}RepositoryCustom实现类"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "@Repository"
out.println "public class ${entityName}RepositoryImpl implements ${entityName}RepositoryCustom {"
out.println ""
out.println " @PersistenceContext"
out.println " private EntityManager em;"
out.println "}"
}
// 生成Controller
def genController(out, table, entityName, fields, basePackage) {
out.println "/**"
out.println " * 文件名: ${entityName}.java"
out.println " * 版权: Copyright 2017-2023 Xxx All Rights Reserved."
out.println " * 描述: Xxx系统"
out.println " */"
out.println "package ${basePackage}.controller;"
out.println ""
out.println " import org.springframework.web.bind.annotation.*; "
out.println ""
out.println "/** "
out.println " * @ClassName: $entityName"
out.println " * @Description: ${entityName} Controller"
out.println " * @author: wsdhla"
out.println " * @date: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")).toString() + ""
out.println " */"
out.println "@RestController"
out.println "@RequestMapping(\"/\")"
out.println "public class ${entityName}Controller {"
out.println ""
out.println "}"
}
// 生成文件夹
def mkdirs(dirs) {
dirs.forEach {
def f = new File(it)
if (!f.exists()) {
f.mkdirs()
}
}
}
def clacBasePackage(dir) {
dir.toString()
.replaceAll("^.+\\\\src\\\\main\\\\java\\\\", "")
.replaceAll("\\\\", ".")
}
def isBaseEntityProperty(property) {
baseEntityProperties.find { it == property } != null
}
// 转换类型
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
fields += [[
name : javaName(col.getName(), false),
colum : col.getName(),
type : typeStr,
dataType : col.getDataType().toString().replaceAll(/\(.*\)/, "").toLowerCase(),
len : col.getDataType().toString().replaceAll(/[^\d]/, ""),
default : col.getDefault(),
comment : col.getComment(),
isNotNull: col.isNotNull(),
position : col.getPosition(),
varlen : col.getDataType().toString(),
]]
}
}
def javaName(str, capitalize) {
def s = str.split(/(?<=[^\p{IsLetter}])/).collect { Case.LOWER.apply(it).capitalize() }
.join("").replaceAll(/[^\p{javaJavaIdentifierPart}]/, "_").replaceAll(/_/, "")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}