DAO设计模式是一个javaEE里的设计模式,DAO是Data Access Object 数据访问接口。
一个典型的DAO实现有三个组件:
1、一个DAO接口
2、一个DAO接口的具体类;
3、数据传递对象(DTO)或者叫值对象(VO)或者叫领域模型(domain)【个人认为就是数据库表里对应的java对象类】
举个例子:
首先在数据库建一个表Person有四个字段id(pk),name,age,desc(描述);
然后建立domain
1 package com.gh.domain; 2 3 public class Person { 4 private int id; 5 private String name; 6 private int age; 7 private String desc; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 public String getDesc() { 27 return desc; 28 } 29 public void setDesc(String desc) { 30 this.desc = desc; 31 } 32 public Person(String name, int age, String desc) { 33 super(); 34 this.name = name; 35 this.age = age; 36 this.desc = desc; 37 } 38 public Person() { 39 super(); 40 } 41 //本来id在数据库里想弄成自动增长不用传的结果oracle里比mysql麻烦些就传了id 42 public Person(int id, String name, int age, String desc) { 43 super(); 44 this.id = id; 45 this.name = name; 46 this.age = age; 47 this.desc = desc; 48 } 49 @Override 50 public String toString() { 51 return "Person [id=" + id + ", name=" + name + ", age=" + age 52 + ", desc=" + desc + "]"; 53 } 54 }
然后写Person的Dao接口
1 package com.gh.Dao; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.gh.domain.Person; 7 /** 8 * Person类的DAO接口 9 * @author ganhang 10 * 11 */ 12 public interface PersonDao{ 13 //添加 14 public void add(Person p) throws SQLException; 15 //更新 16 public void update(Person p)throws SQLException; 17 //删除 18 public void delete(int id)throws SQLException; 19 //通过id查person 20 public Person findById(int id)throws SQLException; 21 //查找所以Person 22 public List<Person> findAll()throws SQLException; 23 24 }
然后Dao接口的实现类
1 package com.gh.Dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.List; 10 11 import javax.sound.sampled.Line; 12 13 import com.gh.Dao.PersonDao; 14 import com.gh.db.JdbcUtil; 15 import com.gh.domain.Person; 16 17 /** 18 * PersonDao接口的实现 19 * 20 * @author ganhang 21 * 22 */ 23 public class PersonDaoImpl implements PersonDao { 24 //添加 25 @Override 26 public void add(Person p) throws SQLException { 27 // oracle 没有主键自动增长 必须用序列和触发器,这边就直接传id了。 28 String sql = "insert into Person(id,name,age,description) values(?,?,?,?)"; 29 //这里做了优化,写个模版类处理所以更删改操作 30 JdbcTemplete.Update(sql, p.getId(), p.getName(), p.getAge(), 31 p.getDesc()); 32 } 33 //更新 34 @Override 35 public void update(Person p) throws SQLException { 36 String sql = "update Person set name=?,age=?,description=? where id=?"; 37 JdbcTemplete.Update(sql, p.getName(), p.getAge(), p.getDesc(), 38 p.getId()); 39 } 40 //删除 41 @Override 42 public void delete(int id) throws SQLException { 43 String sql = "delete Person where id=?"; 44 JdbcTemplete.Update(sql, id); 45 46 } 47 //通过id查询 48 @Override 49 public Person findById(int id) throws SQLException { 50 String sql = "select * from Person where id=?"; 51 //由于JdbcTemplete封装了查询方法,每个处理结果的方法不同这里用了策略模式 52 return (Person) JdbcTemplete.Query(sql, new ResultSetHandler() { 53 @Override 54 public Object doHandler(ResultSet rs) throws SQLException { 55 Person p = null; 56 // 这里只查询一个person 57 if (rs.next()) { 58 p = new Person(); 59 p.setId(rs.getInt(1)); 60 p.setName(rs.getString(2)); 61 p.setAge(rs.getInt(3)); 62 p.setDesc(rs.getString(4)); 63 } 64 return p; 65 } 66 }, id); 67 } 68 //查询所有 69 @SuppressWarnings("unchecked") 70 @Override 71 public List<Person> findAll() throws SQLException { 72 String sql = "select * from Person"; 73 return (List<Person>) JdbcTemplete.Query(sql, new ResultSetHandler() { 74 @Override 75 public Object doHandler(ResultSet rs) throws SQLException { 76 List<Person> Persons = new ArrayList<Person>(); 77 Person p = null; 78 while (rs.next()) { 79 p = new Person(); 80 p.setId(rs.getInt(1)); 81 p.setName(rs.getString(2)); 82 p.setAge(rs.getInt(3)); 83 p.setDesc(rs.getString(4)); 84 Persons.add(p); 85 } 86 return Persons; 87 } 88 }); 89 } 90 91 }
JdbcTemplete的实现
1 package com.gh.Dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import com.gh.db.JdbcUtil; 9 /** 10 * PersonDaoImpl类的模板类 11 * @author ganhang 12 * 13 */ 14 public class JdbcTemplete { 15 /** 16 * 更新方法(包括更、删、改) 17 * @param sql sql语句 18 * @param args 占位符参数 19 */ 20 public static void Update(String sql, Object... args)throws SQLException { 21 Connection conn = JdbcUtil.getConnection(); 22 try { 23 //设置占位符的参数 24 PreparedStatement ps = conn.prepareStatement(sql); 25 if (args != null) { 26 for (int i = 0; i < args.length; i++) { 27 ps.setObject(i + 1, args[i]); 28 } 29 } 30 ps.executeUpdate(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 /** 36 * 查询方法 37 * @param sql sql语句 38 * @param handler 结果集的处理的接口 39 * @param args 40 * @return 41 */ 42 public static Object Query(String sql,ResultSetHandler handler,Object...args)throws SQLException{ 43 Connection conn=JdbcUtil.getConnection(); 44 PreparedStatement ps=null; 45 ResultSet rs=null; 46 try { 47 ps = conn.prepareStatement(sql); 48 if(args!=null){ 49 for(int i=0;i<args.length;i++){ 50 ps.setObject(i+1, args[i]); 51 } 52 } 53 rs=ps.executeQuery(); 54 return handler.doHandler(rs); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 return rs; 59 } 60 }
ResultSetHandler的实现
1 package com.gh.Dao.impl; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 /** 6 * 结果集处理接口 7 * @author ganhang 8 * 9 */ 10 public interface ResultSetHandler { 11 public Object doHandler(ResultSet rs)throws SQLException; 12 }
这样对于Person表的增删改查就实现了。
时间: 2024-10-07 05:51:16