好奇心驅使之下,研究了一下jfinal自動生成代碼模塊,因爲本人如今的項目中用到了sqlserver2012,因此當我用官網下載下來的demo生成的時候發現對sqlserver2012,仍是有一些bug的,如今貼出來,有什麼不對的,請大神們輕虐!!java
第一:我也是在查詢資料以後( 原創地址:http://www.oschina.net/question/2276614_2147333),重寫了一些代碼,因爲我只須要本身建立的表,因此添加用戶名「dbo」,這樣在循環遍歷的時候,集合中就能夠只放本身建立的表了
sql
gernerator.setMetaBuilder(new SqlserverMetaBuilder(getDataSource(),true,"dbo"));
第二:我把下面的代碼貼出來SqlserverMetaBuilder類數據庫
package com.cpa.generator; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.List; import javax.sql.DataSource; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.generator.ColumnMeta; import com.jfinal.plugin.activerecord.generator.MetaBuilder; import com.jfinal.plugin.activerecord.generator.TableMeta; public class SqlserverMetaBuilder extends MetaBuilder { // 是否將不帶前綴的表,加入的生成隊列 默認true,就是全部表都會自動生成 private boolean flag = true; //數據庫用戶名 private String user = ""; public SqlserverMetaBuilder(DataSource dataSource) { super(dataSource); // TODO Auto-generated constructor stub } public SqlserverMetaBuilder(DataSource dataSource, boolean flag,String user) { super(dataSource); this.flag = flag; this.user = user; } protected void buildTableNames(List<TableMeta> ret) throws SQLException { ResultSet rs = dbMeta.getTables(conn.getCatalog(), null, null, new String[] { "TABLE"}); while (rs.next()) { String tableName = ""; String tableSchem = rs.getString("TABLE_SCHEM"); if (user.equalsIgnoreCase(tableSchem)) { tableName = rs.getString("TABLE_NAME"); } if (excludedTables.contains(tableName)) { System.out.println("Skip excluded table :" + tableName); } else { TableMeta tableMeta = new TableMeta(); tableMeta.name = tableName; tableMeta.remarks = rs.getString("REMARKS"); boolean tempFlag = false; // 移除表名前綴僅用於生成 modelName、baseModelName。tableMeta.name 表名自身不受影響 if (removedTableNamePrefixes != null) { for (String prefix : removedTableNamePrefixes) { if (tableName.startsWith(prefix)) { tableName = tableName.replaceFirst(prefix, ""); tempFlag = true; break; } } } if ((flag || tempFlag)&&!tableName.isEmpty()) { tableMeta.modelName = StrKit.firstCharToUpperCase(StrKit .toCamelCase(tableName)); tableMeta.baseModelName = "Base" + tableMeta.modelName; ret.add(tableMeta); } } } rs.close(); } protected void buildColumnMetas(TableMeta tableMeta) throws SQLException { String sql = dialect.forTableBuilderDoBuild(tableMeta.name); sql = sql.replaceAll("`", ""); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData(); for (int i=1; i<=rsmd.getColumnCount(); i++) { ColumnMeta cm = new ColumnMeta(); cm.name = rsmd.getColumnName(i); String colClassName = rsmd.getColumnClassName(i); String typeStr = typeMapping.getType(colClassName); if (typeStr != null) { cm.javaType = typeStr; } else { int type = rsmd.getColumnType(i); if (type == Types.BINARY || type == Types.VARBINARY || type == Types.BLOB) { cm.javaType = "byte[]"; } else if (type == Types.CLOB || type == Types.NCLOB) { cm.javaType = "java.lang.String"; } else { cm.javaType = "java.lang.String"; } } // 構造字段對應的屬性名 attrName cm.attrName = buildAttrName(cm.name); tableMeta.columnMetas.add(cm); } rs.close(); stm.close(); } }
這裏我重寫了兩個方法buildTableNames和buildColumnMetasapp
重寫第一個方法是由於要獲取某個用戶下的表,這樣不會把系統表也建立出來;sqlserver
重寫第二個方法是由於不知道爲何String sql = dialect.forTableBuilderDoBuild(tableMeta.name);這個sql出來以後,是這個樣子的:select * from `表名` where 1 = 2,帶了兩個中文符號,致使後面查詢異常,因爲找不到緣由,我就把中文符號替換了,sql = sql.replaceAll("`", "");以解燃眉之急,ui
最後,大神們看見以後,有什麼好的想法,歡迎評論,我是小白,勿噴,輕虐!!!!
this