package com.j2ee.framework.ibatis; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.j2ee.framework.domain.Right; /** * 根据javaBean或者数据库字段自动生成xml * * @author Tangchao * */ public class IbatisByJavaBenOrTableColumnToXml { private List<Class<?>> classz; private Map<String, String[]> tableNameToColumns = new HashMap<String, String[]>(); public static String DATABASE_MYSQL = "mysql"; public static String DATABASE_ORACLE = "oracle"; public IbatisByJavaBenOrTableColumnToXml(List<Class<?>> classz) { this.classz = classz; } public IbatisByJavaBenOrTableColumnToXml(Connection conn, String[] tableNames, String dataBase) throws SQLException { Statement st = conn.createStatement(); for (String table : tableNames) { String sql = "select * from " + table + (DATABASE_ORACLE.equals(dataBase) == true ? " where rownum=1" : "limit 0,1"); ResultSet rs = st.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData(); tableNameToColumns.put(table, new String[rsmd.getColumnCount()]); for (int i = 1; i <= rsmd.getColumnCount(); i++) { tableNameToColumns.get(table)[i - 1] = rsmd.getColumnName(i); } rs.close(); } if (null != conn) { conn.close(); } } public String getXml() { String xml = getResultMap() + getInsert() + getSelect() + getUpdate() + getDelete(); return xml; } public String getfields() { StringBuffer sb = new StringBuffer(); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { String[] fields = tableNameToColumns.get(table); sb.append(table).append(":{"); for (String field : fields) { sb.append(field).append(","); } sb.deleteCharAt(sb.length() - 1); sb.append("}\n"); } } else { for (Class<?> class1 : classz) { Field[] fields = class1.getDeclaredFields(); sb.append(class1.getSimpleName()).append(":{"); for (Field field : fields) { sb.append(field.getName()).append(","); } sb.deleteCharAt(sb.length() - 1); sb.append("}\n"); } } System.out.println(sb.toString()); return sb.toString(); } private String getProperty(String field) { if (field.indexOf("_") > 0) { String property = field.toLowerCase(); StringBuffer newProperty = new StringBuffer(); int lastIndex = -1; for (int i = 0; i < property.length(); i++) { if (property.charAt(i) == '_') { lastIndex = i + 1; continue; } newProperty.append(lastIndex == i ? Character.toUpperCase(property.charAt(i)) : property.charAt(i)); } return newProperty.toString(); } else { return field.toLowerCase(); } } public String getResultMap() { StringBuffer sb = new StringBuffer(); sb.append("<!-- 映射 -->\n"); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { String[] fields = tableNameToColumns.get(table); sb.append("<resultMap id=\"").append(table).append(".resultMap\"").append(" class=\"").append(table) .append("\"> \n"); for (String field : fields) { sb.append("<result property=\"").append(getProperty(field)).append("\" column=\"").append(field) .append("\" />\n"); } sb.append("</resultMap>\n"); } } else { for (Class<?> class1 : classz) { Field[] fields = class1.getDeclaredFields(); sb.append("<resultMap id=\"").append(class1.getSimpleName()).append(".resultMap\"").append(" class=\"") .append(class1.getName()).append("\"> \n"); for (Field field : fields) { String name = field.getName(); sb.append("<result property=\"").append(name).append("\" column=\"").append(name.toUpperCase()) .append("\" />\n"); } sb.append("</resultMap>\n"); } } System.out.println(sb.toString()); return sb.toString(); } public String getInsert() { StringBuffer sb = new StringBuffer(); sb.append("<!-- 新增 -->\n"); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { String[] fields = tableNameToColumns.get(table); int length = fields.length - 1; sb.append("<insert id=\"insert\"").append(" parameterClass=\"").append(table).append("\"> \n"); sb.append("insert into ").append(table).append("("); for (int i = 0; i < length; i++) { sb.append(fields[i]).append(","); } sb.append(fields[length]); sb.append(")\n values ("); for (int i = 0; i < length; i++) { sb.append("#").append(getProperty(fields[i])).append("#,"); } sb.append("#").append(getProperty(fields[length])).append("#"); sb.append(")\n"); sb.append("</insert>\n"); } } else { for (Class<?> class1 : classz) { Field[] fields = class1.getDeclaredFields(); int length = fields.length - 1; sb.append("<insert id=\"insert\"").append(" parameterClass=\"").append(class1.getName()) .append("\"> \n"); sb.append("insert into ").append("t_").append(class1.getSimpleName()).append("("); for (int i = 0; i < length; i++) { sb.append(fields[i].getName().toUpperCase()).append(","); } sb.append(fields[length].getName().toUpperCase()); sb.append(")\n values ("); for (int i = 0; i < length; i++) { sb.append("#").append(fields[i].getName()).append("#,"); } sb.append("#").append(fields[length].getName()).append("#"); sb.append(")\n"); sb.append("</insert>\n"); } } System.out.println(sb.toString()); return sb.toString(); } public String getSelect() { StringBuffer sb = new StringBuffer(); sb.append("<!-- 查询 -->\n"); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { String[] fields = tableNameToColumns.get(table); int length = fields.length - 1; sb.append("<select id=\"select\"").append(" resultMap=\"").append(table).append(".resultMap\"> \n"); sb.append("select \n"); for (int i = 0; i < length; i++) { sb.append("\t").append(fields[i]).append(",\n"); } sb.append("\t").append(fields[length]).append("\n"); sb.append(" from ").append(table).append(" \n"); sb.append("<dynamic prepend=\"where\">\n"); for (String field : fields) { sb.append("<isNotEmpty property=\"").append(getProperty(field)).append("\" prepend=\"and\">\n"); sb.append(field).append(" = #").append(getProperty(field)).append("#\n"); sb.append("</isNotEmpty>\n"); } sb.append("</dynamic>\n"); sb.append("</select>\n"); } } else { for (Class<?> class1 : classz) { Field[] fields = class1.getDeclaredFields(); int length = fields.length - 1; sb.append("<select id=\"select\"").append(" resultMap=\"").append(class1.getSimpleName()) .append(".resultMap\"> \n"); sb.append("select \n"); for (int i = 0; i < length; i++) { sb.append("\t").append(fields[i].getName()).append(",\n"); } sb.append("\t").append(fields[length].getName()).append("\n"); sb.append(" from ").append("t_").append(class1.getSimpleName()).append(" \n"); sb.append("<dynamic prepend=\"where\">\n"); for (Field field : fields) { String name = field.getName(); sb.append("<isNotEmpty property=\"").append(name).append("\" prepend=\"and\">\n"); sb.append(name.toUpperCase()).append(" = #").append(name).append("#\n"); sb.append("</isNotEmpty>\n"); } sb.append("</dynamic>\n"); sb.append("</select>\n"); } } System.out.println(sb.toString()); return sb.toString(); } public String getUpdate() { StringBuffer sb = new StringBuffer(); sb.append("<!-- 修改 -->\n"); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { String[] fields = tableNameToColumns.get(table); sb.append("<update id=\"update\"").append(" parameterClass=\"").append(table).append("\"> \n"); sb.append("update ").append(table).append("\n"); sb.append("<dynamic prepend=\"set\">\n"); for (String field : fields) { sb.append("<isNotEmpty property=\"").append(getProperty(field)).append("\" prepend=\",\">\n"); sb.append(field).append(" = #").append(getProperty(field)).append("#\n"); sb.append("</isNotEmpty>\n"); } sb.append("</dynamic>\n"); sb.append("where id = 1 \n"); sb.append("</update>\n"); } } else { for (Class<?> class1 : classz) { Field[] fields = class1.getDeclaredFields(); sb.append("<update id=\"update\"").append(" parameterClass=\"").append(class1.getName()) .append("\"> \n"); sb.append("update ").append("t_").append(class1.getSimpleName()).append("\n"); sb.append("<dynamic prepend=\"set\">\n"); for (Field field : fields) { String name = field.getName(); sb.append("<isNotEmpty property=\"").append(name).append("\" prepend=\",\">\n"); sb.append(name.toUpperCase()).append(" = #").append(name).append("#\n"); sb.append("</isNotEmpty>\n"); } sb.append("</dynamic>\n"); sb.append("where id = 1 \n"); sb.append("</update>\n"); } } System.out.println(sb.toString()); return sb.toString(); } public String getDelete() { // StringBuffer sb = new StringBuffer(); sb.append("<!-- 删除 -->\n"); if (tableNameToColumns.size() > 0) { for (String table : tableNameToColumns.keySet()) { sb.append("<delete id=\"delete\" parameterClass=\"String\">\n"); sb.append("delete from" ).append(table).append(" where id = 1\n"); sb.append("</delete>\n"); } } else { for (Class<?> class1 : classz) { sb.append("<delete id=\"delete\" parameterClass=\"String\">\n"); sb.append("delete from " ).append("t_").append(class1.getSimpleName()).append(" where id = 1\n"); sb.append("</delete>\n"); } } System.out.println(sb.toString()); return sb.toString(); } public static void main(String[] args) throws SQLException { List<Class<?>> classz = new ArrayList<Class<?>>(); /* * classz.add(ConstantValue.class); classz.add(Right.class); * classz.add(SysConfig.class); classz.add(TaskBean.class); */ classz.add(Right.class); new IbatisByJavaBenOrTableColumnToXml(classz).getXml(); } }