網(wǎng)站建設(shè)合同 簡單手游推廣個人合作平臺
目錄
DAO模式 含義
DAO模式 的理解
分層思維
分層含義
分層目的
dao層
dao包(對接的是操作數(shù)據(jù)庫的接口)
dao包下lmpl 包(dao包中接口的實現(xiàn)類)
補充
1 你創(chuàng)建的實體類需要和數(shù)據(jù)庫中建的表一一對應(yīng)。
總結(jié)
DAO模式 含義
數(shù)據(jù)訪問對象(Data Access Object)模式,是一種設(shè)計模式,主要用于將業(yè)務(wù)邏輯與數(shù)據(jù)訪問代碼分離,以提高代碼的模塊化、可維護性和可測試性。
DAO模式 的理解
理解DAO模式,我認(rèn)為最為重要的:理解分層思維
分層思維
分層含義
- 將系統(tǒng)分解成多個層次,每個層次都有明確的職責(zé)和功能,并且層次之間通過定義良好的接口進行交互
- 我認(rèn)為應(yīng)該把,同一功能/同一技術(shù)類型的類,放在同一包下
例如,Java分為經(jīng)典三層模型
經(jīng)典的三層架構(gòu)(表現(xiàn)層、業(yè)務(wù)邏輯層、數(shù)據(jù)訪問層)
- 表現(xiàn)層:負(fù)責(zé)用戶界面和用戶交互,如Web頁面、桌面應(yīng)用界面等。
- 業(yè)務(wù)邏輯層:處理業(yè)務(wù)規(guī)則和業(yè)務(wù)流程,如訂單處理、用戶認(rèn)證等。
- 數(shù)據(jù)訪問層:負(fù)責(zé)數(shù)據(jù)的持久化,如數(shù)據(jù)庫操作。
- 以下是一些常見的
- web層:表現(xiàn)層負(fù)責(zé)用戶界面和用戶交互,如Web頁面、桌面應(yīng)用界面
- service層:業(yè)務(wù)邏輯層(主要處理業(yè)務(wù),邏輯代碼,數(shù)據(jù)加工,條件判斷)
- dao層:數(shù)據(jù)訪問層,封裝與數(shù)據(jù)庫操作
- entity/domain..層 實體類(對應(yīng)的數(shù)據(jù)庫的表的類)
- util :工具類(如jdbcUtil 封裝 連接的數(shù)據(jù)庫的一些操作)
分層目的
1 技術(shù)隔離
- 比如dao層使用的技術(shù)jdbc jdbc中的核心的api 類 不能在其他的類出現(xiàn)
2 不能跨層調(diào)用
如下圖 展示的:
dao層
數(shù)據(jù)訪問層,封裝數(shù)據(jù)庫的操作
- dao包(對接的是操作數(shù)據(jù)庫的接口)
- dao包下lmpl 包(dao包中接口的實現(xiàn)類)
dao包(對接的是操作數(shù)據(jù)庫的接口)
StudentDao 接口實例代碼
// 查詢public Student showStudent(int id);//查詢所有學(xué)生public List<Student> showAllStudent();// 刪除public boolean delete(int id);// 修改public boolean update(Object...params);// 添加public boolean add(Object...params);
UserDao接口實例代碼
// 登錄int login( String username, String password);// 注冊int register(String username, String password);
dao包下lmpl 包(dao包中接口的實現(xiàn)類)
- StudentDaoImpl類 實現(xiàn)StudentDao 接口
- ?UserDaoImpl 類? ? 實現(xiàn)UserDao接口
StudentDaoImpl 實例代碼
package it.dao.impl;import it.Util.jdbcUtil; import it.dao.StudentDao; import it.dao.UserDao; import it.entity.Student;import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner;/*** @Author: Administrator* @Description:* @Date: 2024/11/8 下午4:54* @Version: 1.0*/ public class StudentDaoImpl implements StudentDao {Scanner input = new java.util.Scanner(System.in);/** 根據(jù)id查詢學(xué)生*/@Overridepublic Student showStudent(int id) {String sql = "select stuId, stuName, stuSex, stuAge from student where stuId=?";String stuName = null;int stuAge = 0;int stuId = 0;String stuSex = null;try {ResultSet resultSet = jdbcUtil.executeQuery(sql, id);if (resultSet.next()) {stuId = resultSet.getInt("stuId");stuName = resultSet.getString("stuName");stuSex = resultSet.getString("stuSex");stuAge = resultSet.getInt("stuAge");return new Student(stuName, stuAge, stuSex, stuId);} else {System.out.println("該學(xué)生不存在");return null;}} catch (SQLException e) {throw new RuntimeException(e);}}/** 查詢所有學(xué)生*/@Overridepublic List<Student> showAllStudent() {String sql = "select stuId, stuName, stuSex, stuAge from student";List<Student> list = new ArrayList<>();try {Connection conn = jdbcUtil.getConnection();Statement statement = conn.createStatement();ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()) {int stuId = resultSet.getInt("stuId");String stuName = resultSet.getString("stuName");String stuSex = resultSet.getString("stuSex");int stuAge = resultSet.getInt("stuAge");Student student = new Student(stuName, stuAge, stuSex, stuId);list.add(student);}} catch (SQLException e) {throw new RuntimeException(e);}return list;}/*刪除學(xué)生信息*/@Overridepublic boolean delete(int id) {String sql = "delete from student where stuId=?";int i = jdbcUtil.executeUpdate(sql, id);if (i > 0) {return true;}return false;}@Overridepublic boolean update(Object... params) {String sql = "update student set stuName=?,stuAge=?,stuSex=? where stuId=?";int i = jdbcUtil.executeUpdate(sql, params);if (i > 0) {return true;} else {return false;}}/* 添加學(xué)生信息 */@Overridepublic boolean add(Object... params) {String sql = "insert into student(stuName,stuSex,stuAge) values(?,?,?)";int i = jdbcUtil.executeUpdate(sql, params);if (i > 0) {return true;} else {return false;}} }
?UserDaoImpl 類的實例代碼
package it.dao.impl;import it.Util.jdbcUtil; import it.dao.StudentDao; import it.dao.UserDao;import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException;/*** @Author: Administrator* @Description:* @Date: 2024/11/9 下午4:20* @Version: 1.0*/ public class UserDaoImpl implements UserDao {/** 登錄*/@Overridepublic int login( String username, String password) {String sql = "select username,pwd from user where username=? and pwd=?";ResultSet resultSet = null;try {resultSet = jdbcUtil.executeQuery(sql, username, password);if (resultSet.next()) {return 0;} else {return 1;}} catch (SQLException e) {throw new RuntimeException(e);}} /*注冊*/@Overridepublic int register(String username, String password) {String sql = "insert into user(username,pwd) values(?,?)";Date date = new Date(System.currentTimeMillis());int i = jdbcUtil.executeUpdate(sql, username, password);if (i > 0) {return 1;} else {return 0;}} }
補充
1 你創(chuàng)建的實體類需要和數(shù)據(jù)庫中建的表一一對應(yīng)。
如下圖所示:
數(shù)據(jù)庫的字段類型 | 實體類的屬性數(shù)據(jù)類型 |
char/varchar/text[文本型] | String |
int [數(shù)值型] | Integer |
bigint | Long |
double | Double |
decimal | BigDecimal |
注意:數(shù)據(jù)庫中 data/time/datetime 字段類型 ,對應(yīng)在Java中有兩種形式:
- ?Java.util.Date
- java.sql.Date
但我們推薦使用Java.util.Date
原因
這里 存在向上轉(zhuǎn)型:Java.util.Date 是java.sql.Date 的父類
總結(jié)
本篇博客,簡單的介紹了DAO模式。但我認(rèn)為這是遠(yuǎn)遠(yuǎn)不夠的在之后的學(xué)習(xí)中,還無法理解其中的精髓。因此在之后的學(xué)習(xí)中,我會及時補充