前段时间做了个小项目由于是用JDBC来访问数据库所以通过查询来获得数据后免不了会通过实体的set方法来将对象返回到service层,这样一来工作量无形中又加了许多,而且一个方法里大量的代码都是用来封装对象,所以我就根据需要写了个简单的实体封装工具类,感兴趣的朋友可以优化、指正下:
package com.etc.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Title: DAO.java
* @Package com.etc.utils
* @Description: DAO工具类
* @author dc
* @date 2012-11-28 下午10:09:43
* @version V1.0
*/
public class DAOUtil {
/**
* 根据类型和结果集来得到封装的单个对象
*
* @param clazz
* :实体类的Class对象
* @param rs
* :JDBC查询的结果集
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static synchronized <T> T enityEncapsulation(Class<T> clazz,
ResultSet rs) throws SQLException, InstantiationException,
IllegalAccessException, InvocationTargetException {
ResultSetMetaData rsm = rs.getMetaData();
Method[] methods = clazz.getDeclaredMethods();
int column = rsm.getColumnCount();// 得到列数
T obj = null;
while (rs.next()) {
obj = clazz.newInstance();// 创建对象实例
for (int i = 1; i <= column; i++) {
String set = "set" + rsm.getColumnName(i);
for (Method method : methods) {
// 根据数据库的列名(这里没考虑别名)实体的属性名来判断该调用的set方法
if (method.getName().toLowerCase()
.equals(set.toLowerCase())) {
// 根据set方法的参数类型来确定从结果集里拿数据的方式
// (这里的类型只有3种是应为项目只需要3种所有我没有加其他的类型)
if (method.getGenericParameterTypes()[0]
.equals(Integer.TYPE)) {
method.invoke(obj, rs.getInt(i));
} else if (method.getGenericParameterTypes()[0]
.equals(Date.class)) {
method.invoke(obj, rs.getDate(i));
} else {
method.invoke(obj, rs.getString(i));
}
}
}
}
}
return obj;
}
/**
* 根据类型和结果集来得到封装的对象的集合
*
* @param clazz
* :实体类的Class对象
* @param rs
* :JDBC查询的结果集
* @return 返回的是list的集合(根据项目需要)
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static synchronized <T> List<T> enitysEncapsulation(Class<T> clazz,
ResultSet rs) throws SQLException, InstantiationException,
IllegalAccessException, InvocationTargetException {
ResultSetMetaData rsm = rs.getMetaData();
Method[] methods = clazz.getDeclaredMethods();
int column = rsm.getColumnCount();
List<T> list = new ArrayList<T>();
while (rs.next()) {
T obj = clazz.newInstance();
for (int i = 1; i <= column; i++) {
String set = "set" + rsm.getColumnName(i);
for (Method method : methods) {
if (method.getName().toLowerCase()
.equals(set.toLowerCase())) {
if (method.getGenericParameterTypes()[0]
.equals(Integer.TYPE)) {
method.invoke(obj, rs.getInt(i));
} else if (method.getGenericParameterTypes()[0]
.equals(Date.class)) {
method.invoke(obj, rs.getDate(i));
} else {
method.invoke(obj, rs.getString(i));
}
}
}
}
list.add(obj);
}
return list;
}
}
使用方法挺简单的:
1.得到List集合
/**
* 查询所有部门信息
*
* @return 所有部门信息List
*/
@Override
public List<Department> selAllDept() {
List<Department> depts = null;
conn = DButil.getConnection();
try {
ps = conn.prepareStatement("select dt1.dept_id,dt1.dept_name,dt2.dept_name as sup_dept_name,dt1.dept_dspt from t_department dt1,t_department dt2 where dt1.sup_dept_id = dt2.dept_id order by dt1.dept_id");
rs = ps.executeQuery();
Class<Department> clazz=Department.class;
depts=DAOUtil.enitysEncapsulation(clazz, rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
DButil.close(rs, ps, conn);
}
return depts;
}
2.得到单个实体
public Department selDept(int dept_id) {
Connection conn = DButil.getConnection();
Department dept=null;
try {
ps= conn.prepareStatement("select t1.*,t2.dept_name as sup_dept_name from t_department t1,t_department t2 where t1.sup_dept_id=t2.dept_id and t1.dept_id=?");
ps.setInt(1, dept_id);
rs = ps.executeQuery();
Class<Department> clazz =Department.class;
dept=DAOUtil.enityEncapsulation(clazz, rs);
} catch (Exception e) {
e.printStackTrace();
}finally{
DButil.close(rs, ps, conn);
}
return dept;
}
注意:数据库里的列名和实体类的属性名字和类型要对应不然得理想的数据