mybatis-generator获取sqlServer字段注释并修改生成的实体类属性的注释

这两周一直在给mybatis—generator做扩展,首先附上github地址:[代码地址](https://github.com/whaiming/java-generator)
主要增加了以下功能:
1、增加了获取sqlServer数据库字段注释的功能;
2、扩展生成Bo;
3、结合freemarker和XML生成了简单的Service和domain;
4、增加了Mapper中的一些方法。

本篇文章主要是讲第一个功能的实现,希望能给大家带来一些帮助。开始的时候一直获取不到sqlServer的数据库表的字段注释内容,最后发现官网给的解释是JDBC不支持获取REMARKS,这样一来我只能自己改动代码了。首先改动DatabaseIntrospector类的getColumns()方法,增加了Connection参数。

private Map> getColumns(
            TableConfiguration tc,Connection conn) throws SQLException {
        /**
         * 数据库类型判断,如果是sqlServer则执行下面语句,做到对其他数据库的兼容
         */
        ResultSet sqlServerResultSet = null;
        boolean isSqlServer = conn.getMetaData().getDriverName().toUpperCase()
                .indexOf("SQL SERVER") != -1;
       if (isSqlServer) {
           //sqljdbc与sqljdbc4不同,sqlserver中间有空格
           String sql = "SELECT\n" +
                   "\tconvert(varchar(1000), C.\n" +
                   "VALUE)\n" +
                   "\tAS REMARKS\n" +
                   "FROM\n" +
                   "\tsys.tables A\n" +
                   "INNER JOIN sys.columns B ON B.object_id = A.object_id\n" +
                   "LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id\n" +
                   "AND C.minor_id = B.column_id\n" +
                   "WHERE\n" +
                   "\tA.name = ? ";
           PreparedStatement ps = conn.prepareStatement(sql);
           ps.setString(1, localTableName);
           sqlServerResultSet = ps.executeQuery();
       }
        while (rs.next()) {
            IntrospectedColumn introspectedColumn = ObjectFactory
                    .createIntrospectedColumn(context);
            if (isSqlServer){
                sqlServerResultSet.next();
                introspectedColumn.setRemarks(sqlServerResultSet.getString("REMARKS")); //$NON-NLS-1$
            }else {
                introspectedColumn.setRemarks(rs.getString("REMARKS")); //$NON-NLS-1$
            }
            introspectedColumn.setTableAlias(tc.getAlias());
            introspectedColumn.setJdbcType(rs.getInt("DATA_TYPE")); //$NON-NLS-1$
            introspectedColumn.setLength(rs.getInt("COLUMN_SIZE")); //$NON-NLS-1$
            introspectedColumn.setActualColumnName(rs.getString("COLUMN_NAME")); //$NON-NLS-1$
            introspectedColumn
                    .setNullable(rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable); //$NON-NLS-1$
            introspectedColumn.setScale(rs.getInt("DECIMAL_DIGITS")); //$NON-NLS-1$

            introspectedColumn.setDefaultValue(rs.getString("COLUMN_DEF")); //$NON-NLS-1$

            if (supportsIsAutoIncrement) {
                introspectedColumn.setAutoIncrement(
                        "YES".equals(rs.getString("IS_AUTOINCREMENT"))); //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (supportsIsGeneratedColumn) {
                introspectedColumn.setGeneratedColumn(
                        "YES".equals(rs.getString("IS_GENERATEDCOLUMN"))); //$NON-NLS-1$ //$NON-NLS-2$
            }

            ActualTableName atn = new ActualTableName(
                    rs.getString("TABLE_CAT"), //$NON-NLS-1$
                    rs.getString("TABLE_SCHEM"), //$NON-NLS-1$
                    rs.getString("TABLE_NAME")); //$NON-NLS-1$

            List columns = answer.get(atn);
            if (columns == null) {
                columns = new ArrayList();
                answer.put(atn, columns);
            }

            columns.add(introspectedColumn);

            if (logger.isDebugEnabled()) {
                logger.debug(getString(
                        "Tracing.2", //$NON-NLS-1$
                        introspectedColumn.getActualColumnName(), Integer
                                .toString(introspectedColumn.getJdbcType()),
                        atn.toString()));
            }
        }

在这个方法里直接对sqlServer数据库做了查询,然后将REMARKS赋值给introspectedColumn。最后在DefaultCommentGenerator类修改下addFieldComment方法,就可以生成带数据库字段注释的中文注释了。

public void addFieldComment(Field field,
                                IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();

        field.addJavaDocLine("/**"); //$NON-NLS-1$
        String remarks = introspectedColumn.getRemarks();
        if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
            //field.addJavaDocLine(" * Database Column Remarks:");
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));  //$NON-NLS-1$
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" * " + remarkLine);  //$NON-NLS-1$
            }
        }
        sb.append(" * 所属字段为: "); //$NON-NLS-1$
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append(".");
        sb.append(introspectedColumn.getActualColumnName());
        field.addJavaDocLine(sb.toString());

        addJavadocTag(field, false);

        field.addJavaDocLine(" */"); //$NON-NLS-1$
    }

总的来说就是这样了,如果有什么疑问也可以大家一起讨论学习。

你可能感兴趣的:(java工具类)