idea连接数据库⽣成对应实体类(有字段注释)1.到idea 数据库⼯具
2.在右侧到⼯具栏
3.添加数据库连接,⽤户名,密码
4.在表上右键选择
5.创建脚本,⽤代码,写代码
6.上代码,将代码放进之前创建的⽂件中
import com.del.DasTable
import com.del.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.io.*
SimpleDateFormat
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = ""
typeMapping = [
(~/(?i)tinyint|smallint|mediumint/) : "Integer",
(~/(?i)int/) : "Integer",
(~/(?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 ->
SELECTION.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 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 java.io.Serializable;"
out.println "import lombok.Data;"
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;"
}
if (ains("InputStream")) {
out.println "import java.io.InputStream;"
}
out.println ""
out.println "/**\n" +
" * @Description ${table}\n" +
" * @Author Sunk \n" +
" * @Date "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " \n" +
" */"
out.println ""
out.println "@Data"
out.println "public class $className implements Serializable {"
out.println ""
out.println genSerialID()
fields.each() {
replaceall()out.println ""
// 输出注释
if (isNotEmpty(itmoent)) out.println "\t/** ${itmoent} */"
if (it.annos != "") out.println " ${place("[@Id]", "")}"
// 输出成员变量
out.println "\tprivate ${it.type} ${it.name};"
}
out.println ""
out.println "}"
}
def calcFields(table) {
def spec = Case.LOWER.DataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
def comm =[
colName : Name(),
name : Name(), false),
type : typeStr,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论