欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年12月13日 11:55:34   作者:Java Fans  
三層架構(gòu)是一種軟件設(shè)計(jì)架構(gòu),是一種組織代碼的手段和方法,三層架構(gòu)的優(yōu)點(diǎn)是擴(kuò)展性好,復(fù)用性高;缺點(diǎn)是步驟多,比較繁瑣;代碼多,效率降低

什么是三層

(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ù)訪問(wèn)層(Dao)

  • 命名:XxxDaoImpl
  • 職責(zé):向業(yè)務(wù)層提供數(shù)據(jù),將業(yè)務(wù)層加工后的數(shù)據(jù)同步到數(shù)據(jù)庫(kù)

三層架構(gòu)項(xiàng)目搭建步驟

項(xiàng)目環(huán)境搭建

1)新建一個(gè)項(xiàng)目,在項(xiàng)目中創(chuàng)建lib文件夾,將MySQL數(shù)據(jù)庫(kù)jar包放在lib文件夾中,配置環(huán)境。

2)在src文件夾下面創(chuàng)建db.properties文件,編寫數(shù)據(jù)庫(kù)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)類或測(cè)試類(main)

創(chuàng)建book表

創(chuàng)建表

CREATE TABLE IF NOT EXISTS `book`(
`bid` INT PRIMARY KEY AUTO_INCREMENT COMMENT '圖書編號(hào)',
`isbn` VARCHAR(20) UNIQUE NOT NULL COMMENT '國(guó)際標(biāo)準(zhǔn)書號(hào)',
`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從入門到精通','張三','中國(guó)水利水電出版社',79.80,'數(shù)據(jù)庫(kù)');
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 {
	/** 圖書編號(hào) */
	private int bid;
	/** 國(guó)際標(biāo)準(zhǔn)書號(hào) */
	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接口,接口中定義對(duì)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ù)庫(kù)中是否存在
		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("刪除的圖書不存在,無(wú)法刪除");
		} 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;
	}
}

編寫測(cè)試類

測(cè)試增加數(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("圖書添加失敗");
		}
	}
}

測(cè)試刪除數(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("刪除失敗");
		}
	}
}

測(cè)試修改數(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("修改失敗");
		}
	}
}

測(cè)試查詢單個(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("沒(méi)有你要查找的圖書");
		} else {
			System.out.println(book);
		}
	}
}

測(cè)試查詢所有

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ù)庫(kù)里沒(méi)有書的數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring DI依賴注入的方式和類型

    詳解Spring DI依賴注入的方式和類型

    這篇文章主要介紹了詳解Spring DI依賴注入的方式和類型,DI是由容器動(dòng)態(tài)的將某個(gè)依賴關(guān)系注入到組件之中。依賴注入的目的并非為軟件系統(tǒng)帶來(lái)更多功能,而是為了提升組件重用的頻率,并為系統(tǒng)搭建一個(gè)靈活、可擴(kuò)展的平臺(tái),需要的朋友可以參考下
    2023-05-05
  • Java集合ArrayList與LinkedList詳解

    Java集合ArrayList與LinkedList詳解

    這篇文章主要介紹了Java集合ArrayList與LinkedList詳解,對(duì)于ArrayList和LinkedList,他們都是List接口的一個(gè)實(shí)現(xiàn)類,并且我們知道他們的實(shí)現(xiàn)方式各不相同,例如ArrayList底層實(shí)現(xiàn)是一個(gè)數(shù)組
    2022-08-08
  • JAVA獲取Image的三種方式

    JAVA獲取Image的三種方式

    這篇文章主要介紹了JAVA獲取Image的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java驗(yàn)證碼功能的實(shí)現(xiàn)方法

    Java驗(yàn)證碼功能的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Java驗(yàn)證碼功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • java如何讀取超大文件

    java如何讀取超大文件

    這篇文章主要為大家詳細(xì)介紹了java如何讀取超大文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java實(shí)現(xiàn)級(jí)聯(lián)下拉結(jié)構(gòu)的示例代碼

    Java實(shí)現(xiàn)級(jí)聯(lián)下拉結(jié)構(gòu)的示例代碼

    在開(kāi)發(fā)過(guò)程中,會(huì)遇到很多的實(shí)體需要將查出的數(shù)據(jù)處理為下拉或者級(jí)聯(lián)下拉的結(jié)構(gòu),提供給前端進(jìn)行展示。本文為大家介紹了java封裝下拉和級(jí)聯(lián)下拉的通用工具類,需要的可以參考一下
    2022-06-06
  • SpringBoot集成單點(diǎn)登錄CAS的方法實(shí)現(xiàn)

    SpringBoot集成單點(diǎn)登錄CAS的方法實(shí)現(xiàn)

    本文主要介紹了SpringBoot集成單點(diǎn)登錄CAS的方法實(shí)現(xiàn),包括CAS的基本概念、集成步驟、具體代碼示例等,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼

    JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼

    本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • Java操作文件路徑正反斜杠問(wèn)題解決

    Java操作文件路徑正反斜杠問(wèn)題解決

    最近在實(shí)現(xiàn)文件上傳時(shí),windows與linux系統(tǒng)出現(xiàn)的問(wèn)題,兩個(gè)系統(tǒng)中操作文件使用"\","/"導(dǎo)致IOException,本文主要介紹了Java操作文件路徑正反斜杠問(wèn)題解決,感興趣的可以了解一下啊
    2024-01-01
  • servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明詳解

    servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明詳解

    這篇文章主要為大家詳細(xì)介紹了servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評(píng)論