DAO
引导
-
DAO的思想在于由通用方法组成的BaseDAO、具体类的接口及实现类,形成一套调用接口 -
通过反射及泛型的应用,对BaseDAO进行优化。 反射优化部分参考Java六十九: 反射
一、概述
- DAO:Data Access Object,数据访问的类或接口,包括了对数据的CRUD,而不包含任何业务相关的信息,也称作:BaseDAO
- 作用:为了实现功能的模块化,更有利于代码的维护和升级
二、具体表的DAO
-
BaseDAO 里面放增删改查的通用方法 package optimization;
import bean.Customers;
import jdbcutils.JdbcUtils;
import java.lang.reflect.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseDAO<T> {
private Class<T> clazz = null;
{
Type genericSuperclass = this.getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
Type[] typeArguments = paramType.getActualTypeArguments();
clazz = (Class<T>) typeArguments[0];
}
public int update(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
return ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JdbcUtils.closeResource(null,ps);
}
return 0;
}
public List<T> CommonRetrieve(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ArrayList<T> tSet = new ArrayList<>();
while (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String columnLabel = rsmd.getColumnLabel(i+1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t,columnValue);
}
tSet.add(t);
}
return tSet;
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(null,ps,rs);
}
return null;
}
public T singleCommonRetrieve(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String columnLabel = rsmd.getColumnLabel(i+1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t,columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeResource(null,ps,rs);
}
return null;
}
public <E> E getValue(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.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 (SQLException throwables) {
throwables.printStackTrace();
} finally {
JdbcUtils.closeResource(null,ps,rs);
}
return null;
}
}
-
接口 从逻辑上,为了使代码的格式更加规范,增加一个接口,用于规范针对该表的常用操作 package optimization;
import bean.Customers;
import java.sql.Connection;
import java.util.Date;
import java.util.List;
public interface CustomerDAO {
void insert(Connection conn, Customers cust);
void deleteById(Connection conn, int id);
void updateById(Connection conn, Customers cust);
Customers getCustomerById(Connection conn, int id);
List<Customers> getAll(Connection conn);
Long getCount(Connection conn);
Date getMaxBirth(Connection conn);
}
-
实现类 继承BaseDAO,实现该接口,通过调用BaseDAO中的通用方法,实现接口中的抽象方法 package optimization;
import bean.Customers;
import java.sql.Connection;
import java.util.Date;
import java.util.List;
public class CustomerDAOImpl extends BaseDAO<Customers> implements CustomerDAO {
@Override
public void insert(Connection conn, Customers cust) {
String sql = "insert into customers(name,email,birth) values(?,?,?)";
update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth());
}
@Override
public void deleteById(Connection conn, int id) {
String sql = "delete from customers where id = ?";
update(conn,sql,id);
}
@Override
public void updateById(Connection conn, Customers cust) {
String sql = "update customers set name = ?, email = ?, birth = ? where id = ?";
update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth(),cust.getId());
}
@Override
public Customers getCustomerById(Connection conn, int id) {
String sql = "select name,email,birth,id from customers where id = ?";
return singleCommonRetrieve(conn,sql,id);
}
@Override
public List<Customers> getAll(Connection conn) {
String sql = "select name,email,birth,id from customers";
return CommonRetrieve(conn,sql);
}
@Override
public Long getCount(Connection conn) {
String sql = "select count(1) from customers ";
return getValue(conn,sql);
}
@Override
public Date getMaxBirth(Connection conn) {
String sql = "select min(birth) from customers";
return getValue(conn,sql);
}
}
三、优化
-
具体的实现类中都非常明确是操作的哪个表,对应操作类也非常明确,故在实现类中的各个重写方法中的Customer.class参数也可去除,可通过在BaseDAO中增加泛型来解决。在实现类继承BaseDAO后,就指明其泛型为Customer,即可对实现类进行优化 注:BaseDAO类中的方法存在于该博文中 - - JDBC三:使用PreparedStatement实现CRUD -
在BaseDAO增加泛型后,可将泛型对象 clazz在代码块中进行初始化,从而可将类内各通用方法的参数列表中的Class clazz去除掉,但方法内的clazz仍然可用。
|