一、Dao层
1、HeadLineDao接口
public interface HeadLineDao {
List<HeadLine> queryHeadLine(@Param("headLineCondition") HeadLine headLineCondition);
}
2、Dao实现类
<?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.HeadLineDao">
<select id="queryHeadLine" resultType="com.imooc.o2o.entity.HeadLine">
SELECT
line_id,
line_name,
line_link,
line_img,
priority,
enable_status,
create_time,
last_edit_time
FROM
tb_head_line
<where>
<if test="headLineCondition.enableStatus!=null">
and enable_status = #{headLineCondition.enableStatus}
</if>
</where>
ORDER BY
priority DESC
</select>
</mapper>
3、ShopCategoryDao.xml 的补充--使其支持查询parentId为空的情况
<?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 null
</if>
<if test="shopCategoryCondition!=null">
and parent_id is not null
</if>
<if test="shopCategoryCondition != null and shopCategoryCondition.parent!=null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if>
</where>
ORDER BY
priority DESC
</select>
</mapper>
二、service层
1、HeadLineService 接口
package com.imooc.o2o.service;
import java.io.IOException;
import java.util.List;
import com.imooc.o2o.entity.HeadLine;
public interface HeadLineService {
/**
* 根据传入的条件返回指定的头条列表
* @param headLineCondition
* @return
* @throws IOException
*/
List<HeadLine> getHeadLineList(HeadLine headLineCondition) throws IOException;
}
2、HeadLineServiceImpl 实现类
@Service
public class HeadLineServiceImpl implements HeadLineService {
@Autowired
private HeadLineDao headLineDao;
@Override
public List<HeadLine> getHeadLineList(HeadLine headLineCondition) throws IOException {
return headLineDao.queryHeadLine(headLineCondition);
}
}
三、controller层
1、MainPageController
package com.imooc.o2o.web.frontend;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.imooc.o2o.entity.HeadLine;
import com.imooc.o2o.entity.ShopCategory;
import com.imooc.o2o.service.HeadLineService;
import com.imooc.o2o.service.ShopCategoryService;
@Controller
@RequestMapping("/frontend")
public class MainPageController {
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private HeadLineService headLineService;
/**
* 初始化前端展示系统的主页信息,包括获取一级店铺类别列表及头条列表
* @return
*/
@RequestMapping(value="/listmainpageinfo",method=RequestMethod.GET)
@ResponseBody
private Map<String,Object> listMainPageInfo(){
Map<String,Object> modelMap = new HashMap<String, Object>();
List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
try {
//获取一级店铺类别列表(即parentId为空的ShopCategory)
shopCategoryList = shopCategoryService.getShopCaregoryList(null);
modelMap.put("shopCategoryList", shopCategoryList);
}catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
List<HeadLine> headLineList = new ArrayList<HeadLine>();
try {
// 获取状态为可用(1)的头条列表
HeadLine headLineCondition = new HeadLine();
headLineCondition.setEnableStatus(1);
headLineList = headLineService.getHeadLineList(headLineCondition);
modelMap.put("headLineList", headLineList);
}catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
modelMap.put("success",true);
return modelMap;
}
}
四、测试
在网页输入 http://localhost:8080/o2oDemo/frontend/listmainpageinfo