验证AreaDao层、AreaService层和AreaController层

一、验证Dao层

  1. 创建AreaDao接口
package com.lzx.o2o.dao;

import java.util.List;

import com.lzx.o2o.entity.Area;

public interface AreaDao {
	/**
	 * 列出区域列表
	 * 
	 * @return areaList
	 */
	List<Area> queryArea();
}
  1. 在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>
  1. 在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 {

}

  1. 验证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层

  1. 创建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();
}

  1. 创建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();
	}

}

  1. 验证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层

  1. 创建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;
	}
}
  1. 验证AreaController.java

地址栏输入:http://localhost:8080/o2o/superadmin/listarea

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值