国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java開發———DAO設計模式及優化

2019-11-14 09:46:36
字體:
來源:轉載
供稿:網友

什么是DAO?

DAO是Data access Object 數據訪問接口,顧名思義:就是與數據庫打交道。夾在業務邏輯與數據資源的中間。

DAO模式有哪些?

DAO模式實際上是兩個模式的組合 既Data Accessor 模式和Active Domain Object模式。

它們的意義和作用

Data Accessor模式實現了數據訪問和業務邏輯的分離,Active Domain Object 模式實現了業務數據的對象化封裝。 以javaWeb中對數據庫的為例: 首先預先準備一個工具類DBUtils.class

public class DBUtils { public final static String URL="jdbc:MySQL://localhost:3306/mydb"; public final static String USERNAME="root";//用戶名 public final static String PASSWord="*****";//密碼 public final static String DRIVER="com.mysql.jdbc.Driver"; PRivate DBUtils(){ } //使用靜態快加載驅動 static{ try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //定義一個獲取數據庫連接的方法 public static Connection getconnection() { Connection connection=null; try { connection=DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("獲取連接失敗"); } return connection; } public static void close(ResultSet rs,Statement statement,Connection conn){ if (rs!=null) try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (statement!=null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}

接下來我們要來實現Dao模式

一個典型的DAO模式有以下幾個組件:一個接口,數據傳遞對象或者領域模型, 先建立一個類 person.class 數據模型類

public class Person { private int id; private String name; private int age; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Person( String name, int age, String description) { super(); this.name = name; this.age = age; this.description = description; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "person [id=" + id + ", name=" + name + ", age=" + age + ", description=" + description + "]"; }

聲明一個接口,數據操作類 PersonDao.class

import java.sql.SQLException; import java.util.List; import com.vince.domain.Person; public interface PersonDao { public void add(Person p) throws SQLException;//這里需要拋出異常;否則在下面操作的時候會報錯!!!! public void update(Person p) throws SQLException; public void delete(int id) throws SQLException; public Person findById(int id) throws SQLException; public List<Person> findAll() throws SQLException; }

編寫接口的實現類 PersonDanImpl.class

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.util.db.DBUtils;import com.util.db.JdbcTemplete;import com.vince.base.ResultSetHandler;import com.vince.dao.PersonDao;import com.vince.domain.Person;public class PersonDaoImpl implements PersonDao {private JdbcTemplete jdbcTemolete; public PersonDaoImpl() { jdbcTemolete=new JdbcTemplete();} @Override public void add(Person p) throws SQLException { Connection connection=null; PreparedStatement ps=null; String sql="insert into person(name,age,description)values(?,?,?)"; try { connection=DBUtils.getconnection(); ps=connection.prepareStatement(sql); ps.setString(1, p.getName()); ps.setInt(2, p.getAge()); ps.setString(3, p.getDescription()); ps.executeUpdate(); } catch (SQLException e) { throw new SQLException("數據庫添加異常"); }finally { DBUtils.close(null, ps, connection); } }@Override public void update(Person p) throws SQLException { Connection connection=null; PreparedStatement ps=null; String sql="update person set name=?,age=?,description=? where id=?"; try { connection=DBUtils.getconnection(); ps=connection.prepareStatement(sql); ps.setString(1, p.getName()); ps.setInt(2, p.getAge()); ps.setString(3, p.getDescription()); ps.setInt(4, p.getId()); ps.executeUpdate(); } catch (SQLException e) { throw new SQLException("數據庫更新異常"); }finally { DBUtils.close(null, ps, connection); } } @Override public void delete(int id) throws SQLException { Connection connection=null; PreparedStatement ps=null; String sql="delete from person where id=?"; try { connection=DBUtils.getconnection(); ps.setInt(1,id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new SQLException("數據庫刪除異常"); }finally { DBUtils.close(null, ps, connection); } }@Override public List<Person> findAll() throws SQLException { Connection connection=null; PreparedStatement ps=null; ResultSet rs=null; Person p=null; List<Person> persons=new ArrayList<Person>(); String sql="selectid,name,age,description from person"; try { connection=DBUtils.getconnection(); ps=connection.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ p=new Person(); p.setId(rs.getInt(1)); p.setName(rs.getString(2)); p.setAge(rs.getInt(3)); p.setDescription(rs.getString(4)); persons.add(p); } } catch (SQLException e) { // TODO: handle exception throw new SQLException("查詢所有數據異常"); }finally { DBUtils.close(rs, ps, connection); } return persons; }}

上面就是DAO模式的實現過程。這種DAO設計模式主要是可以讓我們寫的代碼,更加簡潔,冗余小,實現了軟件設計設計模式中的一條規則:高內聚,低耦合;下面呢?我們來思考能不能繼續優化DAO設計模式的實現,

新建一個類,JdbcTemplete .class 來再做個一個提煉 抽象; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.vince.base.ResultSetHandler; public class JdbcTemplete { public int update(String sql,Object ...args){ Connection connection=null; PreparedStatement ps=null; try { connection=DBUtils.getconnection(); ps=connection.prepareStatement(sql); if (args!=null) { for(int i=0;i<args.length;i++){ ps.setObject(i+1, args[i]); } } return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); return 0; }finally { DBUtils.close(null, ps, connection); } } public Object query(String sql,ResultSetHandler handler,Object...args){ Connection connection=null; PreparedStatement ps=null; ResultSet rs=null; try { connection=DBUtils.getconnection(); connection.prepareStatement(sql); if (args!=null) { for(int i=0;i<args.length;i++){ ps.setObject(i+1, args[i]); } } rs=ps.executeQuery(); return handler.doHandler(rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } } 所有類的父類是Objects 有多個參數, 所有我們使用Object …args; 接下來在PersonDaoImpl.class類中,我們就可以使用我們提取處在的抽象類了 嘻嘻~ 是不是代碼簡單多了~~ 向對于數據庫的增刪 更新的操作都可以使用類似的方法,

@Override public void add(Person p) throws SQLException { String sql="insert into person(name,age,description)values(?,?,?)"; jdbcTemolete.update(sql, p.getName(),p.getDescription()); }

對于一個需要從數據庫查詢的操作,在查詢的操作,需要用道 ResultSet這個方法的 我們這里定義一個接口ResultSetHandler.class;

import java.sql.ResultSet;import java.sql.SQLException;public interface ResultSetHandler { public Object doHandler(ResultSet rs)throws SQLException;}

**在JdbcTemplete.class中的query這個方法中 就需要用的這個接口 在PersonDaoImpl.class中這個查詢方法,我們就可以這樣寫了**

@Override public Person findById(final int id) throws SQLException { String sql="select name,age,description from person where id=?"; return (Person) jdbcTemolete.query(sql, new ResultSetHandler() { @Override public Object doHandler(ResultSet rs)throws SQLException { Person p=null; if(rs.next()){ p=new Person(); p.setId(id); p.setName(rs.getString(1)); p.setAge(rs.getInt(2)); p.setDescription(rs.getString(3)); } return p; } },id); }

最后 就是弄一個main 方法 來使用我們定義好 對數據操作的方法嘍

有什么錯誤需要改正歡迎指出,共同學習 有什么錯誤需要改正歡迎指出,共同學習


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北安市| 诏安县| 科技| 南汇区| 彰化市| 阜宁县| 化德县| 长沙市| 聂拉木县| 柞水县| 龙江县| 如皋市| 电白县| 繁峙县| 稻城县| 剑川县| 霍山县| 凭祥市| 鄂尔多斯市| 济南市| 衡阳县| 安庆市| 新龙县| 大庆市| 清水河县| 密云县| 金堂县| 新沂市| 内黄县| 维西| 焦作市| 锦屏县| 会东县| 阿拉尔市| 思南县| 抚顺市| 灵台县| 和政县| 类乌齐县| 纳雍县| 沙河市|