DAO(Data Access Object) 设计模式
实现功能的模块化,更有利于代码的维护和升级
DAO访问数据信息的类,包括了对数据的CRUD(CREATE,READ,UPDATE,DELETE).而不包含任何业务相关信息
使用 JDBC 编写DAO 可能会包含的方法:
void update(String sql,Object...objs)
<T> T get(Class<T> clazz , String sql,Object...objs)
<T> List<T> getForList(Class<T> clazz,String sql,Object...objs)
E getForValue(String sql,Object...objs)
Java类的属性
在JavaEE中,Java类的属性通过getter,setter方法 来定义,get(set)方法,去除get和set,首字母小写即为java的类的属性
以前叫的属性,即成员变量,称之为字段
注:一般情况下属性名和字段名一致
操作java类的工具包beanutils,setProperty(),getproperty()
搭建环境,所需的jar包
commons-beanutils-1.9.2
commons-logging-1.2
测试单元类:
package xuezaipiao3;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import xuezaipiao1.JDBC_Tools;
public class DAO {
public static void update(String sql,Object...objs){
Connection conn = null;
PreparedStatement ps =null;
try {
conn = JDBC_Tools.getConnection();
ps = conn.prepareStatement(sql);
for(int i = 0;i<objs.length;i++){
ps.setObject(i+1, objs[i]);
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_Tools.relaseSource(conn, ps);
}
}
public static <T> T get(Class<T> clazz , String sql,Object...objs){
/*T entity = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBC_Tools.getConnection();
ps = conn.prepareStatement(sql);
for(int i = 0;i<objs.length;i++){
ps.setObject(i + 1, objs[i]);
}
rs = ps.executeQuery();
Map<String , Object > values = new HashMap<String, Object>();
ResultSetMetaData rsmd = rs.getMetaData();
if(rs.next()){
for(int i = 0;i<rsmd.getColumnCount();i++){
String columnName =rsmd.getColumnLabel(i+1);
Object value = rs.getObject(i+1);
values.put(columnName, value);
}
}
if(values.size()>0){
entity = clazz.newInstance();
for(Map.Entry<String, Object> entry : values.entrySet()){
String fieldName = entry.getKey();
Object obj = entry.getValue();
//ReflectionUtils.setFieldValue(entity, fieldName, obj);
BeanUtils.setProperty(entity, fieldName, obj);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_Tools.relaseSource(conn, ps);
}
return entity;*/
List<T> ls = getForList(clazz, sql, objs);
if(ls.size()>0){
return ls.get(0);
}
return null;
}
public static <T> List<T> getForList(Class<T> clazz,
String sql,Object...objs){
List<T> lists = new ArrayList<T>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1. 获取数据库连接
conn = JDBC_Tools.getConnection();
//2. 获取 PreparedStatement 对象
ps = conn.prepareStatement(sql);
//3. 填充占位符
for(int i = 0;i<objs.length;i++){
ps.setObject(i + 1, objs[i]);
}
//4. 执行SQL,得到结果集
rs = ps.executeQuery();
List<Map<String, Object>> mapColl = handleResultSetToMapList(rs);
lists = transformMapListToBeanList(clazz, mapColl);
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBC_Tools.relaseSource(rs,conn, ps);
}
return lists;
}
private static <T> List<T> transformMapListToBeanList(Class<T> clazz,
List<Map<String, Object>> mapColl)
throws Exception {
List<T> result = new ArrayList<T>();
T entity = null;
if(mapColl.size() > 0){
for(Map<String , Object> map : mapColl){
entity = clazz.newInstance();
for(Map.Entry<String, Object> entry : map.entrySet()){
String propertyName = entry.getKey();
Object propertyValue = entry.getValue();
BeanUtils.setProperty(entity, propertyName, propertyValue);
}
result.add(entity);
}
}
return result;
}
/**
* 处理结果集得到Map的List,其中一个MAP对应一个对象
* @param rs
* @return
* @throws SQLException
*/
private static List<Map<String, Object>> handleResultSetToMapList(
ResultSet rs) throws SQLException {
List<Map<String,Object>> mapColl = new ArrayList<Map<String,Object>>();
List<String> columnLabels = getColumnLabels(rs);
//5. 获取 ResultSetmetaData 对象
while(rs.next()){
Map<String,Object> values = new HashMap<String, Object>();
for(String columnLabel : columnLabels){
Object value = rs.getObject(columnLabel);
values.put(columnLabel,value);
}
mapColl.add(values);
}
return mapColl;
}
/**
* 获取结果集 column'Label
* @param rs
* @return
* @throws SQLException
*/
private static List<String> getColumnLabels(ResultSet rs) throws SQLException{
List<String> list = new ArrayList<String>();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for(int i = 0;i<columnCount;i++){
list.add(rsmd.getColumnLabel(i + 1));
}
return list;
}
/**
* 查某条记录的某一字段或 一个统计值
* @param sql
* @param objs
* @return
*/
@SuppressWarnings("unchecked")
public static <E> E getForValue(String sql,Object...objs){
//1. 得到结果集,该结果只有一行一列
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1. 获取数据库连接
conn = JDBC_Tools.getConnection();
//2. 获取 PreparedStatement 对象
ps = conn.prepareStatement(sql);
//3. 填充占位符
for(int i = 0;i<objs.length;i++){
ps.setObject(i + 1, objs[i]);
}
//2. 取得结果
rs = ps.executeQuery();
if(rs.next()){
return (E) rs.getObject(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBC_Tools.relaseSource(rs,conn, ps);
}
return null;
}
}
用 BeanUtils 代替了手写反射出现的问题