4-11 ***店铺类别区域信息的获取

一、Dao层

1、创建ShopCategoryDao接口,shopCategoryCondition是ShopCategory的名字

public interface ShopCategoryDao {
	List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition") ShopCategory shopCategory);
//shopCategoryCondition为参数名,ShopCategory为传入参数类别
	
}

解释括号里 使用@Param注解进行参数绑定

例:

public interface PaperDao {
    Paper queryById(@Param("id") long id,@Param("name") String name);}

xml文件:

<select id="queryById" parameterType="long" resultMap="resultMap1">
    SELECT paper_id,name,number,detail
    FROM paper
    WHERE paper_id=#{id} AND name=#{name}
</select>

2、创建ShopCategoryDao.xml 进行实现

红框中的shopCategoryCondition相当于接口中@param(shopCategoryCondition),即ShopCategory,然后取ShopCategory的parent属性的shopCategoryId


看完controller的编写再看这里

该项目中店铺类别都是在二级菜类别以内的(首页是一级类别),因此店铺所属的店铺类别的parent_id是非空的,所以需要选出parent_id不为空的类别供店铺来选择,要在XML文件里加一个<if>标签

<?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.ShopCategoryDao"> 
    	<select id="queryShopCategory" resultType="com.imooc.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!=null">
    				and parent_id is not null
    			</if>
    		</where>
    		<where>
    			<if test="shopCategoryCondition.parent!=null">
    				and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
    			</if>
    		</where>
    		ORDER BY
    		priority DESC
    	</select>
    </mapper>

在controller中传入了对象

符合mapper中的条件1,选出parent不为空的类别

<if test="shopCategoryCondition!=null">
    and parent_id is not null
</if>

3、测试

public class ShopCategoryDaoTest extends BaseTest{
	@Autowired
	private ShopCategoryDao shopCategoryDao;
	@Test
	public void testQueryShopCategory() {
		//传入空对象
		List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(new ShopCategory());
		assertEquals(2, shopCategoryList.size());
		//传入父对象
		ShopCategory testCategory = new ShopCategory();
		ShopCategory parentCategory = new ShopCategory();
		parentCategory.setShopCategoryId(1L);
		testCategory.setParent(parentCategory);
		shopCategoryList = shopCategoryDao.queryShopCategory(testCategory);
		assertEquals(1, shopCategoryList.size());
		System.out.println(shopCategoryList.get(0).getShopCategoryName());//输出咖啡
	}
}

二、service层 

1、创建接口

public interface ShopCategoryService {
	List<ShopCategory> getShopCaregoryList(ShopCategory shopCategoryCondition);
}

2、创建接口实现类

@Service
public class ShopCategoryServiceImpl implements ShopCategoryService{
	@Autowired
	private ShopCategoryDao shopCategoryDao;
	@Override
	public List<ShopCategory> getShopCaregoryList(ShopCategory shopCategoryCondition) {
		return shopCategoryDao.queryShopCategory(shopCategoryCondition);
	}
}

三、controller层

获取区域及店铺类别信息返回前台

继续编写ShopManagementController,添加getShopInitInfo()方法

@RequestMapping(value="/getshopinitinfo",method=RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getShopInitInfo(){
		//获取区域及店铺类别信息返回前台
		Map<String,Object> modelMap = new HashMap<String,Object>();
		List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
		List<Area> areaList = new ArrayList<Area>();
		try {
			shopCategoryList = shopCategoryService.getShopCaregoryList(new ShopCategory());
			areaList = areaService.getAreaList();
			modelMap.put("shopCategoryList", shopCategoryList);
			modelMap.put("areaList", areaList);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}

@ResponseBody负责将返回的modelMap转为JSON串

猜你喜欢

转载自blog.csdn.net/weixin_40703303/article/details/89330315
今日推荐