1. DAO全称:Data Access Object , 数据访问对象。使用DAO设计模式来封装数据持久化层的所有操作(CRUD),使得数据访问逻辑和业务逻辑分离,实现解耦的目的。
2. 典型的DAO实现组件:DAO接口 + DAO接口的实现类
3. 事例代码
1)javabean组件:用于数据传输的对象
/** javabean数据传输对象 */ public class Customer { // 和数据库中的表是对应的 private int id; private String name; private String email; // 省略getter()/setter()方法 @Override public String toString() { return id + ", " + name + ", " + email; } }
2)DAO接口
/** DAO接口,用于定义application访问数据库的通用方法 */ public interface CustomerDemo { // CRUD public void add(Customer c); public void update(Customer c); public void delete(int id); public Customer getCustomerById(int id); public List<Customer> queryList(); }
3)DAO接口的实现类
1 package com.znker.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 // DAO接口的实现类 12 13 public class CustomerDaoImpl implements CustomerDemo { 14 15 @Override 16 public void add(Customer c) { 17 String sql = "insert into CustomerTb1(name,email) values(?,?)"; 18 // 获得一个数据库连接对象 19 Connection conn = DBUtil2.open(); 20 21 try { 22 // 预定义语句对象 23 PreparedStatement pstmt = conn.prepareStatement(sql); 24 // 对占位符进行赋值 25 pstmt.setString(1, c.getName()); 26 pstmt.setString(2, c.getEmail()); 27 // 执行添加操作 28 pstmt.executeUpdate(); 29 30 } catch (SQLException e) { 31 e.printStackTrace(); 32 } finally { 33 DBUtil2.close(conn); 34 } 35 } 36 37 @Override 38 public void update(Customer c) { 39 String sql = "update CustomerTb1 set name=?, email=? where id=?"; 40 Connection conn = DBUtil2.open(); 41 42 try { 43 PreparedStatement pstmt = conn.prepareStatement(sql); 44 pstmt.setInt(3, c.getId()); 45 pstmt.setString(1, c.getName()); 46 pstmt.setString(2, c.getEmail()); 47 48 pstmt.executeUpdate(); 49 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } finally { 53 DBUtil2.close(conn); 54 } 55 } 56 57 @Override 58 public void delete(int id) { 59 String sql = "delete from CustomerTb1 where id = ?"; 60 Connection conn = DBUtil2.open(); 61 62 try { 63 PreparedStatement pstmt = conn.prepareStatement(sql); 64 pstmt.setInt(1, id); 65 66 pstmt.executeUpdate(); 67 68 } catch (SQLException e) { 69 e.printStackTrace(); 70 } finally { 71 DBUtil2.close(conn); 72 } 73 } 74 75 @Override 76 public Customer getCustomerById(int id) { 77 String sql = "select id, name, email from CustomerTb1 where id = ?"; 78 Connection conn = DBUtil2.open(); 79 80 try { 81 PreparedStatement pstmt = conn.prepareStatement(sql); 82 pstmt.setInt(1, id); 83 84 ResultSet rs = pstmt.executeQuery(); 85 86 if (rs.next()) { 87 String name = rs.getString(2); 88 String email = rs.getString("email"); 89 90 Customer c = new Customer(); 91 c.setId(id); 92 c.setName(name); 93 c.setEmail(email); 94 95 return c; 96 } 97 } catch (SQLException e) { 98 e.printStackTrace(); 99 } 100 101 return null; 102 } 103 104 @Override 105 public List<Customer> queryList() { 106 String sql = "select id, name, email from CustomerTb1 "; 107 Connection conn = DBUtil2.open(); 108 109 try { 110 Statement stmt = conn.createStatement(); 111 ResultSet rs = stmt.executeQuery(sql); 112 List<Customer> list = new ArrayList<Customer>(); 113 114 while (rs.next()) { 115 int id = rs.getInt(1); 116 String name = rs.getString(2); 117 String email = rs.getString("email"); 118 119 Customer c = new Customer(); 120 c.setId(id); 121 c.setName(name); 122 c.setEmail(email); 123 124 list.add(c); 125 } 126 127 return list; 128 } catch (SQLException e) { 129 e.printStackTrace(); 130 } finally { 131 DBUtil2.close(conn); 132 } 133 134 return null; 135 } 136 137 }
4)测试代码
import java.util.ArrayList; import java.util.List; public class DaoTest { public static void main(String[] args) { // DAO接口的实现对象,用户业务层访问数据库 CustomerDemo dao = new CustomerDaoImpl(); // 添加一个用户 Customer c = new Customer(); c.setName("bobo"); c.setEmail("[email protected]"); dao.add(c); // 查询一个用户List List<Customer> list = new ArrayList<Customer>(); list = dao.queryList(); System.out.println(list); } }
时间: 2024-10-11 22:41:22