本篇內(nèi)容主要講解“idea怎么根據(jù)數(shù)據(jù)庫表自動生成JPA實體類”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“idea怎么根據(jù)數(shù)據(jù)庫表自動生成JPA實體類”吧!
創(chuàng)新互聯(lián)公司是一家專業(yè)提供全椒企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計制作、做網(wǎng)站、HTML5建站、小程序制作等業(yè)務(wù)。10年已為全椒眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
在一些軟件開發(fā)過程模式下,可能會需要根據(jù)數(shù)據(jù)庫表生成對應(yīng)的實體。在idea工具中如何做到這點呢?最簡單的答案可能是使用插件吧。其實還有一個很快捷的方法,步驟如下:
通過view->tool windows->database菜單,打開數(shù)據(jù)庫工具
連接數(shù)據(jù)庫
選定需要生成實體類的表,右鍵菜單選擇scripted extensions,有一個Generate POJOs.groovy
如此生成的實體類也許不能滿足你的需求,你可以自己寫一個groovy腳本來生成符合需求的實體類。在上面的第三步中,下面有一個go to scripts directory菜單,即可打開腳本目錄。在此目錄下新建一個腳本,比如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('') } }
到此,相信大家對“idea怎么根據(jù)數(shù)據(jù)庫表自動生成JPA實體類”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文題目:idea怎么根據(jù)數(shù)據(jù)庫表自動生成JPA實體類
標(biāo)題路徑:http://muchs.cn/article42/joghec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、Google、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)