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.sql.Date
packageName = "需要生成到的地方;"
typeMapping = [
(~/(?i)tinyint|smallint|mediumint/) : "Integer",
(~/(?i)int/) : "Integer",
(~/(?i)bool|bit/) : "Boolean",
(~/(?i)float|double|decimal|real/) : "Double",
(~/(?i)datetime|timestamp|date|time/) : "Date",
(~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream",
(~/(?i)number/) : "Integer",
(~/(?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 className = javaClassName(table.getName(), true)
def fields = calcFields(table)
def targetClassName = className.substring(1, className.size())
new File(dir, targetClassName + ".java").withPrintWriter { out -> generate(out, targetClassName, fields, table) }
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import javax.persistence.*;"
out.println "import java.io.Serializable;"
Set types = new HashSet()
fields.each() {
types.add(it.type)
}
if (types.contains("Date")) {
out.println "import java.util.Date;"
out.println "import org.springframework.data.annotation.LastModifiedDate;"
out.println "import org.springframework.data.annotation.CreatedDate;"
}
if (types.contains("InputStream")) {
out.println "import java.io.InputStream;"
}
out.println ""
out.println "@Entity"
out.println "@Table ( name =\"" + table.getName() + "\" )"
out.println "public class $className implements Serializable {"
out.println ""
fields.each() {
out.println ""
// 输出注释 暂时不输出注释了
// if (isNotEmpty(it.commoent)) {
// out.println "\t/**${it.commoent}*/"
// }
if (it.annos != "") out.println " ${it.annos}"
// 输出成员变量
out.println "\tprivate ${it.type} ${it.name};"
}
out.println ""
// set get 方法
fields.each() {
out.println ""
out.println "\tpublic ${it.type} get${it.name.capitalize()}() {"
out.println "\t\treturn ${it.name};"
out.println "\t}"
out.println ""
out.println "\tpublic void set${it.name.capitalize()}(${it.type} ${it.name}) {"
out.println "\t\tthis.${it.name} = ${it.name};"
out.println "\t}"
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 comm = [
colName : col.getName(),
name : changeStyle(javaName(col.getName(), false), true),
type : typeStr,
commoent: col.getComment(),
annos : "\t@Column(name = \"" + col.getName() + "\" )"]
if ("id".equals(Case.LOWER.apply(col.getName()))){
comm.annos = "\t@Id"
}
if ("create_time".equals(Case.LOWER.apply(col.getName()))){
comm.annos += "\n\t@CreatedDate"
}
if ("update_time".equals(Case.LOWER.apply(col.getName()))){
comm.annos += "\n\t@LastModifiedDate"
}
fields += [comm]
}
}
// 处理类名
def javaClassName(str, capitalize) {
def s = str.split(/[^\p{Alnum}]/).collect { def s = Case.LOWER.apply(it).capitalize() }.join("")
s = s[1..s.size() - 1]
capitalize ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
def javaName(str, capitalize) {
def s = str.split(/(?<=[^\p{IsLetter}])/).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
}
static String changeStyle(String str, boolean toCamel) {
if (!str || str.size() <= 1)
return str
if (toCamel) {
String r = str.toLowerCase().split('_').collect { cc -> Case.LOWER.apply(cc).capitalize() }.join('')
return r[0].toLowerCase() + r[1..-1]
} else {
str = str[0].toLowerCase() + str[1..-1]
return str.collect { cc -> ((char) cc).isUpperCase() ? '_' + cc.toLowerCase() : cc }.join('')
}
}
评论
0/240
- 拉大锯用Mybatis-Plush可以使用代码生成器,这个之前有提供脚本,好像有点问题,哈哈。2020-11-13 20:33 回复
推荐文章
2024-12-12 16:48 1