实体:(省略setter,getter)
public class Customer {
private int id;
private String name;
private String address;
private String phone;
}
JDBC工具类
/*
* JDBC操作的工具类
*/
public class JdbcUtils {
public static void releaseConnection(Connection connection){
try {
if(connection != null){
connection.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private static DataSource dataSource =null;
//只被执行一次
static{
//数据源只能被创建一次
dataSource = new ComboPooledDataSource("mvcapp");
}
/*
* 返回数据源的Connection对象
*/
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
DAO基类
/*
* 封装了基本的CRUD的方法,以供子类继承使用
* 当前DAO直接在方法中获取数据库连接
* 整个DAO采用DBUtils解决方案
* <T>:当前DAO处理的实体的类型是什么
*/
public class DAO<T> {
private QueryRunner queryRunner = new QueryRunner();
private Class<T> clazz;
public DAO() {
// TODO Auto-generated constructor stub
Type superClass = getClass().getGenericSuperclass();
if(superClass instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType)superClass;
Type [] typesArhs = parameterizedType.getActualTypeArguments();
if(typesArhs != null && typesArhs.length > 0){
if(typesArhs[0] instanceof Class){
clazz = (Class<T>) typesArhs[0];
}
}
}
}
/*
* 返回某一个字段的值,返回某一条记录的customerName的值
*
*/
public <E> E getForValue(String sql,Object ...args)
{
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return (E)queryRunner.query(connection, sql, new ScalarHandler(),args);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
JdbcUtils.releaseConnection(connection);
}
System.out.print(clazz);
return null;
}
/*
* 返回T所对应的List
*/
public List<T> getForList(String sql,Object ...args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanListHandler(clazz), args);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
JdbcUtils.releaseConnection(connection);
}
System.out.print(clazz);
return null;
}
/*
* 返回对应的T的一个实体类对象
*
*/
public T get(String sql,Object ...args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanHandler<>(clazz), args);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
JdbcUtils.releaseConnection(connection);
System.out.println("aa: "+clazz);
}
return null;
}
/*
* 该方法封装了insert、delete、update操作
* sql:SQL语句
* args:sql语句的占位符
*/
public Boolean sava(String sql, Object ... args){
Boolean result = false;
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
queryRunner.update(connection, sql, args);
result = true;
return result;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
JdbcUtils.releaseConnection(connection);
}
return result;
}
}
DAO的使用
public class CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO{
@Override
public List<Customer> getALl() {
// TODO Auto-generated method stub
String sql = "select id,name, address,phone from customers";
return getForList(sql);
}
@Override
public List<Customer> getForListWith(CriteraCustomer cc) {
// TODO Auto-generated method stub
String sql = "select id,name, address,phone from customers where name like ? and address like ? and phone like ?";
return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
}
@Override
public Boolean sava(Customer customer) {
// TODO Auto-generated method stub
String sql = "insert into customers(name,address,phone) values(?,?,?)";
Boolean result = sava(sql, customer.getName(),customer.getAddress(),customer.getPhone());
return result;
}
@Override
public Customer get(Integer id) {
// TODO Auto-generated method stub
String sql = "select id,name, address, phone from customers where id = ?";
return get(sql, id);
}
@Override
public void delet(String name) {
Boolean result;
// TODO Auto-generated method stub
String sql = "delete from customers where name = ?";
sava(sql,name);
}
@Override
public long getCountWithName(String name) {
// TODO Auto-generated method stub
String sql = "select count(id) from customers where name= ?";
return getForValue(sql, name);
}
@Override
public void updata(Customer customer) {
// TODO Auto-generated method stub
String sql = "update customers set name = ?, address = ?, phone = ? where id = ?";
sava(sql,customer.getName(),customer.getAddress(),customer.getPhone(),customer.getId());
}
}