1、如果拿到表结构请关注我前面的文章
2、生成bean
package com.pingan.util; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.commons.io.FileUtils; import com.pingan.bean.ColumnModel; public class BeanProcess { /** * 从表结构中去生成javabean * @param structureList * @param beanName * @return */ public static String genJavaBeanFromTableStructure(ListcolumnModelList,String beanName,String packagePath){ StringBuffer sb = new StringBuffer(); try { //java package引入 sb.append("package "+packagePath.replace("\\", ".")+";\r\n"); //java import引入 for (ColumnModel columnModel : columnModelList) { String columnClassName = columnModel.getColumnClassName(); if(!columnClassName.contains("lang")){ sb.append("import "+columnModel.getColumnClassName()+";\r\n"); } } //java 类声明 sb.append("public class "+StringUtils.toFirstCharUpCase(beanName)+" {\r\n"); //java 属性 for (ColumnModel columnModel : columnModelList) { if(StringUtils.isNotBlank(columnModel.getRemarks())){ sb.append(" //"+columnModel.getRemarks()+" \r\n"); } sb.append(" private "+columnModel.getFieldType()+" "+columnModel.getFieldName()+";\r\n"); } sb.append("\r\n"); //java 属性get set方法 for (ColumnModel columnModel : columnModelList) { sb.append( "\tpublic "+columnModel.getFieldType()+" get"+StringUtils.toFirstCharUpCase((String) columnModel.getFieldName())+"() {\r\n" + "\t\treturn "+columnModel.getFieldName()+";\r\n" + "\t}\r\n" + "\r\n" + "\tpublic void set"+StringUtils.toFirstCharUpCase((String) columnModel.getFieldName())+"("+columnModel.getFieldType()+" "+columnModel.getFieldName()+") {\r\n" + "\t\t this."+columnModel.getFieldName()+" = "+columnModel.getFieldName()+";\r\n" + "\t}\r\n\r\n"); } sb.append("}\r\n"); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } /** * 将bean信息读到磁盘上 * @param classPath * @param packagePath * @param tableName * @param beanClassName * @throws IOException */ public static void createBeanInDisk(String classPath,String packagePath,String tableName,String beanClassName) throws IOException{ List columnModelList = JdbcUtil.getTableStructure(tableName); String beanDetail = genJavaBeanFromTableStructure(columnModelList,beanClassName,packagePath); System.out.println("已生成数据:\r"+beanDetail); File javaFile = new File(classPath+File.separator+packagePath+File.separator+StringUtils.toFirstCharUpCase(beanClassName)+".java"); FileUtils.writeStringToFile(javaFile, beanDetail); } /** * @param args */ public static void main(String[] args) { String classPath = "D:\\zhuangxinliang\\commonWeb\\src"; String packagePath = "com\\pingan\\bean"; try { createBeanInDisk(classPath,packagePath,"wealth_product_info","productInfo"); } catch (IOException e) { e.printStackTrace(); } } }
3、生成对应的ibatis映射
package com.pingan.util; import java.util.List; import com.pingan.bean.ColumnModel; public class IbatisProcess { /** * 生成ibatis对象别名 * @param clazz * @return */ public static String genObjectTypeAlias(Class> clazz){ return ""; } /** * 生成whereSql语句块 * @param clazz * @return */ public static String genWhereSql(List columnModelList,Class> clazz,String tableName){ StringBuffer sb = new StringBuffer(); sb.append(" "); for (ColumnModel columnModel : columnModelList) { if(!columnModel.getColumnName().contains("ID_")){ sb.append(" "); return sb.toString(); } /** * 对象结果映射 * @param clazz * @return */ public static String genResultMapper(List\r\n"); sb.append(" "+columnModel.getColumnName()+"=#"+columnModel.getFieldName()+"#\r\n"); sb.append(" \r\n"); } } sb.append("columnModelList,Class> clazz,String tableName){ StringBuffer sb = new StringBuffer(); sb.append(" \r\n"); for (ColumnModel columnModel : columnModelList) { sb.append(" "); return sb.toString(); } /** * 生成插入语句 * @param clazz * @param tableName * @return */ public static String genInsertMapper(List\r\n"); } sb.append(" columnModelList,Class> clazz,String tableName){ StringBuffer sb = new StringBuffer(); sb.append(" \r\n"); sb.append(" insert into "+tableName+"(\r\n"); for (ColumnModel columnModel : columnModelList) { if(!columnModel.getColumnName().contains("ID_")){ sb.append(" "+columnModel.getColumnName()+",\r\n"); } } sb.deleteCharAt(sb.lastIndexOf(",")); sb.append(" ) "); sb.append("values(\r\n"); for (ColumnModel columnModel : columnModelList) { if(!columnModel.getColumnName().contains("ID_")){ sb.append(" #"+columnModel.getFieldName()+"#,\r\n"); } } sb.deleteCharAt(sb.lastIndexOf(",")); sb.append(" )\r\n"); sb.append(" "); return sb.toString(); } /** * 生成更新语句 * @param clazz * @param tableName * @return */ public static String genUpdateMapper(ListcolumnModelList,Class> clazz,String tableName){ StringBuffer sb = new StringBuffer(); sb.append(" \r\n"); sb.append(" update "+tableName+" set\r\n"); for (ColumnModel columnModel : columnModelList) { if(!columnModel.getColumnName().contains("ID_")){ sb.append(" "); return sb.toString(); } /** * 生成更新语句 * @param clazz * @param tableName * @return */ public static String genSelectMapper(List\r\n"); sb.append(" "+columnModel.getColumnName()+"=#"+columnModel.getFieldName()+"#\r\n"); sb.append(" \r\n"); } } sb.deleteCharAt(sb.lastIndexOf(",")); sb.append(" where 1=1 \r\n"); sb.append("\r\n"); sb.append(" columnModelList,Class> clazz,String tableName){ StringBuffer sb = new StringBuffer(); sb.append(""); return sb.toString(); } /** * 主方法 * @param args */ public static void main(String[] args) { try { String tableName = "wealth_product_info"; List columnModelList = JdbcUtil.getTableStructure(tableName); Class clazz = Class.forName("com.pingan.bean.ProductInfo"); System.out.println(genObjectTypeAlias(clazz)); System.out.println(genWhereSql(columnModelList,clazz,tableName)); System.out.println(genResultMapper(columnModelList,clazz,tableName)); System.out.println(genUpdateMapper(columnModelList,clazz,tableName)); System.out.println(genSelectMapper(columnModelList,clazz,tableName)); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
4、String工具类
package com.pingan.util; public class StringUtils extends org.apache.commons.lang.StringUtils{ /** * 将数据库字段转换成bean属性 * @param columnName * @return */ public static String getFieldName(String columnName) { char[] columnCharArr = columnName.toLowerCase().toCharArray(); StringBuffer sb = new StringBuffer(); int ad = -1; for (int i = 0; i < columnCharArr.length; i++) { char cur = columnCharArr[i]; if(cur=='_'){ ad = i; }else{ if((ad+1)==i&&ad!=-1){ sb.append(Character.toUpperCase(cur)); }else{ sb.append(cur); } ad=-1; } } return sb.toString(); } /** * 将首字母变大写 * @param str * @return */ public static String toFirstCharUpCase(String str){ char[] columnCharArr = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < columnCharArr.length; i++) { char cur = columnCharArr[i]; if(i==0){ sb.append(Character.toUpperCase(cur)); }else{ sb.append(cur); } } return sb.toString(); } /** * 将首字母变小写 * @param str * @return */ public static String toFirstCharLowCase(String str){ char[] columnCharArr = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < columnCharArr.length; i++) { char cur = columnCharArr[i]; if(i==0){ sb.append(Character.toLowerCase(cur)); }else{ sb.append(cur); } } return sb.toString(); } }
其它的类可参数我前面的文章,谢谢