标签:ota ide desc idt sys override printf 建立 create
商品类别模块为什么使用shopId,而不是Shop实体类?
因为我们获取productCategory时,并不需要获取除了shopId之外的信息,因此不用Shop实体类。
public class ProductCategory {
    private Long productCategoryId;
    private Long shopId;
    private String productCategoryName;
    private Integer priority;
    private Date createTime;
}
insert into tb_product_category(`product_category_name`,`priority`,`shop_id`)
    values('店铺类别1',0,1),('店铺类别2',20,1),('店铺类别3',2,1);
package com.csj2018.o2o.dao;
import java.util.List;
import com.csj2018.o2o.entity.ProductCategory;
public interface ProductCategoryDao {
    List<ProductCategory> queryProductCategoryList(long shopId);
}
<?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>
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());
    }
}

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);
}
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);
    }
}
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());
        }
    }
}
"
标签:ota ide desc idt sys override printf 建立 create
原文地址:https://www.cnblogs.com/csj2018/p/12430891.html