java JDBC 万能DAO的实现
数据库和实体类设计好以后,有几个实体类就需要几个Dao吗?显然不是。。。
利用java的反射机制,一个Dao就可以实现对所有实体类的操作
思路就是,传过来一个实体类,利用实体类得到表,把实体类的每一个字段取出,放到表对应的字段。。。。
但是,如果实体类字段发生变化,这个时候往数据库插入数据就会出错,原因是表里面没有与实体类属性对应的字段,
这个时候就要用到java的反射机制了
我们首先需要得到表的所有字段,然后得到实体类的所有属性,两个一一比较,实体类中有的属性而数据库没有与之对应的字段,就舍去,这就是基本的思路
代码如下:
package com.lp.DAO; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.lp.bean.goods; /** * 2015.7.20 利用java反射实现万能DAO 即使bean层的实体类字段改变 也能实现增删查改 * * @author lp */ public class DaoPro { // 由数据库名称得到数据库中的所有列 public List<String> getColumns(String tableName) { Connection connection = DbHelper.getCon(); Statement statement = DbHelper.getStatement(); ResultSet resultSet = null; List<String> list = new ArrayList<String>(); try { String sql = "select * from " + tableName + " where 1=2"; resultSet = DbHelper.getStatement().executeQuery(sql); ResultSetMetaData data = resultSet.getMetaData(); int columnsCount = data.getColumnCount(); for (int i = 1; i <= columnsCount; i++) { String s = data.getColumnName(i); list.add(s); } } catch (SQLException e) { e.printStackTrace(); } finally { DbHelper.cloRes(connection, statement, resultSet); } return list; } // 填充实体类,返回一个实体类集合 public List getList(Class cl) { int i = 0; List objs = new ArrayList(); Connection connection = DbHelper.getCon(); Statement statement = DbHelper.getStatement(); ResultSet resultSet = null; Field[] fields = cl.getDeclaredFields(); List<String> columnsName = getColumns(cl.getSimpleName()); String sql = "select * from " + cl.getSimpleName(); try { System.out.println(sql); resultSet = statement.executeQuery(sql); System.out.println(i++); while (resultSet.next()) { System.out.println(i++); Object obj = cl.newInstance(); for (String str : columnsName) { for (Field ff : fields) { if (str.equals(ff.getName())) { ff.setAccessible(true); ff.set(obj, resultSet.getObject(ff.getName())); break; } } } objs.add(obj); } } catch (SQLException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return objs; } public void addData(Object object) { Connection connection = DbHelper.getCon(); ResultSet resultSet = null; PreparedStatement preparedStatement = null; String tableName = object.getClass().getSimpleName(); Field[] fields = object.getClass().getDeclaredFields(); List<String> allColumns = getColumns(tableName); StringBuffer buffer = new StringBuffer(); StringBuffer buffer1 = new StringBuffer(); buffer.append("insert into "); buffer.append(tableName); buffer.append(" ("); for (int i = 1; i < allColumns.size(); i++) { buffer.append(allColumns.get(i)); buffer1.append("?"); if (i != allColumns.size() - 1) { buffer.append(","); buffer1.append(","); } } buffer.append(") values ("); buffer.append(buffer1); buffer.append(")"); // System.out.println(buffer.toString()); try { preparedStatement = connection.prepareStatement(buffer.toString()); for (int i = 1; i < allColumns.size(); i++) { for (int j = 0; j < fields.length; j++) { if ((fields[j].getName()).equals(allColumns.get(i))) { fields[j].setAccessible(true); if (fields[j].get(object) == null) { preparedStatement.setObject(i, "此字段无填充"); } else { preparedStatement.setObject(i, fields[j].get(object)); } break; } } } int a = preparedStatement.executeUpdate(); System.out.println(a); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbHelper.cloRes(connection, preparedStatement, resultSet); } } public static void main(String[] args) { goods c = new goods(); new DaoPro().addData(c); } }