9-1 首页后台的开发

一、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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值