IntelliJ IDEA 10 generate entity (POJO) from DB model

前端 未结 2 1186
感情败类
感情败类 2020-12-02 06:06

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.

2条回答
  •  庸人自扰
    2020-12-02 06:31

    The default Scripted Extensions Generate POJOs.groovy is not very good when dealing with tables with underscore(which is very common).

    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

提交回复
热议问题