您好,登錄后才能下訂單哦!
本篇內容主要講解“idea怎么根據數據庫表自動生成JPA實體類”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“idea怎么根據數據庫表自動生成JPA實體類”吧!
在一些軟件開發過程模式下,可能會需要根據數據庫表生成對應的實體。在idea工具中如何做到這點呢?最簡單的答案可能是使用插件吧。其實還有一個很快捷的方法,步驟如下:
通過view->tool windows->database菜單,打開數據庫工具
連接數據庫
選定需要生成實體類的表,右鍵菜單選擇scripted extensions,有一個Generate POJOs.groovy
如此生成的實體類也許不能滿足你的需求,你可以自己寫一個groovy腳本來生成符合需求的實體類。在上面的第三步中,下面有一個go to scripts directory菜單,即可打開腳本目錄。在此目錄下新建一個腳本,比如Generate jpa Entity Object.groovy。
比如我創建的腳本如下
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怎么根據數據庫表自動生成JPA實體類”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。