<八>JDBC_重构DAO查询方法
DAO类:
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 com.kk.jdbc.JDBCTools;
import com.kk.jdbc.ReflectionUtils;
public class DAO {
//insert、update、delete操作都可以包含其中
public void update(String sql,Object...args){
Connection con=null;
PreparedStatement ps=null;
try {
con=JDBCTools.getConnection();
ps=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
} catch (Exception e) {
JDBCTools.release(null,ps, con);
}
}
//查询一条记录,返回对应的对象
public <T> T get(Class<T> clazz,String sql,Object...args){
List<T> result=getForList(clazz, sql, args);
if (result.size()>0) {
return result.get(0);
}
return null;
}
//查询多条记录,返回对应的对象的集合
public <T> List<T> getForList(Class<T> clazz,String sql,Object...args){
List<T> list=new ArrayList<>();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
/*
* 1、得到结果集
*
* */
con=JDBCTools.getConnection();
ps=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
rs=ps.executeQuery();
/*
* 2、处理结果集,得到Map的List,其中一个Map对象就是一条记录;
* key-->rs中列的别名
* value-->列的值
* */
List<Map<String, Object>> values = handleResultSetToMapList(rs);
/*
* 3、把Map的List转为clazz对应的集合,其中Map的key即为clazz对应的对象的propertyName
* value即为clazz对应对象的propertyValue.
* */
list=transfterMapListToBeanList(clazz, values);
} catch (Exception e) {
JDBCTools.release(rs, ps, con);
}
return list;
}
private <T> List<T> transfterMapListToBeanList(Class<T> clazz,List<Map<String, Object>> values)
throws InstantiationException, IllegalAccessException {
List<T> result=new ArrayList<>();
T bean=null;
//12、判断list是否为null,不为null遍历,将得到的每一个Map对象转为一个Class参数对应的Object对象
if (values.size()>0) {
for (Map<String, Object> m : values) {
bean=clazz.newInstance();
for (Map.Entry<String,Object> entry: m.entrySet()) {
String propertyName=entry.getKey();
Object value=entry.getValue();
ReflectionUtils.setFieldValue(bean, propertyName, value);
}
//13、把Object对象放入到list中
result.add(bean);
}
}
return result;
}
//处理结果集,得到Map的一个List,其中一个Map对应一条记录
private List<Map<String, Object>> handleResultSetToMapList(ResultSet rs) throws SQLException {
//5、准备一个List<Map<String,Object>>,一个Map对象对应一条记录
List<Map<String,Object>> values=new ArrayList<>();
List<String> columnLabels=getColumnLabels(rs);
Map<String,Object> map=null;
//7、处理ResultSet,使用while循环
while(rs.next()){
map=new HashMap<>();
for (String columnLabel:columnLabels) {
Object value=rs.getObject(columnLabel);
map.put(columnLabel, value);
}
//11、将填充好的Map对象放入list中
values.add(map);
}
return values;
}
//获取结果集的ColumnLabel对应的List
private List<String> getColumnLabels(ResultSet rs) throws SQLException{
List<String> labels=new ArrayList<>();
ResultSetMetaData rsmd=rs.getMetaData();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
labels.add(rsmd.getColumnLabel(i+1));
}
return labels;
}
//返回某个对象的属性值
public <E> E getForValue(String sql,Object...args){
//1、得到结果集:该结果集应该只有一行且只有一列
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=JDBCTools.getConnection();
ps=con.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{
JDBCTools.release(rs, ps, con);
}
//2、取得结果集的
return null;
}
}
单元测试类:
import java.sql.Date;
import java.util.List;
import org.junit.Test;
import com.kk.jdbc.Student;
public class DAOTest {
DAO dao=new DAO();
@Test
public void testUpdate() {
String sql="insert into customers(name,email,birth) values (?,?,?)";
dao.update(sql, "xiaoming","xm@.com",new Date(new java.util.Date().getTime()));
}
@Test
public void testGet() {
String sql="select FlowId flow_Id,Type,IDCard,ExamCard,StudentName from examstudent where FlowId=?";
Student stu=dao.get(Student.class,sql,1);
System.out.println(stu);
}
@Test
public void testGetForList() {
String sql="select FlowId flow_Id,Type,IDCard,ExamCard,StudentName from examstudent";
List<Student> st=dao.getForList(Student.class, sql);
System.out.println("向日葵:"+st);
}
@Test
public void testGetForValue() {
String sql="select ExamCard from examstudent where FlowId=?";
String examCard=dao.getForValue(sql, 13);
System.out.println(examCard);
sql="select max(grade) from examstudent";
int grade=dao.getForValue(sql);
System.out.println(grade);
}
}