DAO and related implementation classes
DAO: Data Access Object's class and interface for accessing data information, including CRUD (Create, Retrival, Update, Delete) to the data, without any business-related information. Sometimes called: BaseDAO
Function: In order to realize the modularization of functions, it is more conducive to the maintenance and upgrade of the code.
The following is a manifestation of the use of DAO in a bookstore project:
【BaseDAO.java】
package com. atguigu. bookstore. dao;
import java. lang. reflect. ParameterizedType;
import java. lang. reflect. Type;
import java. sql. Connection;
import java. sql. SQLException;
import java. util. List;
import org. apache. commons. dbutils. QueryRunner;
import org. apache. commons. dbutils. handlers. BeanHandler;
import org. apache. commons. dbutils. handlers. BeanListHandler;
import org. apache. commons. dbutils. handlers. ScalarHandler;
public abstract class BaseDao < T> {
private QueryRunner queryRunner = new QueryRunner ( ) ;
private Class< T> type;
public BaseDao ( ) {
Class clazz = this . getClass ( ) ;
ParameterizedType parameterizedType = ( ParameterizedType) clazz. getGenericSuperclass ( ) ;
Type[ ] types = parameterizedType. getActualTypeArguments ( ) ;
this . type = ( Class< T> ) types[ 0 ] ;
}
public int update ( Connection conn, String sql, Object. . . params) {
int count = 0 ;
try {
count = queryRunner. update ( conn, sql, params) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return count;
}
public T getBean ( Connection conn, String sql, Object. . . params) {
T t = null;
try {
t = queryRunner. query ( conn, sql, new BeanHandler < T> ( type) , params) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return t;
}
public List< T> getBeanList ( Connection conn, String sql, Object. . . params) {
List< T> list = null;
try {
list = queryRunner. query ( conn, sql, new BeanListHandler < T> ( type) , params) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return list;
}
public Object getValue ( Connection conn, String sql, Object. . . params) {
Object count = null;
try {
count = queryRunner. query ( conn, sql, new ScalarHandler < > ( ) , params) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return count;
}
}
【BookDAO.java】
package com. atguigu. bookstore. dao;
import java. sql. Connection;
import java. util. List;
import com. atguigu. bookstore. beans. Book;
import com. atguigu. bookstore. beans. Page;
public interface BookDao {
List< Book> getBooks ( Connection conn) ;
void saveBook ( Connection conn, Book book) ;
void deleteBookById ( Connection conn, String bookId) ;
Book getBookById ( Connection conn, String bookId) ;
void updateBook ( Connection conn, Book book) ;
Page< Book> getPageBooks ( Connection conn, Page< Book> page) ;
Page< Book> getPageBooksByPrice ( Connection conn, Page< Book> page, double minPrice, double maxPrice) ;
}
【UserDAO.java】
package com. atguigu. bookstore. dao;
import java. sql. Connection;
import com. atguigu. bookstore. beans. User;
public interface UserDao {
User getUser ( Connection conn, User user) ;
boolean checkUsername ( Connection conn, User user) ;
void saveUser ( Connection conn, User user) ;
}
【BookDaoImpl.java】
package com. atguigu. bookstore. dao. impl;
import java. sql. Connection;
import java. util. List;
import com. atguigu. bookstore. beans. Book;
import com. atguigu. bookstore. beans. Page;
import com. atguigu. bookstore. dao. BaseDao;
import com. atguigu. bookstore. dao. BookDao;
public class BookDaoImpl extends BaseDao < Book> implements BookDao {
@Override
public List< Book> getBooks ( Connection conn) {
List< Book> beanList = null;
String sql = "select id,title,author,price,sales,stock,img_path imgPath from books" ;
beanList = getBeanList ( conn, sql) ;
return beanList;
}
@Override
public void saveBook ( Connection conn, Book book) {
String sql = "insert into books(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)" ;
update ( conn, sql, book. getTitle ( ) , book. getAuthor ( ) , book. getPrice ( ) , book. getSales ( ) , book. getStock ( ) , book. getImgPath ( ) ) ;
}
@Override
public void deleteBookById ( Connection conn, String bookId) {
String sql = "DELETE FROM books WHERE id = ?" ;
update ( conn, sql, bookId) ;
}
@Override
public Book getBookById ( Connection conn, String bookId) {
Book book = null;
String sql = "select id,title,author,price,sales,stock,img_path imgPath from books where id = ?" ;
book = getBean ( conn, sql, bookId) ;
return book;
}
@Override
public void updateBook ( Connection conn, Book book) {
String sql = "update books set title = ? , author = ? , price = ? , sales = ? , stock = ? where id = ?" ;
update ( conn, sql, book. getTitle ( ) , book. getAuthor ( ) , book. getPrice ( ) , book. getSales ( ) , book. getStock ( ) , book. getId ( ) ) ;
}
@Override
public Page< Book> getPageBooks ( Connection conn, Page< Book> page) {
String sql = "select count(*) from books" ;
long totalRecord = ( long ) getValue ( conn, sql) ;
page. setTotalRecord ( ( int ) totalRecord) ;
String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books limit ?,?" ;
List< Book> beanList = getBeanList ( conn, sql2, ( page. getPageNo ( ) - 1 ) * Page. PAGE_SIZE, Page. PAGE_SIZE) ;
page. setList ( beanList) ;
return page;
}
@Override
public Page< Book> getPageBooksByPrice ( Connection conn, Page< Book> page, double minPrice, double maxPrice) {
String sql = "select count(*) from books where price between ? and ?" ;
long totalRecord = ( long ) getValue ( conn, sql, minPrice, maxPrice) ;
page. setTotalRecord ( ( int ) totalRecord) ;
String sql2 = "select id,title,author,price,sales,stock,img_path imgPath from books where price between ? and ? limit ?,?" ;
List< Book> beanList = getBeanList ( conn, sql2, minPrice , maxPrice , ( page. getPageNo ( ) - 1 ) * Page. PAGE_SIZE, Page. PAGE_SIZE) ;
page. setList ( beanList) ;
return page;
}
}
【UserDaoImpl.java】
package com. atguigu. bookstore. dao. impl;
import java. sql. Connection;
import com. atguigu. bookstore. beans. User;
import com. atguigu. bookstore. dao. BaseDao;
import com. atguigu. bookstore. dao. UserDao;
public class UserDaoImpl extends BaseDao < User> implements UserDao {
@Override
public User getUser ( Connection conn, User user) {
User bean = null;
String sql = "select id,username,password,email from users where username = ? and password = ?" ;
bean = getBean ( conn, sql, user. getUsername ( ) , user. getPassword ( ) ) ;
return bean;
}
@Override
public boolean checkUsername ( Connection conn, User user) {
User bean = null;
String sql = "select id,username,password,email from users where username = ?" ;
bean = getBean ( conn, sql, user. getUsername ( ) ) ;
return bean != null;
}
@Override
public void saveUser ( Connection conn, User user) {
String sql = "insert into users(username,password,email) values(?,?,?)" ;
update ( conn, sql, user. getUsername ( ) , user. getPassword ( ) , user. getEmail ( ) ) ;
}
}
【Book.java】
package com. atguigu. bookstore. beans;
public class Book {
private Integer id;
private String title;
private String author;
private double price;
private Integer sales;
private Integer stock;
private String imgPath = "static/img/default.jpg" ;
}
【Page.java】
package com. atguigu. bookstore. beans;
import java. util. List;
public class Page < T> {
private List< T> list;
public static final int PAGE_SIZE = 4 ;
private int pageNo;
private int totalRecord;
【User.java】
package com. atguigu. bookstore. beans;
public class User {
private Integer id;
private String username;
private String password;
private String email;