The saddest thing is that after |

柒木木木

园龄:2年8个月粉丝:9关注:0

BasicDAO 设计模式

1.BasicDAO设计模式

在开发中需要用到多个表,BasicDAO设计模式可以对多个表进行管理,增加开发效率。

1.1 BasicDAO 设计模式图解

image
image

1.2 BasicDAO 设计模式的文件目录

实际开发中,这些目录会有很多。

image

2. BasicDAO设计模式小案例

2.1 项目需求

image

2.2 创建goods数据库
用到的SQL语句
CREATE DATABASE goods;
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
goods_name VARCHAR(10),
price DOUBLE
)CHARSET=utf8;
SELECT *FROM goods;
DROP TABLE goods;
SELECT COUNT(*) FROM goods;
SELECT goods_name FROM goods
2.3 创建BasicDAO类

在dao目录下创建BasicDAO类,类型为泛型

package com.dz.dao.dao;
import com.dz.jdbc.utilis.JDBCUtils;
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;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class BasicDAO<T> {
private QueryRunner queryRunner = new QueryRunner();
public int update(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtils.connection();
int update = queryRunner.update(connection, sql, parameters);
return update;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, connection);
}
}
public List<T> QueryMulti(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtils.connection();
return queryRunner.query(connection, sql, new BeanListHandler<>(clazz), parameters);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, connection);
}
}
public T single(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtils.connection();
return queryRunner.query(connection, sql, new BeanHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, connection);
}
}
public Object Scalar(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtils.connection();
return queryRunner.query(connection,sql, new ScalarHandler<T>(), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, connection);
}
}
}
2.4 创建Goods类

对应goods数据库的字段

package com.dz.dao.domain;
public class Goods {
private Integer id;
private String goods_name;
private Double price;
public Goods() {
}
public Goods(Integer id, String goods_name, Double price) {
this.id = id;
this.goods_name = goods_name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "\nGoods{" +
"id=" + id +
", goods_name='" + goods_name + '\'' +
", price=" + price +
'}';
}
}
2.5 创建GoodsDAO类

创建GoodsDAO类,并继承BasicDAO类,类型为Goods.

package com.dz.dao.dao;
import com.dz.dao.domain.Goods;
public class GoodsDAO extends BasicDAO<Goods> {
//根据需求可以,增加特有的方法。
}
2.5 创建TestDAO测试类

完成对goods表的增删改查操作

package com.dz.dao.test;
import com.dz.dao.dao.GoodsDAO;
import com.dz.dao.domain.Goods;
import org.junit.Test;
import java.util.List;
public class TestDAO {
@Test
public void testGoods() {
GoodsDAO goodsDAO = new GoodsDAO();
String sql = "insert into goods value (null,?,?)";
int affectedRow = goodsDAO.update(sql, "小米11", "2500.0");
System.out.println(affectedRow > 0 ? "执行成功" : "执行没有影响数据库");
String sql1 = "select * from goods";
List<Goods> goods = goodsDAO.QueryMulti(sql1, Goods.class);
for (Goods actor : goods) {
System.out.print(actor);
}
System.out.println("查询单行记录");
String sql2 = "select * from goods where id=?";
Object single = goodsDAO.single(sql2, Goods.class, 1);
System.out.print(single);
System.out.println("查询单行单列");
String sql3 = "select goods_name from goods where id=?";
Object scalar = goodsDAO.Scalar(sql3,3);
System.out.print(scalar);
}
}

本文作者:柒木木木

本文链接:https://www.cnblogs.com/qimu666/articles/16600226.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   柒木木木  阅读(21)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起