Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
根据Jetbrain IDE 的数据库查询工具得到的结果生成Java Bean
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
import com.intellij.database.datagrid.DataConsumer
import com.intellij.database.dialects.DatabaseDialect
import com.intellij.database.extensions.DataStream
import com.intellij.database.extensions.ValueFormatter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import java.lang.reflect.Field
import com.intellij.database.model.DasTable
import com.intellij.database.extensions.DataColumn
Project PROJECT = PROJECT;
DatabaseDialect DATABASE_DIALECT = DIALECT;
DasTable DATABASE_TABLE = TABLE;
List<? extends DataColumn> ALL_COLUMNS = ALL_COLUMNS;
List<? extends DataColumn> SELECTED_COLUMNS = COLUMNS;
ValueFormatter FORMATTER = FORMATTER;
Appendable OUTPUT = OUT;
DataStream INPUT = ROWS;
Boolean TRANSPOSED = TRANSPOSED;
def typeMap = new HashMap<String, String>()
typeMap.put("CHAR", "String")
typeMap.put("VARCHAR", "String")
typeMap.put("LONGVARCHAR", "String")
typeMap.put("NUMERIC", "long")
typeMap.put("DECIMAL", "long")
typeMap.put("BIT", "boolean")
typeMap.put("TINYINT", "long")
typeMap.put("SMALLINT", "long")
typeMap.put("INTEGER", "long")
typeMap.put("INT", "long")
typeMap.put("BIGINT", "long")
typeMap.put("REAL", "double")
typeMap.put("FLOAT", "double")
typeMap.put("DOUBLE", "double")
typeMap.put("DATE", "java.util.Date")
typeMap.put("DATETIME", "java.util.Date")
def builder = new StringBuilder()
builder.append("@lombok.Data\n")
builder.append("@lombok.NoArgsConstructor\n")
builder.append("@lombok.AllArgsConstructor\n")
builder.append("public class Demo {")
builder.append("\n")
for (DataColumn column : ALL_COLUMNS) {
Field f = column.getClass().getDeclaredField("myColumn");
f.setAccessible(true);
DataConsumer.Column col = f.get(column)
builder.append("\t")
builder.append("private ")
builder.append(typeMap.getOrDefault(col.getTypeName(), col.getTypeName()) + " ")
builder.append(getJavaName(col.getName()) + " ")
builder.append(";")
builder.append("\n")
}
private String getJavaName(String name) {
def charArray = name.toCharArray()
def sb = new StringBuilder()
boolean flag = false
for (char c : charArray) {
if (Character.isLetterOrDigit(c)) {
if (flag) {
sb.append(Character.toUpperCase(c))
} else {
sb.append(c)
}
flag = false
} else {
flag = true
}
}
return sb.toString()
}
builder.append("}")
OUTPUT.append(builder.toString())
@lihongjie0209
Copy link
Author

lihongjie0209 commented Dec 4, 2019

使用方法, 在Jetbrain IDE中选择数据库工具,然后执行一条SQL语句, 在查询结果中选择
image
然后把这个文件复制到相应的文件夹
image

导出选项中就会多出自定义的导出脚本

image

选择之后点击保存到剪贴板或者是文件

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment