DAO設計模式
DAO設計模式簡介: DAO設計模式可以減少代碼量,增強程序的可移植性,提高代碼的可讀性。
DAO(數據庫操作對象)設計模式是 JavaEE 數據層的操作.主要由五部分組成:
1.數據庫連接類:連接數據庫並獲取連接對象。
2.VO實體類:包含屬性和表中字段完全對應的類。
3.DAO接口:提供了用戶所有的操作方法(就如老師給學生提供一些學習方法)。
4.DAO實現類:實現DAO中所有的方法(就如老師給提供的方法看你如何去完成)。
5.DAO工廠類:為程序提供方法,如果要替換DAO實現類,只需要修改該Dao工廠類中的方法代碼,
而不必邀修改所有的操作數據庫代碼(如代理人專門為需要的公司提供人才,也叫做服務層)。
下面以一個用戶登錄為例:
1.數據庫建表:
2.數據庫連接類 com.util(記得導入c3p0框架包)
package com.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Util { // C3P0數據源 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static DataSource getDataSource() { return dataSource; } public static Connection getConn() { Connection conne = null; try { conne = dataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conne; } // 關閉資源 public static void closeAll(ResultSet rs, Statement st, Connection conne) { try { if (null != rs) { rs.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (null != st) { st.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (conne != null) { conne.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3.VO實體類 com.entity
package com.entity; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private String password; 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 String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4.DAO接口類 com.dao
package com.dao; import com.entity.Person; public interface PersonDao { //根據對象查詢 public Person findMaster(Person person); }
5.DAO實現類 com.daoImpl
package com.daoImpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.dao.PersonDao; import com.entity.Person; import com.util.C3P0Util; public class PersonDaoImpl implements PersonDao { @Override public Person findMaster(Person person) { Connection conne = null; PreparedStatement pstmt = null; ResultSet rs = null; Person person2 = null; String sql = "SELECT * FROM person WHERE name = ? AND password = ?"; try { conne = C3P0Util.getConn(); // 建立了到特定數據庫的連接之後,就可用該連接發送 SQL 語句。 pstmt = conne.prepareStatement(sql); pstmt.setString(1, person.getName()); pstmt.setString(2, person.getPassword()); rs = pstmt.executeQuery(); if (rs.next()) { person2 = new Person(); person2.setId(rs.getInt("id")); person2.setName(rs.getString("name")); person2.setPassword(rs.getString("password")); } } catch (Exception e) { e.printStackTrace(); } finally { C3P0Util.closeAll(rs, pstmt, conne); } return person2; } }
6.DAO工廠接口 com.service
package com.service; import com.entity.Person; public interface PersonService { public boolean login (Person person); }
7.DAO工廠實現類 com.serviceImpl
package com.serviceImpl; import com.dao.PersonDao; import com.daoImpl.PersonDaoImpl; import com.entity.Person; import com.service.PersonService; public class PersonServiceImpl implements PersonService { @Override public boolean login(Person person) { boolean islogin = false; PersonDao pd = new PersonDaoImpl(); Person ps2 = pd.findMaster(person); if (null != ps2) { islogin = true; } return islogin; } }
8.測試
package com.test; import java.util.Scanner; import org.junit.Test; import com.entity.Person; import com.service.PersonService; import com.serviceImpl.PersonServiceImpl; public class LoginTest { @Test public void loginTest() { Scanner sc = new Scanner(System.in, "UTF-8"); System.out.println("請輸入用戶名"); String loginName = sc.next(); System.out.println("請輸入密碼"); String loginPassword = sc.next(); Person person = new Person(); person.setName(loginName); person.setPassword(loginPassword); PersonService ps = new PersonServiceImpl(); boolean isLoginid = ps.login(person); if (isLoginid) { System.out.println("登錄成功"); } else { System.out.println("用戶名或密碼錯誤"); } } }
運行結果如下: