您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“idea怎么根據(jù)數(shù)據(jù)庫(kù)表自動(dòng)生成JPA實(shí)體類”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“idea怎么根據(jù)數(shù)據(jù)庫(kù)表自動(dòng)生成JPA實(shí)體類”吧!
在一些軟件開發(fā)過(guò)程模式下,可能會(huì)需要根據(jù)數(shù)據(jù)庫(kù)表生成對(duì)應(yīng)的實(shí)體。在idea工具中如何做到這點(diǎn)呢?最簡(jiǎn)單的答案可能是使用插件吧。其實(shí)還有一個(gè)很快捷的方法,步驟如下:
通過(guò)view->tool windows->database菜單,打開數(shù)據(jù)庫(kù)工具
連接數(shù)據(jù)庫(kù)
選定需要生成實(shí)體類的表,右鍵菜單選擇scripted extensions,有一個(gè)Generate POJOs.groovy
如此生成的實(shí)體類也許不能滿足你的需求,你可以自己寫一個(gè)groovy腳本來(lái)生成符合需求的實(shí)體類。在上面的第三步中,下面有一個(gè)go to scripts directory菜單,即可打開腳本目錄。在此目錄下新建一個(gè)腳本,比如Generate jpa Entity Object.groovy。
比如我創(chuàng)建的腳本如下
import com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij.database.util.DasUtil /* * Available context bindings: * SELECTION Iterable<DasObject> * PROJECT project * FILES files helper */ packageName = "me.test.entity;" typeMapping = [ (~/(?i)bigint/) : "Long", (~/(?i)tinyint/) : "Boolean", (~/(?i)int/) : "Integer", (~/(?i)float|double|decimal|real/): "Double", (~/(?i)datetime|timestamp/) : "java.sql.Timestamp", (~/(?i)date/) : "java.sql.Date", (~/(?i)time/) : "java.sql.Time", (~/(?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) new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields, table) } } def generate(out, className, fields, table) { out.println "package $packageName" out.println "" out.println "import lombok.Data;" out.println "import javax.persistence.*;" out.println "" out.println "/**" out.println " * entity class for ${table.getName()}" if (isNotEmpty(table.getComment())) { out.println " * ${table.getComment()}" } out.println "*/" out.println "@Data" out.println "@Entity" out.println "@Table(name = \"${table.getName()}\")" out.println "public class $className {" out.println "" fields.each() { out.println "\t/**" out.println "\t* ${isNotEmpty(it.comment) ? it.comment : it.name}" out.println "\t*/" if (it.annos.size() > 0) it.annos.each() { out.println "\t${it}" } out.println "\tprivate ${it.type} ${it.name};" } 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 anos = []; if (Case.LOWER.apply(col.getName()).equals('id')) { anos += ["@Id", "@GeneratedValue(strategy = GenerationType.IDENTITY)"] } else { anos += ["@Column(name = \"${col.getName()}\")"] } def field = [ name : javaName(col.getName(), false), type : typeStr, comment: col.getComment(), annos: anos] fields += [field] } } 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 } 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('') } }
到此,相信大家對(duì)“idea怎么根據(jù)數(shù)據(jù)庫(kù)表自動(dòng)生成JPA實(shí)體類”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。