文章目录
-
-
- 1. Dao层的实现
-
- 1. HeadLineDao接口
- 2. HeadLineDao.xml实现类
- 3. HeadLineDaoTest实现类
- 2. Service层的实现
-
- 2.1 HeadLineService接口
- 2.2 HeadLineServiceImpl实现类
- 3. 完善ShopCategoryDao接口
-
- 3.1 ShopCategoryDao.xml
- 3.2 ShopCategoryDaoTest测试类
- 4. Controller层的实现
-
1. Dao层的实现
1. HeadLineDao接口
public interface HeadLineDao {
/**
* 根据传入的查询条件进行查询
* @param headLineCondition
* @return
*/
List<HeadLine> queryHeadLine(@Param("headLineCondition")HeadLine headLineCondition);
}
2. HeadLineDao.xml实现类
<?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. HeadLineDaoTest实现类
public class HeadLIneDaoTest extends BaseTest {
@Autowired
private HeadLineDao headLineDao;
@Test
public void testQueryHeadLine(){
List<HeadLine> headLineList = headLineDao.queryHeadLine(new HeadLine());
assertEquals(2,headLineList.size());
}
}
2. Service层的实现
2.1 HeadLineService接口
public interface HeadLineService {
/**
* 根据查询条件返回指定的头条列表
* @param headLineCondition
* @return
*/
List<HeadLine> getShopCategoryList(HeadLine headLineCondition);
}
2.2 HeadLineServiceImpl实现类
@Service
public class HeadLineServiceImpl implements HeadLineService {
@Autowired
private HeadLineDao headLineDao;
@Override
public List<HeadLine> getShopCategoryList(HeadLine headLineCondition) {
return headLineDao.queryHeadLine(headLineCondition);
}
}
3. 完善ShopCategoryDao接口
3.1 ShopCategoryDao.xml
<select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
select
shop_category_id,
shop_category_name,
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>
3.2 ShopCategoryDaoTest测试类
public class ShopCategoryDaoTest extends BaseTest {
@Autowired
private ShopCategoryDao shopCategoryDao;
@Test
public void testQueryShopCategory() {
List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(null);
System.out.println(shopCategoryList.size());//1
}
}
4. Controller层的实现
@Controller
@RequestMapping("/frontend")
public class MainPageController {
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private HeadLineService headLineService;
@ResponseBody
@RequestMapping(value = "/listmainpageinfo",method = RequestMethod.GET)
private Map<String,Object> listMainPageInfo(){
Map<String,Object> modelMap = new HashMap<>();
try {
//获取一级店铺类别列表(productId为空的ShopCategory)
List<ShopCategory> shopCategoryList = new ArrayList<>();
shopCategoryList = shopCategoryService.getShopCategoryList(null);
modelMap.put("shopCategoryList",shopCategoryList);
} catch (Exception e) {
modelMap.put("success",false);
modelMap.put("errMsg",e.getMessage());
return modelMap;
}
//获取状态为1可用的头条列表
List<HeadLine> headLineList = new ArrayList<>();
try {
HeadLine headLineCondition = new HeadLine();
headLineCondition.setEnableStatus(1);
headLineList = headLineService.getShopCategoryList(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;
}
}