SSM校园商铺平台(六)

本文详细介绍了SSM框架下校园商铺平台的实现过程,主要包括dao层、service层、controller层的实现以及前端展示。在service层,使用自定义的ProductCategoryExecution类和ProductCategoryOperationException异常类来处理添加、删除操作,并通过ProductCategoryStateEnum枚举表示操作状态。controller层的实现中,所有方法依赖session获取shopId,确保操作前提。前端部分包括productcategorymanage.html和productcategorymanage.js,成功操作后会刷新数据列表。
摘要由CSDN通过智能技术生成


本节实现商品类别管理

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

  • 添加和删除的实现方法基本一样
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值