首页 / IDEA / Idea导出数据为POJO
Idea导出数据为POJO
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Idea导出数据为POJO,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2911字,纯文字阅读大概需要5分钟。
内容图文
`import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
/*
- Available context bindings:
- SELECTION Iterable
- PROJECT project
- FILES files helper
*/
packageName = ""
typeMapping = [
(~/(?i)int/) : "int",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)bool|bit/) : "Boolean",
(~/(?i)datetime|timestamp/) : "java.util.Date",
(~/(?i)date/) : "java.util.Date",
(~/(?i)time/) : "java.util.Date",
(~/(?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 fields = calcFields(table)
packageName = getPackageName(dir)
new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) }
}
// 获取包所在文件夹路径
def getPackageName(dir) {
return dir.toString().replaceAll("\\", ".").replaceAll("/", ".").replaceAll("^.*src(\.main\.java\.)?", "") + ";"
}
def generate(out, className, fields) {
out.println "package $packageName"
out.println "import org.hibernate.annotations.GenericGenerator;"
out.println "import javax.persistence.*;"
out.println "import java.util.Date;"
out.println ""
out.println ""
out.println "/**\n" +
" * description \n" +
" * create by zhangqiang\n" +
" * create time "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " \n" +
" */"
out.println "@Table"
out.println "@Entity"
out.println "public class $className {"
out.println ""
fields.each() {
if (it.name =="id"){
out.println " @Id"
out.println " @GeneratedValue(generator = "uuid")"
out.println " @GenericGenerator(name = "uuid", strategy = "uuid2")"
out.println " @Column(columnDefinition = "char(36) not null")"
}
if (it.annos != "") out.println " ${it.annos}"
out.println " private ${it.type} ${it.name};"
}
out.println ""
fields.each() {
out.println ""
out.println " public ${it.type} get${it.name.capitalize()}() {"
out.println " return ${it.name};"
out.println " }"
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,
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]
}
`
原文:https://www.cnblogs.com/larryzq/p/13385145.html
内容总结
以上是互联网集市为您收集整理的Idea导出数据为POJO全部内容,希望文章能够帮你解决Idea导出数据为POJO所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。