JDBC三層架構(gòu)深入刨析

什么是三層
(1)表示層(View)
- 命名:XxxView
- 職責(zé):收集用戶的數(shù)據(jù)和需求、數(shù)據(jù)
(2)業(yè)務(wù)邏輯層(service)
- 命名:XxxServiceImpl
- 職責(zé):數(shù)據(jù)加工處理、調(diào)用DAO完成業(yè)務(wù)實(shí)現(xiàn)、控制事務(wù)
(3)數(shù)據(jù)訪問層(Dao)
- 命名:XxxDaoImpl
- 職責(zé):向業(yè)務(wù)層提供數(shù)據(jù),將業(yè)務(wù)層加工后的數(shù)據(jù)同步到數(shù)據(jù)庫

三層架構(gòu)項(xiàng)目搭建步驟
項(xiàng)目環(huán)境搭建
1)新建一個(gè)項(xiàng)目,在項(xiàng)目中創(chuàng)建lib文件夾,將MySQL數(shù)據(jù)庫jar包放在lib文件夾中,配置環(huán)境。
2)在src文件夾下面創(chuàng)建db.properties文件,編寫數(shù)據(jù)庫driver、url、user、password信息。
3)創(chuàng)建三層需要的各個(gè)包。
(1)utils包:存放工具類(DBUtils類、DateUtils類)
(2)entity包:存放實(shí)體類(Xxx.java)
(3)dao包:存放DAO接口
impl包:存放DAO接口實(shí)現(xiàn)類
(4)service包:存放service接口
impl:存放service接口實(shí)現(xiàn)類
(5)view包:存放程序啟動(dòng)類或測試類(main)
創(chuàng)建book表
創(chuàng)建表
CREATE TABLE IF NOT EXISTS `book`( `bid` INT PRIMARY KEY AUTO_INCREMENT COMMENT '圖書編號', `isbn` VARCHAR(20) UNIQUE NOT NULL COMMENT '國際標(biāo)準(zhǔn)書號', `name` VARCHAR(20) NOT NULL COMMENT '書名', `author` VARCHAR(20) NOT NULL COMMENT '作者', `press` VARCHAR(20) NOT NULL COMMENT '出版社', `price` DOUBLE NOT NULL COMMENT '價(jià)格', `classification` VARCHAR(20) NOT NULL COMMENT '分類' );
向表中插入數(shù)據(jù)
INSERT INTO `book`(`bid`,`isbn`,`name`,`author`,`press`,`price`,`classification`) VALUES (1001,'978-7-5170-7654-4','SQL從入門到精通','張三','中國水利水電出版社',79.80,'數(shù)據(jù)庫'); INSERT INTO `book`(`bid`,`isbn`,`name`,`author`,`press`,`price`,`classification`) VALUES (1002,'976-9-5245-7633-5','java從入門到精通','李四','清華大學(xué)出版社',99.80,'程序設(shè)計(jì)');
創(chuàng)建entity實(shí)體類Book
package com.cxyzxc.www.entity;
import java.util.Date;
/**
* entity實(shí)體類Booke類
*/
public class Book {
/** 圖書編號 */
private int bid;
/** 國際標(biāo)準(zhǔn)書號 */
private String isbn;
/** 書名 */
private String name;
/** 作者 */
private String author;
/** 出版社 */
private String press;
/** 價(jià)格 */
private double price;
/** 分類 */
private String classification;
/** 出版日期 */
private Date pubdate;
public Book() {
super();
}
public Book(String isbn, String name, String author, String press,
double price, String classification, Date pubdate) {
super();
this.isbn = isbn;
this.name = name;
this.author = author;
this.press = press;
this.price = price;
this.classification = classification;
this.pubdate = pubdate;
}
public Book(int bid, String isbn, String name, String author, String press,
double price, String classification, Date pubdate) {
super();
this.bid = bid;
this.isbn = isbn;
this.name = name;
this.author = author;
this.press = press;
this.price = price;
this.classification = classification;
this.pubdate = pubdate;
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public Date getPubdate() {
return pubdate;
}
public void setPubdate(Date pubdate) {
this.pubdate = pubdate;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", isbn=" + isbn + ", name=" + name
+ ", author=" + author + ", press=" + press + ", price="
+ price + ", classification=" + classification + ", pubdate="
+ pubdate + "]";
}
}創(chuàng)建BookDao接口
package com.cxyzxc.www.dao;
import java.util.List;
import com.cxyzxc.www.entity.Book;
/**
* 定義BookDao接口,接口中定義對book表增刪改查的方法
*/
public interface BookDao {
//增
int insert(Book book);
//刪
int delete(int bid);
//改
int update(Book book);
//查單個(gè)
Book selectOne(int bid);
//查所有
List<Book> selectAll();
}
創(chuàng)建BookDaoImpl實(shí)現(xiàn)類
package com.cxyzxc.www.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.cxyzxc.www.dao.BookDao;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.utils.DBUtils;
import com.cxyzxc.www.utils.DateUtils;
/**
* 定義BookDaoImpl類,實(shí)現(xiàn)BookDao接口,重寫接口中的增刪改查方法
*/
public class BookDaoImpl implements BookDao {
@Override
public int insert(Book book) {
Connection connection = null;
PreparedStatement preparedStatement = null;
connection = DBUtils.getConnection();
String sql = "INSERT INTO `book`(`isbn`,`name`,`author`,`press`,`price`,`classification`,`pubdate`)VALUES(?,?,?,?,?,?,?);";
try {
preparedStatement = connection.prepareStatement(sql);
// 綁定參數(shù)
preparedStatement.setString(1, book.getIsbn());
preparedStatement.setString(2, book.getName());
preparedStatement.setString(3, book.getAuthor());
preparedStatement.setString(4, book.getPress());
preparedStatement.setDouble(5, book.getPrice());
preparedStatement.setString(6, book.getClassification());
preparedStatement.setDate(7,
DateUtils.utilDateToSqlDate(book.getPubdate()));
int result = preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int delete(int bid) {
Connection connection = null;
PreparedStatement preparedStatement = null;
connection = DBUtils.getConnection();
String sql = "DELETE FROM `book` WHERE `bid`=?;";
try {
preparedStatement = connection.prepareStatement(sql);
// 綁定參數(shù)
preparedStatement.setInt(1, bid);
int result = preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int update(Book book) {
Connection connection = null;
PreparedStatement preparedStatement = null;
connection = DBUtils.getConnection();
String sql = "UPDATE `book` SET `isbn`=?,`name`=?,`author`=?,`press`=?,`price`=?,`classification`=?,`pubdate`=? WHERE `bid`=?;";
try {
preparedStatement = connection.prepareStatement(sql);
// 綁定參數(shù)
preparedStatement.setString(1, book.getIsbn());
preparedStatement.setString(2, book.getName());
preparedStatement.setString(3, book.getAuthor());
preparedStatement.setString(4, book.getPress());
preparedStatement.setDouble(5, book.getPrice());
preparedStatement.setString(6, book.getClassification());
preparedStatement.setDate(7,
DateUtils.utilDateToSqlDate(book.getPubdate()));
preparedStatement.setDouble(8, book.getBid());
int result = preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Book selectOne(int bid) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Book book = null;
connection = DBUtils.getConnection();
String sql = "Select * FROM `book` WHERE `bid`=?;";
try {
preparedStatement = connection.prepareStatement(sql);
// 綁定參數(shù)
preparedStatement.setInt(1, bid);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int bookid = resultSet.getInt(1);
String isbn = resultSet.getString(2);
String name = resultSet.getString(3);
String author = resultSet.getString(4);
String press = resultSet.getString(5);
double price = resultSet.getDouble(6);
String classification = resultSet.getString(7);
Date pubdate = resultSet.getDate(8);
book = new Book(bookid, isbn, name, author, press, price,
classification, pubdate);
}
return book;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Book> selectAll() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Book book = null;
List<Book> bookList = new ArrayList<Book>();
connection = DBUtils.getConnection();
String sql = "Select * FROM `book`;";
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int bookid = resultSet.getInt(1);
String isbn = resultSet.getString(2);
String name = resultSet.getString(3);
String author = resultSet.getString(4);
String press = resultSet.getString(5);
double price = resultSet.getDouble(6);
String classification = resultSet.getString(7);
Date pubdate = resultSet.getDate(8);
book = new Book(bookid, isbn, name, author, press, price,
classification, pubdate);
bookList.add(book);
}
return bookList;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}創(chuàng)建BookService接口
package com.cxyzxc.www.service;
import java.util.List;
import com.cxyzxc.www.entity.Book;
/**
* 定義BookService接口,接口中定義業(yè)務(wù)邏輯方法
*/
public interface BookService {
//添加圖書
int addBook(Book book);
//刪除圖書
int deleteBook(int bid);
//修改圖書
int updateBook(Book book);
//查詢一本圖書
Book selectOne(int bid);
//查詢所有圖書
List<Book> selectAll();
}創(chuàng)建BookServiceImpl實(shí)現(xiàn)類
package com.cxyzxc.www.service.impl;
import java.util.List;
import com.cxyzxc.www.dao.BookDao;
import com.cxyzxc.www.dao.impl.BookDaoImpl;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.service.BookService;
/**
* 定義BookServiceImpl類,實(shí)現(xiàn)BookService接口,重寫B(tài)ookService接口中的所有方法
*/
public class BookServiceImpl implements BookService {
BookDao bookDao = new BookDaoImpl();
@Override
public int addBook(Book book) {
// 首先查詢一下插入的圖書在數(shù)據(jù)庫中是否存在
Book book2 = bookDao.selectOne(book.getBid());
if (book2 == null) {
return bookDao.insert(book);
} else {
System.out.println("插入的圖書已經(jīng)存在,插入失敗");
}
return 0;
}
@Override
public int deleteBook(int bid) {
// 查詢要?jiǎng)h除的圖書是否存在
Book book2 = bookDao.selectOne(bid);
if (book2 == null) {
System.out.println("刪除的圖書不存在,無法刪除");
} else {
return bookDao.delete(bid);
}
return 0;
}
@Override
public int updateBook(Book book) {
// 查詢要修改的圖書是否存在
Book book2 = bookDao.selectOne(book.getBid());
if (book2 == null) {
System.out.println("你要修改的圖書不存在");
} else {
return bookDao.update(book);
}
return 0;
}
@Override
public Book selectOne(int bid) {
Book book2 = bookDao.selectOne(bid);
return book2;
}
@Override
public List<Book> selectAll() {
List<Book> bookList = bookDao.selectAll();
return bookList;
}
}編寫測試類
測試增加數(shù)據(jù)
package com.cxyzxc.www.view;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.service.BookService;
import com.cxyzxc.www.service.impl.BookServiceImpl;
import com.cxyzxc.www.utils.DateUtils;
public class Test01InsertBook {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
// 添加圖書
Book book = new Book("978-9-9456-3286-9", "JSP從入門到精通", "李二狗", "郵電出版社",129, "編程設(shè)計(jì)", DateUtils.strDateToUtilDate("2022-01-13"));
int result = bookService.addBook(book);
if (result == 1) {
System.out.println("圖書添加成功");
} else {
System.out.println("圖書添加失敗");
}
}
}測試刪除數(shù)據(jù)
package com.cxyzxc.www.view;
import com.cxyzxc.www.service.BookService;
import com.cxyzxc.www.service.impl.BookServiceImpl;
public class Test02DeleteBook {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
// 刪除圖書
int result = bookService.deleteBook(1003);
if (result == 1) {
System.out.println("刪除成功");
} else {
System.out.println("刪除失敗");
}
}
}
測試修改數(shù)據(jù)
package com.cxyzxc.www.view;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.service.BookService;
import com.cxyzxc.www.service.impl.BookServiceImpl;
import com.cxyzxc.www.utils.DateUtils;
public class Test03UpdateBook {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
// //修改圖書
Book book = new Book(1002, "976-9-5245-7633-5", "JSP從入門到放棄", "李四","清華大學(xué)出版社", 109.8, "編程設(shè)計(jì)",DateUtils.strDateToUtilDate("2022-10-13"));
int result = bookService.updateBook(book);
if (result == 1) {
System.out.println("修改成功");
} else {
System.out.println("修改失敗");
}
}
}測試查詢單個(gè)
package com.cxyzxc.www.view;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.service.BookService;
import com.cxyzxc.www.service.impl.BookServiceImpl;
public class Test04SelectOneBook {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
Book book = bookService.selectOne(1003);
if (book == null) {
System.out.println("沒有你要查找的圖書");
} else {
System.out.println(book);
}
}
}
測試查詢所有
package com.cxyzxc.www.view;
import java.util.List;
import com.cxyzxc.www.entity.Book;
import com.cxyzxc.www.service.BookService;
import com.cxyzxc.www.service.impl.BookServiceImpl;
public class Test05SelectAllBook {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
List<Book> books = bookService.selectAll();
if (books.isEmpty()) {
System.out.println("數(shù)據(jù)庫里沒有書的數(shù)據(jù)");
} else {
for (int i = 0; i < books.size(); i++) {
System.out.println(books.get(i));
}
}
}
}到此這篇關(guān)于JDBC三層架構(gòu)深入刨析的文章就介紹到這了,更多相關(guān)JDBC三層架構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java驗(yàn)證碼功能的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Java驗(yàn)證碼功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Java實(shí)現(xiàn)級聯(lián)下拉結(jié)構(gòu)的示例代碼
在開發(fā)過程中,會遇到很多的實(shí)體需要將查出的數(shù)據(jù)處理為下拉或者級聯(lián)下拉的結(jié)構(gòu),提供給前端進(jìn)行展示。本文為大家介紹了java封裝下拉和級聯(lián)下拉的通用工具類,需要的可以參考一下2022-06-06
SpringBoot集成單點(diǎn)登錄CAS的方法實(shí)現(xiàn)
本文主要介紹了SpringBoot集成單點(diǎn)登錄CAS的方法實(shí)現(xiàn),包括CAS的基本概念、集成步驟、具體代碼示例等,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實(shí)例代碼
本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
servlet實(shí)現(xiàn)文件下載的步驟及說明詳解
這篇文章主要為大家詳細(xì)介紹了servlet實(shí)現(xiàn)文件下載的步驟及說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

