数据访问对象层Dao的设计

    xiaoxiao2025-05-17  2

    在基于MVC模式的框架中,Dao层处理的是访问数据库操作,本文以访问Emp表为例 首先在bean包下创建与数据库中emp表对应的实体类Emp

    public class Emp implements Serializable{ private static final long serialVersionUID = 456852456852L; @Column(values = "empno") private Integer empno; @Column(values = "ename") private String ename; @Column(values = "job") private String job; @Column(values = "sal") private Double sal; @Column(values = "hiredate") private Date hiredate; @Column(values = "mgr") private Integer mgr; @Column(values = "comm") private Double comm; @Column(values = "deptno") private Integer deptno; public Emp() { super(); } public Emp(Integer empno,String ename, String job, Double sal, Date hiredate, Integer mgr, Double comm, Integer deptno) { super(); this.empno=empno; this.ename = ename; this.job = job; this.sal = sal; this.hiredate = hiredate; this.mgr = mgr; this.comm = comm; this.deptno = deptno; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } @Override public String toString() { return "Emp [empno=" + empno +",ename=" + ename + ", job=" + job + ", sal=" + sal + ", hiredate=" + hiredate + ", mgr=" + mgr + ", comm=" + comm + ", deptno=" + deptno + "]"; } }

    在dao包下定义所有访问数据库的接口的父接口

    public interface IDao { public <T> List<T> query(); public <T> int insert(T t); public <T> int update(T t); public int delete(int id); }

    在dao包下定义访问Emp表的接口

    public interface IEmpDao { public <Emp> List<Emp> query(); public <Emp> int insert(Emp e); public <Emp> int update(Emp e); public int delete(int id); }

    为该接口定义实现类

    public class EmpDaoImpl implements IEmpDao { private String sql; @Override public List<Emp> query() { List<Emp> empList = new ArrayList<Emp>(); sql = "SELECT * FROM Emp"; try { empList = DBUtil.selectList(JDBCUtil.getConnection(), sql, Emp.class); } catch (Exception e) { e.printStackTrace(); } return empList; } @Override public int insert(Emp t) { sql = "INSERT INTO Emp(id,sname,age,classid) VALUES(?,?,?,?)"; try { Connection con = JDBCUtil.getConnection(); return DBUtil.add(con, sql, t,false); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 根据对象的id更新对象属性 */ @Override public int update(Emp t) { sql = "UPDATE Emp SET sname=?,age=?,classid=? WHERE id=?"; try{ Connection con = JDBCUtil.getConnection(); return DBUtil.edit(con, sql, t); }catch (Exception e) { e.printStackTrace(); } return 0; } @Override public int delete(int id) { sql = "DELETE FROM emp WHERE id=?"; try{ Connection con = JDBCUtil.getConnection(); return DBUtil.remove(con, sql, id); }catch (Exception e) { e.printStackTrace(); } return 0; } }
    最新回复(0)