麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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

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

什么是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 方法 來使用我們定義好 對數據操作的方法嘍

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


上一篇:POJ 1010

下一篇:nodejs時間處理工具momentjs

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色a级片视频 | 欧美一级毛片免费观看视频 | 日韩精品a在线观看 | 日本在线视频免费观看 | 成人午夜小视频 | 日韩精品中文字幕在线播放 | 午夜精品成人一区二区 | 校花被肉干高h潮不断 | 中文在线日韩 | 成人免费在线网 | 成人在线观看一区 | 一级黄色毛片播放 | 国产午夜免费不卡精品理论片 | 欧美一级电影网站 | 九九热在线精品视频 | 一级一片免费 | 久久久久九九九女人毛片 | 国产日韩大片 | 极品大长腿啪啪高潮露脸 | 亚洲免费永久 | 午夜视频中文字幕 | 久久精品视频亚洲 | 中文字幕国产欧美 | www.精品视频 | 一级做a爱片性色毛片 | 欧美视频99 | 久久中文免费 | 亚洲成人在线视频网站 | 91九色视频观看 | 99麻豆久久久国产精品免费 | av在线免费观看中文字幕 | 精品一区二区三区在线视频 | 一级免费a | 国产成人免费精品 | 牛牛热这里只有精品 | 露脸各种姿势啪啪的清纯美女 | 666sao| 国产精品免费一区二区 | 成人免费看片a | 久久成人福利 | 久久福利剧场 |