SSM到SpringBoot(校园商铺)
验证配置的正确性:
使用Area来测试
1、创建AreaDao,新增查询方法
package com.hytxwz.o2o.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.hytxwz.o2o.entity.Area;
@Repository("areaDao")
public interface AreaDao {
//查询区域列表
List<Area> queryArea();
}
2、创建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.hytxwz.o2o.dao.AreaDao">
<!-- 查询区域列表 -->
<select id="queryArea" resultType="Area">
SELECT area_id, area_name, priority
FROM tb_area
ORDER BY priority DESC
</select>
</mapper>
3、在数据库tb_area表中添加数据
4、创建AreaDaoTest
首先创建父类BaseTest
package com.hytxwz.o2o.test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author 河堐头小王子
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-*.xml")
public class BaseTest {
}
创建AreaDaoTest 继承BaseTest
package com.hytxwz.o2o.dao;
import static org.junit.Assert.assertEquals;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import com.hytxwz.o2o.entity.Area;
import com.hytxwz.o2o.test.BaseTest;
//AreaDao的测试类
public class AreaDaoTest extends BaseTest {
@Resource(name="areaDao")
private AreaDao areaDao;
@Test
public void testAreaDao() {
List<Area> list = areaDao.queryArea();
assertEquals(2, list.size());
}
}
5、运行测试;
6、创建AreaService和AreaServiceImpl
package com.hytxwz.o2o.service;
import java.util.List;
import com.hytxwz.o2o.entity.Area;
public interface AreaService {
List<Area> getAreaList();
}
@Service("areaService")
public class AreaServiceImpl implements AreaService {
@Resource(name = "areaDao")
private AreaDao areaDao;
@Override
public List<Area> getAreaList() {
return areaDao.queryArea();
}
}
7、在src/test/java下的com.hytxwz.o2o.service包中创建AreaServiceTest
package com.hytxwz.o2o.service;
import static org.junit.Assert.assertEquals;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import com.hytxwz.o2o.entity.Area;
import com.hytxwz.o2o.test.BaseTest;
public class AreaServiceTest extends BaseTest {
@Resource(name = "areaService")
private AreaService areaService;
@Test
public void testAreaService() {
List<Area> list = areaService.getAreaList();
assertEquals(2, list.size());
}
}
8、运行测试;
9、创建AreaController
@Controller
@RequestMapping("/superadmin")
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping(value = "/listarea", method = RequestMethod.GET)
@ResponseBody //以JSON形式返回结果
public Map<String, Object> getAreaList() {
Map<String, Object> modelMap = new HashMap<>();
List<Area> areaList = new ArrayList<>();
try {
areaList = areaService.getAreaList();
//得到结果后将areaList和areaList.size封装到modelMap中
modelMap.put("rows", areaList);
modelMap.put("total", areaList.size());
} catch (Exception e) {
//错误 将错误日志返回
e.printStackTrace();
modelMap.put("success", false);
modelMap.put("errorMsg", e.toString());
}
return modelMap;
}
}
10、运行测试: