時間:2023-06-12|瀏覽:260
DAO:是指DataAccessObject,即訪問數據信息的類和接口。它包括了對數據的CRUD(Create、Retrival、Update、Delete),但不包含任何業務相關的信息。
它的作用是為了實現功能的模塊化,從而更易于代碼的維護和升級。
1.1 表和 JavaBean 1.2 DAO 接口
DepartmentDAO:
void addDepartment(Department department) throws Exception;
void updateDepartment(Department department) throws Exception;
void deleteById(String did) throws Exception;
Department getById(String did) throws Exception;
List EmployeeDAO: void addEmployee(Employee emp) throws Exception;
void updateEmployee(Employee emp) throws Exception;
void deleteById(String eid) throws Exception;
Employee getById(String eid) throws Exception;
List 1.3 DAO 實現類(1)原生版 DepartmentDAOImpl: public class DepartmentDAOImpl implements DepartmentDAO {
@Override
public void addDepartment(Department department) throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "INSERT INTO t_department (did, dname, description) VALUES (NULL, ?, ?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, department.getName());
pst.setString(2, department.getDescription());
pst.executeUpdate();
JDBCUtils.closeQuietly(pst, conn);
} @Override
public void updateDepartment(Department department) throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "UPDATE t_department SET dname = ?, description = ? WHERE did = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, department.getName());
pst.setString(2, department.getDescription());
pst.setInt(3, department.getId());
pst.executeUpdate();
JDBCUtils.closeQuietly(pst, conn);
} @Override
public void deleteById(String did) throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "DELETE FROM t_department WHERE did = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, did);
pst.executeUpdate();
JDBCUtils.closeQuietly(pst, conn);
} @Override
public Department getById(String did) throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "SELECT did, dname, description FROM t_department WHERE did = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, did);
ResultSet rs = pst.executeQuery();
Department dept = null;
if (rs.next()) {
dept = new Department();
dept.setId(rs.getInt("did"));
dept.setName(rs.getString("dname"));
dept.setDescription(rs.getString("description"));
}
JDBCUtils.closeQuietly(rs, pst, conn);
return dept;
} @Override
public List 1.4 抽取 BasicDAO BasicDAOImpl: /**
* 這個類的作用是:對DAOImpl再次抽象,把共同的部分再次抽取
*/
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.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.atguigu.utils.JDBCUtils; // 泛型類
public abstract class BasicDAOImpl @SuppressWarnings("all")
protected BasicDAOImpl() {
// 為什么要在構造器中寫,因為子類繼承BasicDAOImpl類一定會調用父類的構造器
Class> clazz =