一、验证Dao层
- 创建AreaDao接口
package com.lzx.o2o.dao;
import java.util.List;
import com.lzx.o2o.entity.Area;
public interface AreaDao {
/**
* 列出区域列表
*
* @return areaList
*/
List<Area> queryArea();
}
- 在mapper文件夹下创建AreaDao.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.lzx.o2o.dao.AreaDao">
<!-- 查询表tb_area中的area对象,根据priority降序排列 -->
<select id="queryArea" resultType="com.lzx.o2o.entity.Area"
parameterType="com.lzx.o2o.entity.Area">
SELECT
area_id,area_name,priority,create_time,last_edit_time FROM tb_area
ORDER BY priority DESC
</select>
</mapper>
- 在src/test/java下创建基础测试类BaseTest.java
package com.lzx.o2o;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
*
* 配置spring和junit整合,junit启动时加载springIOC容器
*
*/
//告诉我们用这个类(SpringJUnit4ClassRunner)跑单元测试
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件的位置
@ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" })
public class BaseTest {
}
- 验证AreaDaoTest.java
package com.lzx.o2o.dao;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzx.o2o.BaseTest;
import com.lzx.o2o.entity.Area;
public class AreaDaoTest extends BaseTest {
@Autowired
private AreaDao areaDao;
@Test
public void testQueryArea() {
List<Area> areaList = areaDao.queryArea();
assertEquals(2, areaList.size());
}
}
二、验证Service层
- 创建AreaService接口
package com.lzx.o2o.service;
import java.util.List;
import com.lzx.o2o.entity.Area;
import com.lzx.o2o.service.AreaService;
public interface AreaService {
List<Area> getAreaList();
}
- 创建AreaServiceImpl实现类
package com.lzx.o2o.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lzx.o2o.dao.AreaDao;
import com.lzx.o2o.entity.Area;
import com.lzx.o2o.service.AreaService;
//告诉SpringIOC,AreaServiceImpl这个实现类需要你去托管
@Service
public class AreaServiceImpl implements AreaService {
// 希望程序自动将AreaDao方法的实现自动注入到spring容器
@Autowired
private AreaDao areaDao;
@Override
public List<Area> getAreaList() {
return areaDao.queryArea();
}
}
- 验证AreaServiceTest.java
package com.lzx.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.lzx.o2o.BaseTest;
import com.lzx.o2o.entity.Area;
public class AreaServiceTest extends BaseTest {
// 一旦我们的AreaServiceTest类用到成员变量时,Spring容器就会往里注入AreaService的实现类AreaServiceImpl,因为我们已经在AreaServiceImpl上写了注解@Service
@Autowired
private AreaService areaService;
@Test
public void testGetAreaList() {
List<Area> areaList = areaService.getAreaList();
assertEquals("西苑", areaList.get(0).getAreaName());
}
}
三、验证Controller层
- 创建AreaController.java
package com.lzx.o2o.web.superadmin;
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.lzx.o2o.entity.Area;
import com.lzx.o2o.service.AreaService;
//表明在Spring容器下,这个是一个Controller
@Controller
//与路由相关,如果我们想要调用Controller下的方法的话,必须在 superadmin 这个路径下调用
@RequestMapping("/superadmin")
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping(value = "/listarea", method = RequestMethod.GET)
// 告诉Controller我们返回的数据对象modelMap自动转换成json数据
@ResponseBody
private Map<String, Object> listArea() {
Map<String, Object> modelMap = new HashMap<String, Object>();
List<Area> list = new ArrayList<Area>();
try {
list = areaService.getAreaList();
modelMap.put("rows", list);
modelMap.put("total", list.size());
} catch (Exception e) {
e.printStackTrace();
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
}
return modelMap;
}
}
- 验证AreaController.java
地址栏输入:http://localhost:8080/o2o/superadmin/listarea