idea生成mysql表_IDEA根据数据库表结构生成Model类(POJO)

idea自动的脚本可以将一个数据的表结构自动生成模型类,变量名采用驼峰命名方式。下面是一个简单的系统参数表创建的sql:create table system_parameter

(

ID bigint auto_increment

comment '主键'

primary key,

SKEY varchar(32) not null

comment '键名',

SVALUE varchar(256) null

comment '键值',

REMARK varchar(256) null

comment '备注',

OPERATER varchar(20) null

comment '操作人',

CREATE_TIME datetime null

comment '创建时间',

UPDATE_TIME datetime null

comment '更新时间',

constraint UK_SYSTEM_PARAMETER

unique (SYS_KEY)

)

comment '系统参数表'

charset = utf8;

自动生成该表对应的dmo类,操作如下:

1.表名上右击 - Scripted Extensions - Generate POJOs.groovy

idea生成mysql表_IDEA根据数据库表结构生成Model类(POJO)_第1张图片

2. 然后选择保存的位置。

改进的Script

Idea自带的script已经很强大了,但是如果想把表中注释信息,自动注释到dmo类上面,就需要使用下面的script。import com.intellij.database.model.DasTable

import com.intellij.database.model.ObjectKind

import com.intellij.database.util.Case

import com.intellij.database.util.DasUtil

/*

* Available context bindings:

* SELECTION Iterable

* PROJECT project

* FILES files helper

*/

packageName = "com.suning.lcbpss.dao.dmo;"

typeMapping = [

(~/(?i)int/) : "Long",

(~/(?i)float|double|decimal|real/): "BigDecimal",

(~/(?i)datetime|timestamp/) : "Timestamp",

(~/(?i)date/) : "Date",

(~/(?i)time/) : "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 className = javaName(table.getName(), true)+"Dmo"

def fields = calcFields(table)

new File(dir, className + ".java").withPrintWriter('utf-8') { out -> generate(out, className, fields) }

}

def generate(out, className, fields) {

out.println "package $packageName"

out.println ""

out.println ""

Set types = new HashSet()

fields.each() {

types.add(it.type)

}

if (types.contains("Date")) {

out.println "import java.util.Date;"

}

if (types.contains("BigDecimal")) {

out.println "import java.math.BigDecimal;"

}

if (types.contains("Timestamp")) {

out.println "import java.sql.Timestamp;"

}

if (types.contains("Time")) {

out.println "import java.sql.Time;"

}

out.println ""

out.println ""

out.println "public class $className {"

out.println ""

fields.each() {

if (isNotEmpty(it.commoent)) {

out.println " /**"

out.println " * ${it.commoent}"

out.println " */"

}

if (it.annos != "") out.println " ${it.annos}"

out.println " private ${it.type} ${it.name};"

out.println ""

}

out.println ""

out.println ""

fields.each() {

if (isNotEmpty(it.commoent)) {

out.println " /**"

out.println " *"

out.println " * @return ${it.name}"

out.println " */"

}

out.println " public ${it.type} get${it.name.capitalize()}() {"

out.println " return ${it.name};"

out.println " }"

out.println ""

if (isNotEmpty(it.commoent)) {

out.println " /**"

out.println " *"

out.println " * @param ${it.name}"

out.println " */"

}

out.println " public void set${it.name.capitalize()}(${it.type} ${it.name}) {"

out.println " this.${it.name} = ${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

fields += [[

name : javaName(col.getName(), false),

type : typeStr,

commoent: col.getComment(),

annos: ""]]

}

}

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

}

添加脚本的方法如下:

1.在任意表名上右击 - Scripted Extensions - Go to Scripts Directory

idea生成mysql表_IDEA根据数据库表结构生成Model类(POJO)_第2张图片

2.然后在schema文件加下创建新的文件,将上面的脚本copy进去,每次生成dmo时,使用新的脚本即可。

你可能感兴趣的:(idea生成mysql表)