Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically generate JPA entity classes based on database tables by idea

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how idea automatically generates JPA entity classes according to database tables". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how idea automatically generates JPA entity classes according to database tables.

In some software development process patterns, it may be necessary to generate corresponding entities based on database tables. How do you do this in the idea tool? Perhaps the simplest answer is to use plug-ins. In fact, there is a very quick way, the steps are as follows:

From the view- > tool windows- > database menu, open the database tool

Connect to the database

Select the table that needs to generate the entity class, right-click the menu and select scripted extensions. There is a Generate POJOs.groovy.

The entity classes generated in this way may not meet your needs. You can write a groovy script to generate entity classes that meet your requirements. In the third step above, there is a go to scripts directory menu below to open the script directory. Create a new script in this directory, such as Generate jpa Entity Object.groovy.

For example, the script I created is as follows

Import com.intellij.database.model.DasTableimport com.intellij.database.util.Caseimport com.intellij.database.util.DasUtil/* * Available context bindings: * SELECTION Iterable * PROJECT project * FILES files helper * / packageName = "me.test.entity "typeMapping = [(~ / (?) bigint/):" Long ", (~ / (?) tinyint/):" Boolean ", (~ / (?) int/):" Integer ", (~ / (?) float | double | decimal | real/):" Double " (~ / (?) datetime | timestamp/): "java.sql.Timestamp", (~ / (?) date/): "java.sql.Date", (~ / (?) 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 it.comment *" out.println "\ t * ${isNotEmpty (it.comment)? It.comment: it.name} "out.println"\ tprivate / "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) .join {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 () 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 ('')} I believe you have a deeper understanding of "how idea automatically generates JPA entity classes according to database tables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report