SSM到SpringBoot(校园商铺)5

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、运行测试:
在这里插入图片描述

ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值