商品类别模块为什么使用shopId,而不是Shop实体类?
因为我们获取productCategory时,并不需要获取除了shopId之外的信息,因此不用Shop实体类。
public class ProductCategory {
private Long productCategoryId;
private Long shopId;
private String productCategoryName;
private Integer priority;
private Date createTime;
}
1.Dao层
insert into tb_product_category(`product_category_name`,`priority`,`shop_id`)
values('店铺类别1',0,1),('店铺类别2',20,1),('店铺类别3',2,1);
1.1 建立Dao文件
package com.csj2018.o2o.dao;
import java.util.List;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryDao {
List<ProductCategory> queryProductCategoryList(long shopId);
}
1.2.建立mapper文件
<?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.csj2018.o2o.dao.ProductCategoryDao">
<select id="queryProductCategoryList" resultType="com.csj2018.o2o.entity.ProductCategory" parameterType="Long">
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>
</mapper>
1.3.对Dao进行单元测试
package com.csj2018.o2o.dao;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
public class ProductCategoryDaoTest extends BaseTest {
@Autowired
private ProductCategoryDao productCategoryDao;
@Test
public void testQueryByShopId() throws Exception {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
System.out.println("该店铺自定义商品类别数为:"+productCategoryList.size());
}
}
2.Service层
2.1建立Service接口
package com.csj2018.o2o.service;
import java.util.List;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryService {
/**
* 查询指定某个店铺下的所有商品类别信息
* @param shopId
* @return
*/
List<ProductCategory> getProductCategoryList(long shopId);
}
2.2 建立Service实现类
package com.csj2018.o2o.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.dao.ProductCategoryDao;
import com.csj2018.o2o.entity.ProductCategory;
import com.csj2018.o2o.service.ProductCategoryService;
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Autowired
private ProductCategoryDao productCategoryDao;
@Override
public List<ProductCategory> getProductCategoryList(long shopId) {
// TODO Auto-generated method stub
return productCategoryDao.queryProductCategoryList(shopId);
}
}
2.3 对Service层进行单元测试
package com.csj2018.o2o.service;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ProductCategory;
public class ProductCategoryServiceTest extends BaseTest{
@Autowired
ProductCategoryService productCategoryService;
@Test
public void testGetbyShopid() {
long shopId = 1;
List<ProductCategory> productCategoryList = productCategoryService.getProductCategoryList(shopId);
for(ProductCategory pc:productCategoryList) {
System.out.printf("%s 权重:%s",pc.getProductCategoryName(),pc.getPriority());
}
}
}
3.controller层
3.1 建立controller层
"
原文地址:https://www.cnblogs.com/csj2018/p/12430891.html
时间: 2024-11-10 00:49:09