DAO模式随记
Dao模式的随记
** 写这个是为了日后的更好的mvc的基础,而且更能让自己的思维的更加清晰,作业好做,但是方法难与培养**
实现Dao模式的步骤
– 主要分为5个模块
- VO类
- DAO接口
- DAO实现类
- DAO工厂类
- 数据库连接类
下面是为上面的5个模块进行详细介绍
- 数据库连接类
- 这个就是用于底层的实现就是连接的数据库的,,但是在这里,也是可以分为一个数据库的配置文件,和一个底层实现类,,
- 上面的的其他类就是可以很方便的调用的这个类的方法
- 例如连接,关闭,等等
- VO
- Value Object 也是值对象,也相当于javabean,里面的属性应该尽量与数据库的字段一致,,这样可以省略的很多的麻烦
- 在这里类的里面应该还有set、get方法来获取属性,
- DAO接口
- 这个定义了所有用户的操作例如用户,添加,修改,删除的,查找记录等,
- 但是这里面都是抽象方法,这个是为开发人员提供了访问数据库表的一些通用方法
- 降低了应用程序对底层数据库的依赖
- 但是里面的注释必须要写清楚
- DAO实现类
- 这个实现DAO接口,更加明确的实现sql语句和功能的实现
- DAO工厂类
- 工厂模式是最常用的实例化对象模式
- 代替new的模式的一种模式,
- 这个作用的是就是在实例化DAO实现类的功能
- 如果DAO实现类需要调整的,可以在这个直接更改,不需要再进行大量更改实现类的作用
下面是代码的实现
数据库的连接类(这个根据自己的所用的不同,我用的C3p0,你也可以用读取配置文件,)
/**
* Created by IntelliJ IDEA.
*
* @version : 1.0
* @auther : Firewine
* @mail : 1451661318@qq.com
* @Program Name: <br>
* @Create : 2018-11-16-17:50
* @Description : <br/>
*/
public class JDBCTools {
public static void release(ResultSet resultSet, Statement statement, Connection connection){
if (resultSet != null){
try{
resultSet.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (statement != null){
try{
statement.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (connection != null){
try{
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void release(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
if (resultSet != null){
try{
resultSet.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (preparedStatement != null){
try{
preparedStatement.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (connection != null){
try{
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
private static DataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource("democ3p0");
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
DAO接口
/**
* Created by IntelliJ IDEA.
*
* @version : 1.0
* @auther : Firewine
* @mail : 1451661318@qq.com
* @Program Name: <br>
* @Create : 2018-11-16-17:43
* @Description : <br/>
*/
public interface bookDao {
/**
* 保存书本信息到数据库中
* @param book
* @return
*/
public boolean save(Book book);
/**
* 查询数据库的表中的一本书
* @param name
* @return 返回book的对象
*/
public Book findBook(String name);
/**
* 获取数据库中的所有的书
* @return 返回Book的列表
*/
public List<Book> findAll();
}
VO类
/**
* Created by IntelliJ IDEA.
*
* @version : 1.0
* @auther : Firewine
* @mail : 1451661318@qq.com
* @Program Name: <br>
* @Create : 2018-11-16-13:38
* @Description : <br/>
*/
public class Book {
private String bookName;
private String bookAuther;
private String bookPrice;
private String bookInfo;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuther() {
return bookAuther;
}
public void setBookAuther(String bookAuther) {
this.bookAuther = bookAuther;
}
public String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookInfo() {
return bookInfo;
}
public void setBookInfo(String bookInfo) {
this.bookInfo = bookInfo;
}
@Override
public String toString() {
return "book{" +
"bookName='" + bookName + '\'' +
", bookAuther='" + bookAuther + '\'' +
", bookPrice='" + bookPrice + '\'' +
", bookInfo='" + bookInfo + '\'' +
'}';
}
public Book(String bookName, String bookAuther, String bookPrice, String bookInfo) {
this.bookName = bookName;
this.bookAuther = bookAuther;
this.bookPrice = bookPrice;
this.bookInfo = bookInfo;
}
public Book() {
}
}
DAO接口的实现
/**
* Created by IntelliJ IDEA.
*
* @version : 1.0
* @auther : Firewine
* @mail : 1451661318@qq.com
* @Program Name: <br>
* @Create : 2018-11-16-19:26
* @Description : <br/>
*/
public class bookDaoImpl implements bookDao {
public boolean save(Book book) {
String sql = "INSERT INTO book (bookName,bookAuther,bookPrice,bookInfo) VALUES (?,?,?,?);";
Connection connection = null;
PreparedStatement preparedStatement = null;
Boolean result = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,book.getBookName());
preparedStatement.setString(2,book.getBookAuther());
preparedStatement.setString(3,book.getBookPrice());
preparedStatement.setString(4,book.getBookInfo());
if (preparedStatement.executeUpdate() != 0){
result = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCTools.release(null,preparedStatement,connection);
}
return result;
}
public Book findBook(String name) {
String sql = "SELECT * FROM book where bookName = ?;";
Book book = new Book();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
book.setBookName( resultSet.getString(1));
book.setBookAuther(resultSet.getString(2));
book.setBookPrice(resultSet.getString(3));
book.setBookInfo( resultSet.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(resultSet,preparedStatement,connection);
}
return book;
}
public List<Book> findAll() {
List<Book> bookList = new ArrayList<Book>();
String sql = "SELECT * FROM book ;";
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
Book book = new Book();
book.setBookName(resultSet.getString("bookName"));
book.setBookAuther(resultSet.getString("bookAuther"));
book.setBookPrice(resultSet.getString("bookPrice"));
book.setBookInfo(resultSet.getString("bookInfo"));
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(resultSet,preparedStatement,connection);
}
return bookList;
}
}
DAO工厂
/**
* Created by IntelliJ IDEA.
*
* @version : 1.0
* @auther : Firewine
* @mail : 1451661318@qq.com
* @Program Name: <br>
* @Create : 2018-11-16-20:16
* @Description : <br/>
*/
public class DaoFactory {
public static bookDao getbookDaoInstance(){
return new bookDaoImpl();
}
}