码迷,mamicode.com
首页 > 其他好文 > 详细

校园商铺-7商品类别模块-2商品类别列表从后到前

时间:2020-03-06 21:49:21      阅读:73      评论:0      收藏:0      [点我收藏+]

标签: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;
}

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层

"

校园商铺-7商品类别模块-2商品类别列表从后到前

标签:ota   ide   desc   idt   sys   override   printf   建立   create   

原文地址:https://www.cnblogs.com/csj2018/p/12430891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!