okx

DAO通用CRUD方法

时间:2023-06-12|浏览:181

DAO和增删改查通用方法

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 getAll() throws Exception;

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 getAll() throws Exception; Long getCount() throws Exception; List getAll(int page, int pageSize) throws Exception; Double getMaxSalary() throws Exception; Map getAvgSalaryByDid() throws Exception;

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 getAll() throws Exception { Connection conn = JDBCUtils.getConnection(); String sql = "SELECT did, dname, description FROM t_department"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); ArrayList list = new ArrayList(); while (rs.next()) { Department dept = new Department(); dept.setId(rs.getInt("did")); dept.setName(rs.getString("dname")); dept.setDescription(rs.getString("description")); list.add(dept); } JDBCUtils.closeQuietly(rs, pst, conn); return 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 { private Class type;

@SuppressWarnings("all") protected BasicDAOImpl() { // 为什么要在构造器中写,因为子类继承BasicDAOImpl类一定会调用父类的构造器 Class clazz =

« 上一条| 下一条 »
区块链交流群
数藏交流群

合作伙伴

币圈ICO官网 茶百科 二手域名 聚币网 借春秋 宠物丫 装修装饰网 非小号行情 币圈交流群 币圈官网 百悦米 币爸爸 兼职信息网 數字黃金 谷歌留痕 宝宝起名 玩票票财经 妈妈知道 减肥瘦身吧 爱网站 旅游资讯网 谈股票 天天财富 趣开心资讯 培训资讯网 佩佩蛙官网 今日黄金 皮卡丘资讯 周公解梦 免费电影 起名取名网 趣玩币 百科书库 玩合约 黄金行情 元宇宙Web 代特币圈 金色币圈 秒懂域名 去玩呗SPA 美白没斑啦 数字财经 借春秋财经 币圈论坛
在区块链世界中,智能合约不仅是代码的信任,更是商业的革命。通过了解其在供应链、版权保护等领域的应用,您将真正体验到智能合约的无限可能性
区块链世界GxPiKaQiu.com ©2020-2024版权所有 桂ICP备16002597号-2