import com.intellij.database.model.DasTable import com.intellij.database.model.ObjectKind import com.intellij.database.util.Case import com.intellij.database.util.DasUtil import com.intellij.psi.codeStyle.NameUtil import javax.swing.* /** * @author shirohoo * @link https://github.com/shirohoo/create-automation-jpa-entity * @param pakageName , primaryKey * * <pre> * * this script's default primary key strategy is @GeneratedValue(strategy = GenerationType.IDENTITY) * and specialized in Microsoft SQL Server * and finally implemented Serializable so recommend that create serial version UID * * first. enter your project package name. for example: * > com.intelliJ.psi * * second. enter primary key column name of target database table. * this script is convert input to camel case. for example 1: * > table primary key column name = MEMBER_ID * > enter primary key = memberId * * example 2: * > table primary key column name = ID * > enter primary key = id * * </pre> */ columnType = [ (~/(?i)bigint/) : "Long", (~/(?i)int/) : "Integer", (~/(?i)bit/) : "Boolean", (~/(?i)decimal/) : "BigDecimal", (~/(?i)float|double|real/) : "Double", (~/(?i)datetime|timestamp/): "LocalDateTime", (~/(?i)time/) : "LocalTime", (~/(?i)date/) : "LocalDate", (~/(?i)nvarchar/) : "nvarchar", (~/(?i)varchar/) : "varchar", (~/(?i)char/) : "String", (~/(?i)text/) : "String" ] def input = 23; JFrame jframe = new JFrame() String answer = JOptionPane.showInputDialog(jframe, it) jframe.dispose() answer 25; packageName = input("Enter your package name") primaryKey = input("Enter column name of primary key") FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") 23; dir -> SELECTION.filter 23; it instanceof DasTable && it.getKind() == ObjectKind.TABLE 25;.each 23; generate(it, dir) 25; 25; def generate(table, dir) 23; def tableName = table.getName() def className = convertFieldName(tableName + "Entity", true) def fields = categorizeFields(table) new File(dir, className + ".java").withPrintWriter 23; out -> generate(out, tableName, className, fields) 25; 25; def generate(out, tableName, className, fields) 23; out.println "package $packageName;" out.println "" out.println "import javax.persistence.*;" out.println "import java.time.LocalDateTime;" out.println "import lombok.Data;" out.println "" out.println "@Entity" out.println "@Data" out.println "@Table(name = \"$tableName\")" out.println "public class $className" out.println "{" fields.each() 23; if (it.annos != "") 23; out.println " ${it.annos}" 25; if (it.name == primaryKey) 23; out.println " @Id" out.println " @GeneratedValue(strategy = GenerationType.IDENTITY)" 25; if (it.type == 'nvarchar') 23; out.println " @Nationalized" out.println " @Column(name = \"${it. colName}\")" out.println " private String ${it.name};" 25; else if (it.type == 'varchar') 23; out.println " @Column(name = \"${it.colName}\")" out.println " private String ${it.name};" 25; else 23; out.println " @Column(name = \"${it.colName}\")" out.println " private ${it.type} ${it.name};" 25; out.println "" 25; out.println "}" 25; def categorizeFields(table) 23; DasUtil.getColumns(table).reduce([]) 23; fields, col -> def spec = Case.LOWER.apply(col.getDataType().getSpecification()) def typeStr = columnType.find 23; p, t -> p.matcher(spec).find() 25;.value fields += [[ colName: col.getName(), name : convertFieldName(col.getName(), false), type : typeStr, annos : ""]] 25; 25; def convertFieldName(str, capitalize) 23; def s = NameUtil.splitNameIntoWords(str) .collect 23; Case.LOWER.apply(it).capitalize() 25; .join("") .replaceAll(/[^\p23;javaJavaIdentifierPart25;[_]]/, "_") capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1] 25;