database IntelliJ IDEA 10 从 DB 模型生成实体 (POJO)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5259276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ IDEA 10 generate entity (POJO) from DB model
提问by pierre
How can I generate entity (POJO) from database model using IntelliJ IDEA 10. I create "Data source" in IntelliJ but I have not any idea how can I generate the POJO.
如何使用 IntelliJ IDEA 10 从数据库模型生成实体 (POJO)。我在 IntelliJ 中创建了“数据源”,但我不知道如何生成 POJO。
回答by zpontikas
UPDATE:
In IntelliJ 16 this feature in now implemented.
The steps to do it are:
1. Databaseview context menu
2. Scripted Extensions
3. Generate POJOs
更新:
在 IntelliJ 16 中,此功能现已实现。执行此操作的步骤是:
1.数据库视图上下文菜单
2. 脚本化扩展
3. 生成 POJO
You can read more here:
Feature request: allow "generate classes from database schema" for plain-JDBC developers
您可以在此处阅读更多信息:
功能请求:允许普通 JDBC 开发人员“从数据库模式生成类”
Note:The following information is about version 15 and earlier:
注意:以下信息是关于 15 及更早版本的:
First, you need to tell IntelliJ that you are using Hibernate (I guess you are if you need the orm POJO of the table)
首先,您需要告诉IntelliJ您正在使用Hibernate(如果您需要表的orm POJO,我猜您是)
- Go to "Project structure" (alt+ctrl+shift+s)
- In "Project settings" select "Modules"
- Press +and add the Hibernate facet in your module.
- 转到“项目结构”(alt+ ctrl+ shift+ s)
- 在“项目设置”中选择“模块”
- +在您的模块中按下并添加 Hibernate facet。
Now you have set up your hibernate configuration facet you can extract your POJOs.
现在您已经设置了您的休眠配置方面,您可以提取您的 POJO。
- At your bottom right horizontal panel, you will now see a tab called "Persistence" (ιf you can't find Persistence tab you may show it by choosing View > Tool Windows > Persistence)
- There you can right-click on the hibernate icon named like your module
- Go to "Generate Persistence Mapping"-"by database schema"
- Now I guess you can find your way...
- In general, settings select the datasource that you want to use and now you can see all the tables in your datasource object
- Now you can do many things, add relationships with the + sign, change the name and type of the POJO's properties etc. note: if you get an error and the "OK" is disabled its probably because the data type that IntelliJ found for your POJO is invalid. Just change it to the one you need and you are ready to go!
- 在右下角的水平面板中,您现在将看到一个名为“Persistence”的选项卡(如果您找不到 Persistence 选项卡,您可以通过选择 View > Tool Windows > Persistence 来显示它)
- 在那里你可以右键单击像你的模块一样命名的休眠图标
- 转到“生成持久性映射”-“按数据库模式”
- 现在我想你可以找到你的路了......
- 通常,设置选择您要使用的数据源,现在您可以看到数据源对象中的所有表
- 现在您可以做很多事情,添加与 + 号的关系,更改 POJO 属性的名称和类型等。注意:如果您收到错误并且“OK”被禁用,这可能是因为 IntelliJ 为您找到的数据类型POJO 无效。只需将其更改为您需要的那个,您就可以开始使用了!
回答by aristotll
The default Scripted ExtensionsGenerate POJOs.groovyis not very good when dealing with tables with underscore(which is very common).
在处理带有下划线的表时(这很常见),默认的脚本扩展Generate POJOs.groovy不是很好。
So I make some modifications.
所以我做了一些修改。
The main code
主要代码
def calcFields(DasObject table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.dataType.specification)
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
fields += [[
name : javaName(col.name, false),
type : typeStr,
annos: """
/**
* $col.comment
*/"""]]
}
}
static String javaName(String str, boolean capitalize) {
def s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str);
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
You can find the whole gist here https://gist.github.com/aristotll/ad799a7462e8b705b26103944cca24a6
你可以在这里找到整个要点https://gist.github.com/aristotll/ad799a7462e8b705b26103944cca24a6

