利用泛型封装DAO层(万能DAO)
利用泛型封装DAO层(万能DAO)
package com.cdsxt.base;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import com.cdsxt.po.Person;
import com.cdsxt.util.DBUtil;
//泛型方法
public class BaseDao2 {
//传入po 根据po的值 返回匹配的list
public static List queryList(String sql,Object[] params,T t){
Class clazz=t.getClass();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List list=new ArrayList();
try {
conn=DBUtil.getConn();
ps=conn.prepareStatement(sql);
//填坑
if(params!=null&¶ms.length>0){
for(int i=0;i
ps.setObject(i+1, params[i]);
}
}
rs=ps.executeQuery();
ResultSetMetaData metaData=rs.getMetaData();
int count=metaData.getColumnCount();
while(rs.next()){
T temp=(T)clazz.newInstance();
for(int i=0;i
String fieldName=metaData.getColumnName(i+1);
Field field=clazz.getDeclaredField(fieldName);
Method method=clazz.getMethod(getSetter(fieldName), field.getType());
method.invoke(temp, rs.getObject(i+1));
}
list.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(rs,ps,conn);
}
return list;
}
//根据id查询po
public static T queryPo(String sql,int id,T t){
Class clazz=t.getClass();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
T obj=null;
try {
conn=DBUtil.getConn();
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
ResultSetMetaData metaData=rs.getMetaData();
int count=metaData.getColumnCount();
obj=(T)clazz.newInstance();
while(rs.next()){
for(int i=0;i
String fieldName=metaData.getColumnName(i+1);
Field field =clazz.getDeclaredField(fieldName);
Method method=clazz.getMethod(getSetter(fieldName), field.getType());
method.invoke(obj, rs.getObject(i+1));
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(rs,ps,conn);
}
return obj;
}
public static void changePo(String sql,Object[] params,Class clazz){
Connection conn=null;
PreparedStatement ps=null;
try {
conn=DBUtil.getConn();
ps=conn.prepareStatement(sql);
//填坑
if(params!=null&¶ms.length>0){
for(int i=0;i
ps.setObject(i+1, params[i]);
}
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(ps,conn);
}
}
public static String getSetter(String fieldName){
//传入属性名 拼接set方法
return “set”+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
}
public static void main(String[] args) {
// List list=BaseDao2.queryList(“select * from person “, null, new Person());
// System.out.println(list.size());
Person person=queryPo(“select * from person where id=?”,4,new Person());
System.out.println(person.getName());
}
}
后面的main()方法为测试用,用的时候将其删除即可
2.DBUtil类:
package com.cdsxt.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DBUtil {
private static String mysqlDriver;
private static String url;
private static String user;
private static String password;
static{
try {
InputStream is=DBUtil.class.getResourceAsStream(“/db.properties”);
Properties ps=new Properties();
ps.load(is);
mysqlDriver=ps.getProperty(“mysqlDriver”);
url=ps.getProperty(“url”);
user=ps.getProperty(“user”);
password=ps.getProperty(“password”);
Class.forName(mysqlDriver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConn(){
Connection conn=null;
try {
conn= DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
System.out.println(“获取数据库链接异常”);
}
return conn;
}
//关闭资源
public static void close(Object…objs){
if(objs!=null&&objs.length>0){
for(int i=0;i
try {
if(objs[i] instanceof Connection){
((Connection)objs[i]).close();
}else if(objs[i] instanceof PreparedStatement){
((PreparedStatement)objs[i]).close();
}else if(objs[i] instanceof ResultSet){
((ResultSet)objs[i]).close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(“关闭资源 异常”);
}
}
}
}
}
———————
作者:mischen520
来源:CSDN
原文:https://blog.csdn.net/miachen520/article/details/52005505
版权声明:本文为博主原创文章,转载请附上博文链接!