1、DAO(Data Access Object):访问数据信息的类,包含对数据的CRUD(Create、Read、Update、Delete),而不包含任何业务相关的信息。
--DAO能够实现功能的模块化;
--有利于代码的维护和升级。
2、DAO中常用的方法:
--1)int update(String sql, Object ... args); //执行更新操作,返回受影响的行数,包括INSERT、UPDATE、DELETE操作。
--2)<T> T get(Class<T> clazz, String sql, Object ... args); //查询一条记录,返回对应的对象。
--3)<T> List<T> getForList(Class<T> clazz, String sql, Objeect ... args); // 查询多条记录,返回对应的对象列表。
--4)<E> E getForValue(String sql, Object ... args); //返回某条记录的某一个字段的值或一个统计的记录数。
3、实现:
public class DAO { public int update(String sql, Object ... args){ Connection conn = null; PreparedStatement ps = null; int rowNum = 0; try{ conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for(int i = 0; i < args.length; i++){ ps.setObject(i + 1, args[i]); } rowNum = ps.executeUpdate(); return rowNum; }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, ps, null); } return rowNum; } public <T> T get(Class<T> clazz, String sql, Object ...args){ T entity = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for(int i = 0; i < args.length; i++){ ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); Map<String, Object> map = new HashMap<String, Object>(); List<String> columnLabels = getColumnLabels(rs); if(rs.next()){ for(String columnLabel : columnLabels){ Object columnValue = rs.getObject(columnLabel); map.put(columnLabel, columnValue); } } if(map.size() > 0){ entity = clazz.newInstance(); for(Map.Entry<String, Object> entry : map.entrySet()){ String fieldName = entry.getKey(); Object value = entry.getValue(); //使用BeanUtils工具类来为属性赋值 BeanUtils.setProperty(entity, fieldName, value); /**使用反射的方式为属性赋值 Field field = clazz.getDeclaredField(key); field.setAccessible(true); field.set(entity, value);*/ } return entity; } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, ps, rs); } return entity; } public <T> List<T> getForList(Class<T> clazz, String sql, Object ... args){ List<T> list = new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for(int i = 0; i < args.length; i++){ ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); /** * 将得到的ResultSet结果集转换为Map<String, Object>列表, * 其中key值为rs表的别名,value值为对应别名的值 */ List<Map<String, Object>> data = handleResultSetToMapList(rs); /** * 将MapList转换为对应的BeanList */ list = transferMapListToBeanList(clazz, data); }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, ps, rs); } return list; } private <T> List<T> transferMapListToBeanList(Class<T> clazz,List<Map<String, Object>> data) throws Exception { List<T> list = new ArrayList<>(); if(data.size() > 0){ for(Map<String, Object> map : data){ T entity = clazz.newInstance(); for(Map.Entry<String, Object> entry : map.entrySet()){ String fieldName = entry.getKey(); Object value = entry.getValue(); BeanUtils.setProperty(entity, fieldName, value);; } list.add(entity); } } return list; } public static <E> E getForValue(String sql, Object ... args){ E entity = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for(int i = 0; i < args.length; i++){ ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); if(rs.next()){ return (E)rs.getObject(1); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, ps, rs); } return entity; } /** * 将获取到的结果集处理成MapList * @param rs * @return * @throws Exception */ private List<Map<String, Object>> handleResultSetToMapList(ResultSet rs) throws Exception { List<String> columnLabels = getColumnLabels(rs); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); while(rs.next()){ Map<String, Object> map = new HashMap<String, Object>(); for(String columnLabel : columnLabels){ Object columnValue = rs.getObject(columnLabel); map.put(columnLabel, columnValue); } data.add(map); } return data; } /** * 根据结果集获取所有列的别名 * @param rs * @return * @throws Exception */ private List<String> getColumnLabels(ResultSet rs) throws Exception{ List<String> list = new ArrayList<>(); ResultSetMetaData rsmd = rs.getMetaData(); for(int i = 0; i < rsmd.getColumnCount(); i++){ list.add(rsmd.getColumnLabel(i + 1)); } return list; } }