package com.ly.dao.util;
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.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @ClassName: Dao
* @Description: TODO(通用Dao)
* @author Uncle liu
* @date 2018年5月24日 下午7:31:57
*
*/
@SuppressWarnings({"unchecked","rawtypes"})
public class Dao<T> {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
/**
* @param conn 要设置的 Connection
*/
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* @return conn
*/
private Connection getConn() {
return conn;
}
/**
*
* @Title: queryList
* @Description: TODO(通用查询方法)
* @param sql SQL语句
* @param params SQL语句中占位符的参数
* @param t 接受数据的对象
* @param m 设置对象属性接受位置,key为属性名,value为获得数据位置
* @return List<T>
*/
public List<T> queryList(String sql, Object[] params,T t,Map<String, Integer> m) {
//获得类对象
Class c=t.getClass();
//接收对象数据的集合
List<T> li = new ArrayList<>();
try {
this.conn = getConn();
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
this.rs = this.ps.executeQuery();
while(this.rs.next()){
//实例化对象
T temp=(T)c.newInstance();
//通过Map.entrySet遍历key和value
for (Entry<String, Integer> s : m.entrySet()) {
String name = s.getKey();
Field field=c.getDeclaredField(name);
name = name.substring(0,1).toUpperCase()+name.substring(1);
Method method = c.getMethod("set"+name, field.getType());
try {
method.invoke(temp, rs.getObject(s.getValue()));
}catch (Exception e) {
//如果不是对应基本数据类型和引用数据类型,就设置为String类型
method.invoke(temp, rs.getString(s.getValue()));
}
}
//添加对象进集合
li.add(temp);
}
return li;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close(this.conn, this.ps, this.rs);
}
return null;
}
/**
*
* @Title: queryList
* @Description: TODO(通用查询方法)
* @param sql SQL语句
* @param params SQL语句中占位符的参数
* @param path 接受数据的对象完整路径名
* @param m 设置对象属性接受位置,key为属性名,value为获得数据位置
* @return
* @throws ClassNotFoundException List<T>
*/
public List<T> queryList(String sql, Object[] params,String path,Map<String, Integer> m) throws ClassNotFoundException {
//获得类对象
Class c=Class.forName(path);
List<T> li = new ArrayList<>();
try {
this.conn = getConn();
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
this.rs = ps.executeQuery();
while(this.rs.next()){
//实例化对象
T temp=(T)c.newInstance();
//通过Map.entrySet遍历key和value
for (Entry<String, Integer> s : m.entrySet()) {
String name = s.getKey();
Field field=c.getDeclaredField(name);
name = name.substring(0,1).toUpperCase()+name.substring(1);
Method method = c.getMethod("set"+name, field.getType());
try {
method.invoke(temp, rs.getObject(s.getValue()));
}catch (Exception e) {
//如果不是对应基本数据类型和引用数据类型,就设置为String类型
method.invoke(temp, rs.getString(s.getValue()));
}
}
//添加对象进集合
li.add(temp);
}
return li;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close(this.conn, this.ps, this.rs);
}
return null;
}
/**
*
* @Title: executeUpdate
* @Description: TODO(增、删、改的通用方法)
* @param SQL语句
* @param params SQL语句中占位符的参数
* @return int
*/
public int executeUpdate(String sql, Object[] params) {
int result = -1;
try {
this.conn = getConn();
this.conn.setAutoCommit(false);
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
result = this.ps.executeUpdate();
this.conn.commit();
} catch (Exception e) {
// TODO: handle exception
try {
this.conn.rollback();
System.out.println("失败");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//throw new RuntimeException(e);
} finally {
close(this.conn, this.ps);
}
return result;
}
/**
*
* @Title: close
* @Description: TODO(查询关闭连接)
* @param con
* @param ps
* @param rs void
*/
private static void close(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(!con.isClosed()&&con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @Title: close
* @Description: TODO(增删改关闭连接)
* @param con
* @param ps void
*/
private static void close(Connection con,PreparedStatement ps) {
close(con,ps,null);
}
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.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @ClassName: Dao
* @Description: TODO(通用Dao)
* @author Uncle liu
* @date 2018年5月24日 下午7:31:57
*
*/
@SuppressWarnings({"unchecked","rawtypes"})
public class Dao<T> {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
/**
* @param conn 要设置的 Connection
*/
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* @return conn
*/
private Connection getConn() {
return conn;
}
/**
*
* @Title: queryList
* @Description: TODO(通用查询方法)
* @param sql SQL语句
* @param params SQL语句中占位符的参数
* @param t 接受数据的对象
* @param m 设置对象属性接受位置,key为属性名,value为获得数据位置
* @return List<T>
*/
public List<T> queryList(String sql, Object[] params,T t,Map<String, Integer> m) {
//获得类对象
Class c=t.getClass();
//接收对象数据的集合
List<T> li = new ArrayList<>();
try {
this.conn = getConn();
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
this.rs = this.ps.executeQuery();
while(this.rs.next()){
//实例化对象
T temp=(T)c.newInstance();
//通过Map.entrySet遍历key和value
for (Entry<String, Integer> s : m.entrySet()) {
String name = s.getKey();
Field field=c.getDeclaredField(name);
name = name.substring(0,1).toUpperCase()+name.substring(1);
Method method = c.getMethod("set"+name, field.getType());
try {
method.invoke(temp, rs.getObject(s.getValue()));
}catch (Exception e) {
//如果不是对应基本数据类型和引用数据类型,就设置为String类型
method.invoke(temp, rs.getString(s.getValue()));
}
}
//添加对象进集合
li.add(temp);
}
return li;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close(this.conn, this.ps, this.rs);
}
return null;
}
/**
*
* @Title: queryList
* @Description: TODO(通用查询方法)
* @param sql SQL语句
* @param params SQL语句中占位符的参数
* @param path 接受数据的对象完整路径名
* @param m 设置对象属性接受位置,key为属性名,value为获得数据位置
* @return
* @throws ClassNotFoundException List<T>
*/
public List<T> queryList(String sql, Object[] params,String path,Map<String, Integer> m) throws ClassNotFoundException {
//获得类对象
Class c=Class.forName(path);
List<T> li = new ArrayList<>();
try {
this.conn = getConn();
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
this.rs = ps.executeQuery();
while(this.rs.next()){
//实例化对象
T temp=(T)c.newInstance();
//通过Map.entrySet遍历key和value
for (Entry<String, Integer> s : m.entrySet()) {
String name = s.getKey();
Field field=c.getDeclaredField(name);
name = name.substring(0,1).toUpperCase()+name.substring(1);
Method method = c.getMethod("set"+name, field.getType());
try {
method.invoke(temp, rs.getObject(s.getValue()));
}catch (Exception e) {
//如果不是对应基本数据类型和引用数据类型,就设置为String类型
method.invoke(temp, rs.getString(s.getValue()));
}
}
//添加对象进集合
li.add(temp);
}
return li;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
close(this.conn, this.ps, this.rs);
}
return null;
}
/**
*
* @Title: executeUpdate
* @Description: TODO(增、删、改的通用方法)
* @param SQL语句
* @param params SQL语句中占位符的参数
* @return int
*/
public int executeUpdate(String sql, Object[] params) {
int result = -1;
try {
this.conn = getConn();
this.conn.setAutoCommit(false);
this.ps = this.conn.prepareStatement(sql);
//读参数
if(params != null) {
for (int i = 0; i < params.length; i++) {
this.ps.setObject(i+1, params[i]);
}
}
result = this.ps.executeUpdate();
this.conn.commit();
} catch (Exception e) {
// TODO: handle exception
try {
this.conn.rollback();
System.out.println("失败");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//throw new RuntimeException(e);
} finally {
close(this.conn, this.ps);
}
return result;
}
/**
*
* @Title: close
* @Description: TODO(查询关闭连接)
* @param con
* @param ps
* @param rs void
*/
private static void close(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(!con.isClosed()&&con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @Title: close
* @Description: TODO(增删改关闭连接)
* @param con
* @param ps void
*/
private static void close(Connection con,PreparedStatement ps) {
close(con,ps,null);
}
}
//-----------------------------测试
package com.ly.Book.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ly.Book.entity.Book;
import com.ly.dao.util.Dao;
/**
* @ClassName: TestBook
* @Description: TODO(Book测试)
* @author Uncle
* @date 2018年5月23日 下午3:21:20
*
*/
public class TestBook {
public static void main(String[] args) throws Exception {
Map<String, Integer> m = new HashMap<>();
m.put("bid", 1);
m.put("bname", 2);
m.put("bauthor", 3);
m.put("byear", 4);
m.put("bprice", 5);
Dao<Book> b = new Dao<>();
b.setConn(DBHelper.getCon());//数据库连接
List<Book> allBook = b.queryList("select * from tb_book ORDER BY byear DESC", null, new Book(), m);
for (Book book : allBook) {
System.out.println(book);
}
}
}
评论 (0)