在数据库设计符合约定的情况下,自己通过编程实现根据数据库自动生成vo类。
首先我们需要获取数据库中的所有的表及其结构信息。
代码如下:(示例为MySQL数据库)
package com.wanmait.demo.util.vocreator; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.wanmait.demo.util.DbHelper; public class SQLInfomationUtil { public static List<Column> getTableColumns(String dbName,String tableName){ ArrayList<Column> columns = new ArrayList<>(); String sql = "select column_name,data_type from information_schema.`columns`\r\n" + "where table_schema=? and table_name=?"; Connection conn = DbHelper.getConn(); PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1,dbName); ps.setString(2,tableName); rs = ps.executeQuery(); while(rs.next()) { Column column = new Column(); column.setName(rs.getString("column_name")); column.setType(rs.getString("data_type")); columns.add(column); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbHelper.close(conn, ps, rs); } return columns; } public static List<String> getTableNames(String dbName){ String sql = "select table_name from information_schema.`TABLES`\r\n" + "where table_schema=?"; ArrayList<String> names = new ArrayList<>(); Connection conn = DbHelper.getConn(); PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1,dbName); rs = ps.executeQuery(); while(rs.next()) { names.add(rs.getString("table_name")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbHelper.close(conn, ps, rs); } return names; } }
方法getTableNames()获取指定数据库中的所有表的名称信息getTableColumns()方法获取指定表的结构信息。
有两个这样的信息,我们就可以尝试生成对应的Vo类了。
package com.wanmait.demo.util.vocreator; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.util.List; import com.wanmait.demo.test.Test; public class JavaCodeCreator { public static void turnTableToVo(String dbName,String packageName) { //String dbName = "newssys"; //String packageName = "com.wanmait.newssys.vo"; List<String> names = SQLInfomationUtil.getTableNames(dbName); names.forEach( name->{ List<Column> columns = SQLInfomationUtil.getTableColumns(dbName, name); JavaCodeCreator.createVoCode(name,columns,packageName); }); } private static File getDir(String packageName) { //String packageName = "com.wanmait.demo.vo"; URL url = Test.class.getClassLoader().getResource("db.properties"); String path = url.getPath().substring(1); path = path.substring(0,path.indexOf("/build/")); File root = new File(path+"/src/"+packageName.replaceAll("\\.", "/")); if(!root.exists()||!root.isDirectory()) { root.mkdirs(); } return root; } private static String turnClassName(String name) { return name.toUpperCase().charAt(0)+name.substring(1); } public static void createVoCode(String tableName,List<Column> columns,String packageName) { String name = turnClassName(tableName); StringBuilder buf = new StringBuilder(); buf.append("package ").append(packageName).append(";\r\n"); buf.append("public class "+name+"{\r\n"); //生成属性定义 columns.forEach(column->{ buf.append(String.format("\tprivate %s %s;\r\n",column.getJavaType(),column.getJavaName())); }); //生成 setter getter columns.forEach(column->{ String cname = column.getJavaName(); String ctype = column.getJavaType(); buf.append(String.format("\tpublic void set%s(%s %s){\r\n\t\tthis.%s=%s;\r\n\t}\r\n",turnClassName(cname),ctype,cname,cname,cname )); buf.append(String.format("\tpublic %s get%s(){\r\n\t\treturn %s;\r\n\t}\r\n",ctype,turnClassName(cname),cname)); }); buf.append("}\r\n"); File dir = getDir(packageName); File file = new File(dir,name+".java"); try { FileWriter out = new FileWriter(file); out.write(buf.toString()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
0条评论
点击登录参与评论