IDEA⾃带的代码⽣成脚本vy IDEA⾃带的代码⽣成脚本vy
1. 打开IDEA,连接到数据库
2. 脚本代码
成功连接数据库后,会出现下图所⽰的脚本⽂件(⽤来⾃动⽣成实体类):
3. ⽣成实体类
注意:此时⽣成的实体类是没有 Jpa 注解的4. ⾃定义(JPA)实体类⽣成脚本修改vy脚本⽂件
import com.del.DasTable
import com.del.ObjectKind
import com.del.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
SimpleDateFormat
/*
* Available context bindings:
*  SELECTION  Iterable<DasObject>
*  PROJECT    project
*  FILES      files helper
*/
packageName = ""
typeMapping = [
(~/(?i)tinyint|smallint|mediumint/)      : "Integer",
(~/(?i)int/)                            : "Long",
(~/(?i)bool|bit/)                        : "Boolean",
(~/(?i)float|double|decimal|real/)      : "BigDecimal",
(~/(?i)datetime|timestamp|date|time/)    : "Date",
(~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream",
(~/(?i)/)                                : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
get out ofSELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = Name(), true)
def fields = calcFields(table)
packageName = getPackageName(dir)
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "UTF-8"))  printWriter.withPrintWriter { out -> generate(out, className, fields, table) }
//    new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields,table) }
}
// 获取包所在⽂件夹路径
def getPackageName(dir) {
String().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println "import javax.persistence.Id;"
out.println "import javax.persistence.GeneratedValue;"
out.println "import java.io.Serializable;"
Set types = new HashSet()
fields.each() {
types.pe)
}
if (ains("Date")) {
out.println "import java.util.Date;"
}
if (ains("BigDecimal")) {
out.println "import java.math.BigDecimal;"
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。