JDBC应用中对数据库对象记录的映射JavaBean及增删改查(CRUD)的封装DAO
应用程序中使用数据访问对象(DAO)使我们可以将底层数据访问逻辑与业务逻辑分离开来。构建了为每一个数据源提供 CRUD (创建、读取、更新、删除)操作的 DAO 类。
Dao其实一般没有这个类,一般是指java中MVC架构中的model的概念,主要是访问数据库的一些常用方法。
相关概念:
JavaBean
满足如下规范的类: - 有package - 有无参构造器 - 实现序列化接口 - 有get/set方法
DAO
下面通过简单实例演示如何封装常用数据库操作类Dao
项目目录结构如下图
1.编写实体类:Emp
/** * 1.通常实体类和表名一致 * 2.通常该类中属性名和字段名一致 * 3.通常该类中属性都使用封装类型 * @author Cher_du * */ public class Emp implements Serializable{ private static final long serialVersionUID = 1L; private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer 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 Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } 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; } }
2.编写数据库操作类EmpDao:【封装CURD】
public class EmpDao { //查询所有 public List<Emp> findAll(){ Connection conn = null; List<Emp> empList = new ArrayList<Emp>(); try { conn = DBUtil.getConnection(); String sql = "select * from emp "; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()){ Emp e =new Emp(); e.setEmpno(rs.getInt("empno")); e.setEname(rs.getString("ename")); e.setJob(rs.getString("job")); e.setMgr(rs.getInt("mgr")); e.setHiredate(rs.getDate("hiredate")); e.setSal(rs.getDouble("sal")); e.setComm(rs.getDouble("comm")); e.setDeptno(rs.getInt("deptno")); empList.add(e); } return empList; } catch (SQLException e) { e.printStackTrace(); } return empList; } public Emp findById(int id){ Connection conn =null; try { conn = DBUtil.getConnection(); String sql = "select * from emp " +"where empno =?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if(rs.next()){ Emp e =new Emp(); e.setEmpno(rs.getInt("empno")); e.setEname(rs.getString("ename")); e.setJob(rs.getString("job")); e.setMgr(rs.getInt("mgr")); e.setHiredate(rs.getDate("hiredate")); e.setSal(rs.getDouble("sal")); e.setComm(rs.getDouble("comm")); e.setDeptno(rs.getInt("deptno")); return e; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("根据ID查询员工失败!",e); }finally{ DBUtil.close(conn); } return null; } public boolean save(Emp emp){ Connection conn = null; try { conn = DBUtil.getConnection(); String sql = "insert into emp values(" +"?,?,?,?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, emp.getEmpno()); ps.setString(2, emp.getEname()); ps.setString(3, emp.getJob()); ps.setInt(4, emp.getMgr()); ps.setDate(5, emp.getHiredate()); ps.setDouble(6,emp.getSal()); ps.setDouble(7, emp.getComm()); ps.setInt(8, emp.getDeptno()); int count = ps.executeUpdate(); if(count>0){ return true; }else{ return false; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("保存员工失败!",e); }finally{ DBUtil.close(conn); } } public void update(Emp emp){ Connection conn = null; try { conn = DBUtil.getConnection(); String sql = "update emp set " +"ename =?," +"job =?," +"mgr =?," +"hiredate =?," +"sal =?," +"comm =?," +"deptno =? " +"where empno=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, emp.getEname()); ps.setString(2, emp.getJob()); ps.setInt(3, emp.getMgr()); ps.setDate(4, emp.getHiredate()); ps.setDouble(5,emp.getSal()); ps.setDouble(6, emp.getComm()); ps.setInt(7, emp.getDeptno()); ps.setInt(8, emp.getEmpno()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("修改员工失败!",e); }finally{ DBUtil.close(conn); } } public boolean delete(int id){ Connection conn =null; try { conn = DBUtil.getConnection(); String sql = "delete from emp " +"where empno =?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, id); int count = ps.executeUpdate(); if(count>0){ return true; }else{ return false; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("根据ID删除员工失败!",e); }finally{ DBUtil.close(conn); } } }
3.编写EmpDao测试类
public class EmpDaoTest { public static void main(String[] args) { // EmpDao dao = new EmpDao(); // Emp emp = new Emp(); // emp.setEmpno(1000); // emp.setEname("Jimmy"); // emp.setJob("经理"); // emp.setHiredate(new Date(System.currentTimeMillis())); // emp.setSal(10000.00); // emp.setComm(2000.00); // emp.setDeptno(20); // emp.setMgr(45); // // boolean b = dao.save(emp); // System.out.println(b); // EmpDao dao = new EmpDao(); // Emp emp = new Emp(); // emp.setEmpno(1000); // emp.setEname("Jimmy"); // emp.setJob("高级经理"); // emp.setHiredate(new Date(System.currentTimeMillis())); // emp.setSal(10000.00); // emp.setComm(2000.00); // emp.setDeptno(20); // emp.setMgr(45); // // dao.update(emp); // EmpDao dao = new EmpDao(); // Emp emp = new Emp(); // // emp =dao.findById(1000); // System.out.println(emp.getEname()); // EmpDao dao = new EmpDao(); // Emp emp = new Emp(); // // boolean b =dao.delete(1000); // System.out.println(b); EmpDao dao = new EmpDao(); List<Emp> empList =dao.findAll(); for(Emp e :empList){ System.out.println(e.getEname()); } } }
总结: DAO常用于MVC开发模式中
DAO层一般有接口和该接口的实现类! 接口用于规范实现类! 实现类一般用于用于操作数据库! 一般操作修改,添加,删除数据库操作的步骤很相似,就写了一个公共类DAO类 ,修改,添加,删除数据库操作时 直接调用公共类DAO类