package com.oracle.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.mysql.jdbc.JDBC4DatabaseMetaDataUsingInfoSchema;
import com.oracle.utils.JDBCUtils;
public class BaseDao<T> {
public QueryRunner qr=new QueryRunner();
private Class<T> clazz;
public BaseDao(){
Type type=this.getClass().getGenericSuperclass();
if(type instanceof ParameterizedType){
ParameterizedType pt=(ParameterizedType)type;
Type[] array=pt.getActualTypeArguments();
if(array[0] instanceof Class){
clazz=(Class<T>)array[0];
}
}
}
/*
* 非事务处理查询返回一个对象
*/
public T get(String sql,Object...args){
Connection conn=null;
try {
conn=JDBCUtils.getconnection();
return qr.query(conn, sql, new BeanHandler<>(clazz), args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.close(conn);
}
return null;
}
/*非事务处理返回一个列表
*
*/
public List<T> getList(String sql,Object...args){
List<T> T=null;
Connection conn=null;
try {
conn=JDBCUtils.getconnection();
T=qr.query(conn, sql, new BeanListHandler<>(clazz), args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.close(conn);
}
return T;
}
/*
* 非事务处理更新
*/
public int update(String sql,Object...args){
Connection conn=null;
conn=JDBCUtils.getconnection();
int i=0;
try {
i = qr.update(conn, sql, args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.close(conn);
}
return i;
}
/*
* 非事务处理返回查询到的只有一个值的
*/
public Object getValue(String sql,Object...args){
Connection conn=null;
QueryRunner q=new QueryRunner();
try {
conn=JDBCUtils.getconnection();
return qr.query(conn, sql, new ScalarHandler(),args);
} catch (Exception e) {
// TODO: handle exception
}finally{
JDBCUtils.close(conn);
}
return null;
}
/*
* 事务处理,查询一个对象
*/
public T get(Connection conn,String sql,Object...args){
T entity=null;
try {
entity=qr.query(conn, sql, new BeanHandler<>(clazz), args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return entity;
}
/*
* 事务处理,查询一个列表
*/
public List<T> getList(Connection conn,String sql,Object...args){
List<T> t=null;
try {
t=qr.query(conn, sql, new BeanListHandler<>(clazz), args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
/*
* 事务处理更新
*/
public int update(Connection conn,String sql,Object...args){
int i=0;
try {
i = qr.update(conn, sql, args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
/*
* 事务处理,获得object对象
*/
public Object getValue(Connection conn,String sql,Object...args){
Object value=null;
try {
value = qr.query(conn, sql, new ScalarHandler(), args);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
}