后端开发DAO及其相关类,DAO的优化
Posted cls1277
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后端开发DAO及其相关类,DAO的优化相关的知识,希望对你有一定的参考价值。
优化前
BaseDAO.java
package com.cls1277.dao;
import com.cls1277.utils.JDBCutils;
import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//抽象类
public abstract class BaseDAO
public void 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]);
ps.execute();
catch (Exception e)
e.printStackTrace();
finally
JDBCutils.closeResource(null, ps);
public <T> T getInstance1(Connection conn, Class<T> clazz, 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 <T> List<T> getInstance2(Connection conn, Class<T> clazz, 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> ts = 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);
ts.add(t);
return ts;
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 (Exception e)
e.printStackTrace();
finally
JDBCutils.closeResource(null, ps, rs);
return null;
// @Test
// public void getValueTest() throws SQLException, IOException, ClassNotFoundException
// Connection conn = JDBCutils.getConnection();
// String sql = "select count(*) from user_table";
// long ans = getValue(conn, sql);
// System.out.println(ans);
//
CustomerDAO.java(接口)
package com.cls1277.dao;
import com.cls1277.preparedstatement.customer.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
//接口:规范针对于customers表的操作
public interface CustomerDAO
//加文档注释最好
void insert(Connection conn, Customer customer);
void delete(Connection conn, int id);
void update(Connection conn, Customer customer);
Customer getCustomer(Connection conn, int id);
List<Customer> getAll(Connection conn);
Long getCount(Connection conn);
Date getMaxDate(Connection conn);
CustomerDAOImpl(CustomerDAO的实现)
package com.cls1277.dao;
import com.cls1277.preparedstatement.customer.Customer;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
public class CustomerDAOImpl extends BaseDAO implements CustomerDAO
@Override
public void insert(Connection conn, Customer customer)
String sql = "insert into customers(name, email, birth)value(?,?,?)";
update(conn, sql, customer.getName(), customer.getEmail(), customer.getBirth());
@Override
public void delete(Connection conn, int id)
String sql = "delete from customers where id = ?";
update(conn, sql, id);
@Override
public void update(Connection conn, Customer customer)
String sql = "update customers set name = ?, email = ?, birth = ? where id = ?";
update(conn, sql, customer.getName(), customer.getEmail(), customer.getBirth(), customer.getId());
@Override
public Customer getCustomer(Connection conn, int id)
String sql = "select id,name,email,birth from customers where id = ?";
return getInstance1(conn, Customer.class, sql, id);
@Override
public List<Customer> getAll(Connection conn)
String sql = "select id,name,email,birth from customers";
return getInstance2(conn, Customer.class, sql);
@Override
public Long getCount(Connection conn)
String sql = "select count(*) from customers";
return getValue(conn, sql);
@Override
public Date getMaxDate(Connection conn)
String sql = "select max(birth) from customers";
return getValue(conn, sql);
优化后
优化的地方在于:因为我的这个DAO是针对customers表的,所以就没必要在代码中再获取一下Customer这个类,只需要在BaseDAO里使用反射的方法获取到当前子类的父类的泛型即可。
BaseDAO.java
package com.cls1277.daoplus;
import com.cls1277.utils.JDBCutils;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
//抽象类
public abstract class BaseDAO<T>
private Class<T> clazz = null;
// public BaseDAO()
//
//
//获取当前子类继承的父类的泛型
//如何理解?https://blog.csdn.net/qq_26424655/article/details/71515741
Type genericSuperclass = this.getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = paramType.getActualTypeArguments();
clazz = (Class<T>) actualTypeArguments[0];
public void 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]);
ps.execute();
catch (Exception e)
e.printStackTrace();
finally
JDBCutils.closeResource(null, ps);
public T getInstance1(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 List<T> getInstance2(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> ts = new ArrayList<>();
while(rs.next())
T t = clazz.newInstance();
for(int i=0; i<columnCount; i++) 后端开发DAO及其相关类,DAO的优化