店铺类别
dao层
package com.csj2018.o2o.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csj2018.o2o.entity.ShopCategory;
public interface ShopCategoryDao {
List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition") ShopCategory shopCategoryCondition);
List<ShopCategory> queryAllShopCategory();
}
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.ShopCategoryDao">
<select id="queryShopCategory" resultType="com.csj2018.o2o.entity.ShopCategory">
select shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
from tb_shop_category
<where>
<if test="shopCategoryCondition.parent != null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if>
</where>
order by priority desc
</select>
<select id="queryAllShopCategory" resultType="com.csj2018.o2o.entity.ShopCategory">
select shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
from tb_shop_category order by priority desc
</select>
</mapper>
验证Dao
package com.csj2018.o2o.dao;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.ShopCategory;
public class ShopCatetoryDaoTest extends BaseTest{
@Autowired
private ShopCategoryDao shopCategoryDao;
@Test
public void testQueryShopCategory() {
List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(new ShopCategory());
System.out.println(shopCategoryList.size());
ShopCategory parent = new ShopCategory();
ShopCategory son = new ShopCategory();
parent.setShopCategoryId(1L);
son.setParent(parent);
List<ShopCategory> shopCategoryList2 = shopCategoryDao.queryShopCategory(son);
}
@Test
@Ignore
public void testAllCategory() {
List<ShopCategory> shopCategoryList = shopCategoryDao.queryAllShopCategory();
}
}
Service层
package com.csj2018.o2o.service;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.csj2018.o2o.entity.ShopCategory;
public interface ShopCategoryService {
List<ShopCategory> getShopCategoryList(ShopCategory queryShopCategory);
}
Service实现层
package com.csj2018.o2o.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.csj2018.o2o.dao.ShopCategoryDao;
import com.csj2018.o2o.entity.ShopCategory;
import com.csj2018.o2o.service.ShopCategoryService;
@Service
public class ShopCategoryServiceImpl implements ShopCategoryService{
@Autowired
private ShopCategoryDao shopCategoryDao;
@Override
public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) {
// TODO Auto-generated method stub
return shopCategoryDao.queryShopCategory(shopCategoryCondition);
}
}
Service 用例
package com.csj2018.o2o.service;
import static org.junit.Assert.assertEquals;
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.ShopCategory;
public class ShopCategoryServiceTest extends BaseTest{
@Autowired
private ShopCategoryService shopCategoryService;
@Test
public void testGetShopCategoryList() {
ShopCategory parent = new ShopCategory();
List<ShopCategory> shopCategoryList = shopCategoryService.getShopCategoryList(parent);
assertEquals(2,shopCategoryList.size());
ShopCategory son = new ShopCategory();
parent.setShopCategoryId(1L);
son.setParent(parent);
List<ShopCategory> shopCategoryList2 = shopCategoryService.getShopCategoryList(son);
assertEquals(1,shopCategoryList2.size());
}
}