SSM校园商铺平台(六)
本节实现商品类别管理
1. dao层实现
public interface ProductCategoryDao {
//根据ProductCategory输入的条件查询所有的商品种类
List<ProductCategory> queryProductCategory(long shopId);
//批量新增商品类别,返回值表示新增的数量
int batchInsertProductCategory(List<ProductCategory> productCategoryList);
//删除商品类别
int deleteProductCategory(@Param("productCategoryId")long productCategoryId,@Param("shopId")long shopId);
}
ProductCategoryDao .xml的实现
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.o2o.dao.ProductCategoryDao">
<!-- 查询所有商品类别按shopid-->
<select id="queryProductCategory" resultType="com.imooc.o2o.entity.ProductCategory">
select
product_category_id,
product_category_name,
priority,
create_time,
shop_id
from
tb_product_category
where
shop_id = #{shopId}
order by
priority
desc
</select>
<!-- 批量添加商品类别-->
<insert id="batchInsertProductCategory" parameterType="java.util.List">
insert into
tb_product_category(product_category_name,priority,create_time,shop_id)
values
<foreach collection="list" item="ProductCategory" index="index" separator=",">
(
#{ProductCategory.productCategoryName},
#{ProductCategory.priority},
#{ProductCategory.creatTime},
#{ProductCategory.shopId}
)
</foreach>
</insert>
<!-- 删除商品类别-->
<delete id="deleteProductCategory">
delete from
tb_product_category
where
product_category_id = #{productCategoryId}
and shop_id = #{shopId}
</delete>
</mapper>
2. service层的实现
3.1 ProductCategoryService接口
- 注意:在service层,添加和删除操作返回类型都是ProductCategoryExecution这是自定义的一个类,用来保存返回的状态和状态标识
- 在service层的方法还需要抛出自定义的异常ProductCategoryOperationException,该异常类继承于runtimeException用于添加和删除失败时,对数据库进行回滚
public interface ProductCategoryService {
//查询店铺列表
List<ProductCategory> getProductCategoryList(long shopId);
//批量添加
/**
* 批量添加
* @param productCategoryList
* @return
* @throws ProductCategoryOperationException 自定义一个异常继承于runtimeexception
*/
ProductCategoryExecution batchAddProductCategoryList(List<ProductCategory> productCategoryList)
throws ProductCategoryOperationException;
//删除商品类别
ProductCategoryExecution deleteProductCategory(long productCategoryId,long shopId)
throws ProductCategoryOperationException;
}
3.2 ProductCategoryOperationException自定义异常类
public class ProductCategoryOperationException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ProductCategoryOperationException(String msg) {
super(msg);
}
}
3.3 ProductCategoryExecution 类的实现‘
- 该类用于service层返回值的类型
- 用来标识增加、删除操作是否成功
- 第二个作用是获取操作的数据
- 该类调用了ProductCategoryStateEnum枚举类型,来表示ProductCategory操作的各种状态
public class ProductCategoryExecution {
//结果状态
private int state;
//状态标识
private String stateInfo;
private List<ProductCategory> productCategoryList;
public ProductCategoryExecution() {
}
//操作失败时候的构造器
public ProductCategoryExecution(ProductCategoryStateEnum stateEnum) {
this.state = stateEnum.getState();
this.stateInfo = stateEnum.getStateInfo();
}
//操作成功时候的构造器
public ProductCategoryExecution(ProductCategoryStateEnum stateEnum,List<ProductCategory> productCategoryList) {
this.state = stateEnum.getState();
this.stateInfo = stateEnum.getStateInfo();
this.productCategoryList = productCategoryList;
}
}
3.4 ProductCategoryStateEnum枚举类
package com.imooc.o2o.enums;
public enum ProductCategoryStateEnum {
SUCCESS(1, "创建成功"),
INNER_ERROR(-1001, "操作失败"),
EMPTY_LIST(-1002, "添加数少于1");
private int state;
private String stateInfo;
private ProductCategoryStateEnum(int state, String stateInfo) {
this.state = state;
this.stateInfo = stateInfo;
}
public int getState() {
return state;
}
public String getStateInfo() {
return stateInfo;
}
public static ProductCategoryStateEnum stateOf(int index) {
for (ProductCategoryStateEnum state : values()) {
if (state.getState() == index) {
return state;
}
}
return null;
}
}
3.5 接口的实现类ProductCategoryServiceImpl
- 添加和删除的实现方法基本一样