package com.gw.bookstroe.dao.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.gw.bookstroe.dao.ProductDao;
import com.gw.bookstroe.domain.Order;
import com.gw.bookstroe.domain.OrderItem;
import com.gw.bookstroe.domain.Product;
import com.gw.bookstroe.utils.DataSourceUtils;
public class ProductDaoimpl implements ProductDao {
//0.创建QueryRunner对象
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
//1. 添加商品
@Override
public void addProduct(Product p) throws SQLException{
String sql = "insert into products values(?,?,?,?,?,?,?)";
runner.update(sql, p.getId(), p.getName(), p.getPrice(),
p.getCategory(), p.getPnum(), p.getImgurl(), p.getDescription());
}
// 2.查找所有商品
@Override
public List<Product> listAll() throws SQLException{
String sql = "select * from products";
return runner.query(sql, new BeanListHandler<Product>(Product.class));
}
// 3.根据id查找商品
@Override
public Product findProductById(String id) throws SQLException {
String sql = "select * from products where id=?";
return runner.query(sql, new BeanHandler<Product>(Product.class), id);
}
// 4.多条件查询
@Override
public List<Product> findProductByManyCondition(String id, String name, String category, String minprice,
String maxprice) throws SQLException{
//List<Object>是Object的参数化类型,其引用变量可以接受List,可以添加元素
List<Object> list = new ArrayList<Object>();
//1.构造存在where,但是条件又根据不同情况而不同的
String sql = "select * from products where 1=1 ";
//2.创建sql语句,并拼接sql
if (id != null && id.trim().length() > 0) {
sql += " and id=?";
list.add(id);
}
//2.名字进行模糊查询
if (name != null && name.trim().length() > 0) {
sql += " and name like ?";
list.add("%"+name+"%");
}
if (category != null && category.trim().length() > 0) {
sql += " and category=?";
list.add(category);
}
if (minprice != null && maxprice != null
&& minprice.trim().length() > 0 && maxprice.trim().length() > 0) {
sql += " and price between ? and ?";
list.add(minprice);
list.add(maxprice);
}
//3.把参数放入Object类(集合类)
Object[] params = list.toArray();
return runner.query(sql, new BeanListHandler<Product>(Product.class),
params);
}
// 5.修改商品信息
@Override
public void editProduct(Product p) throws SQLException {
// TODO Auto-generated method stub
//1.创建集合并将商品信息添加到集合中
List<Object> obj = new ArrayList<Object>();
obj.add(p.getName());
obj.add(p.getPrice());
obj.add(p.getCategory());
obj.add(p.getPnum());
obj.add(p.getDescription());
//2.创建sql语句,并拼接sql
String sql = "update products " +
"set name=?,price=?,category=?,pnum=?,description=? ";
//3判断是否有图片
if (p.getImgurl() != null && p.getImgurl().trim().length() > 0) {
sql += " ,imgurl=?";
obj.add(p.getImgurl());
}
//4.注意: where id=?要拼接在最后面
sql += " where id=?";
obj.add(p.getId());
//5.使用QueryRunner对象的update()方法更新数据
runner.update(sql, obj.toArray());
}
//6.后台系统,根据id删除商品信息
@Override
public void deleteProduct(String id) throws SQLException{
String sql = "DELETE FROM products WHERE id = ?";
runner.update(sql, id);
}
// 7.销售榜单
@Override
public List<Object[]> salesList(String year, String month) throws SQLException {
String sql = "SELECT products.name,SUM(orderitem.buynum) AS totalsalnum FROM orders,products,orderItem WHERE orders.id=orderItem.order_id AND products.id=orderItem.product_id AND orders.paystate=1 and year(ordertime)=? and month(ordertime)=? GROUP BY products.name ORDER BY totalsalnum DESC";
return runner.query(sql, new ArrayListHandler(), year, month);
}
//8删除订单时,修改商品数量
@Override
public void updateProductNum(List<OrderItem> items) throws SQLException {
String sql = "update products set pnum=pnum+? where id=?";
Object[][] params = new Object[items.size()][2];
for (int i = 0; i < params.length; i++) {
params[i][0] = items.get(i).getBuynum();
params[i][1] = items.get(i).getP().getId();
}
runner.batch( sql, params);
}
// 9.获取数据总条数
@Override
public int findAllCount(String category) throws SQLException {
String sql = "select count(*) from products";
if (!"全部商品".equals(category)) {
sql += " where category=?";
Long count = (Long) runner.query(sql, new ScalarHandler(), category);
return count.intValue();
} else {
Long count = (Long) runner.query(sql, new ScalarHandler());
return count.intValue();
}
}
//10.获取当前页数据
@Override
public List<Product> findByPage(int currentPage, int currentCount, String category) throws SQLException {
// TODO Auto-generated method stub
// 要执行的sql语句
String sql = null;
// 参数
Object[] obj = null;
// 如果category不为null,代表是按分类查找
if (!"全部商品".equals(category)) {
sql = "select * from products where category=? limit ?,?";
obj = new Object[] { category, (currentPage - 1) * currentCount,
currentCount, };
} else {
sql = "select * from products limit ?,?";
obj = new Object[] { (currentPage - 1) * currentCount,
currentCount, };
}
return runner.query(sql, new BeanListHandler<Product>(Product.class),
obj);
}
// 11.生成订单时,将商品数量减少
@Override
public void changeProductNum(Order order) throws SQLException {
String sql = "update products set pnum=pnum-? where id=?";
List<OrderItem> items = order.getOrderItems();
Object[][] params = new Object[items.size()][2];
for (int i = 0; i < params.length; i++) {
params[i][0] = items.get(i).getBuynum();
params[i][1] = items.get(i).getP().getId();
}
runner.batch(DataSourceUtils.getConnection(), sql, params);
}
//12.前台,用于搜索框根据书名来模糊查询相应的图书
@Override
public List<Product> findBookByName(int currentPage, int currentCount, String searchfield) throws SQLException {
//根据名字模糊查询图书
String sql = "SELECT * FROM products WHERE name LIKE '%"+searchfield+"%' LIMIT ?,?";
return runner.query(sql,new BeanListHandler<Product>(Product.class),currentPage-1,currentCount);
}
//13.前台搜索框,根据书名模糊查询出的图书总数量
@Override
public int findBookByNameAllCount(String searchfield) throws SQLException {
String sql = "SELECT COUNT(*) FROM products WHERE name LIKE '%"+searchfield+"%'";
//查询出满足条件的总数量,为long类型
Long count = (Long)runner.query(sql, new ScalarHandler());
return count.intValue();
}
}
2301_76524972
- 粉丝: 0
- 资源: 1
最新资源
- 基于springboot的医患档案管理系统.zip
- 黄金价格预测.xlsx
- 基于springboot的一起来约苗系统.zip
- Python课程大作业-基于深度学习的垃圾分类目标检测系统(前后端源码+运行说明文档).zip
- Python毕业设计-基于深度学习的垃圾分类目标检测系统(前后端源码+运行说明文档).zip
- C#全自动多线程上位机源码编程 0,纯源代码 1,替代传统plc搭载的触摸屏 2,工控屏幕一体机直接和plc通信 3,功能强大,多级页签 4,可以自由设定串口或以太网通信 5,主页 6,报
- 基于STM32的智能路灯系统控制20241212
- 网络搭建与应用赛项省赛竞赛样题1.pdf
- Fluent电弧,激光,熔滴一体模拟 UDF包括高斯旋转体热源、双椭球热源(未使用)、VOF梯度计算、反冲压力、磁场力、表面张力,以及熔滴过渡所需的熔滴速度场、熔滴温度场和熔滴VOF
- 逻辑回归, k-近邻算法,朴素贝叶斯分类器, 支持向量机,决策树,组合分类器,聚类基础,机器学习基础实验,含原理精华、基础实验 关注微信公众号:分享之心,后台回复“机器学习基础实验”获取代码和文档链接
- 一个用于抓取和分析 X (Twitter) 用户数据和推文的工具
- 动画制作(Adobe Animate 2020)
- 抓包工具使用技巧集锦.docx
- 基本文献复现-计及碳排放成本的电-气-热综合能源系统节点能价计算方法研究 真正做到了电热气潮流耦合,很适合综合能源系统建模的初学者,配合复现lunwen 运行程序HeatGasPowerCombin
- 中职组网络搭建与应用赛项(二)网络基础理论测试.rar
- 基于改进Ostu算法的鼠精子图像分割与计数研究
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈