构造url测试action与dao实例
来源:互联网 发布:澳大利亚土木工程知乎 编辑:程序博客网 时间:2024/05/16 11:49
从页面获取操作粒度和开始与结束时间,统计男女比例。
最初打算在manager里面测试dao层,但是发现无论是在action里或则manager里都无法测试成功,我采用的junit测试,字面含义应该是单元测试。所以很可能当调用到其他单元的时候就无法达到想要的目的。
后来知道了测试应该是构造URL来传入参数来测试整个dao层和业务逻辑,最开始没有正确编码mapper导致整个项目无法跑起来,对于整个团队还是应该自己测试后再提交否则整个项目都会报错。我们是通过传入queryMap的Map类型的参数来实现查询的。在mapper里的paramtype为java.util.map类型。在mapper里查询的时候从map里取出名字的值来动态注入。因为对名字的要求很严格,一定要做到命名的规范。
在已经成功在URL里构造参数返回到页面JSON了,但是更新了SVN之后发现报bean.xml错误,后来查看了Spring的数据库连接properties文件才发现其他开发人员把连接的数据库改掉了,可能是测试吧,所以我改成了我需要连接的数据库名称重启,错误得到了解决。
在mapper里联合查询的别名字段都是对应mapper文件里的sql语句里的,并不是对应数据库里的。这里可以参考这个系列的mybatis前几章节的教程。mybatis实战
在mapper里存在着类型的转换,最开始我在navicat的console里查询:varchar类型和int类型可以不用添加单引号,但是日期类型必须要加加单引号,否则无法达到想要的效果。
构造url:
在项目名称下按照convention与struts的命名规则进行名称改变,请求action会默认把驼峰的第一个改成小写,后面不变,并且把后缀改成.action,请求在页面显示出json的话就在驼峰之间使用-,并且都改成小写,然后就会在页面返回结果了。例如我这次构造的url:http://localhost:8080/DSP-MONITOR/report/tourist-gender!display.action?areaId=10001&startTime=2015-07-20&endTime=2015-10-20 !为方法请求,?后面为提交参数,&为多个参数的连接符。下面贴出这次从url获取数据到dao的实例:
/***** @File: TouristGenderAction.java* @Date: Mar 2, 2015* @Author: micro_hz*/@SuppressWarnings("serial")@Results({ @Result(name = WebActionSupport.RELOAD, location = "tourist-gender!display.action", type = "redirect"), @Result(name = WebActionSupport.ERROR, location = "../../common/error.jsp") })public class TouristGenderAction extends WebActionSupport{ private static final Logger logger = LoggerFactory.getLogger(SourceCityAction.class); @Override public String execute() { return SUCCESS; } public void display() { String areaId = ParamUtil.getFilteredParameter(request, "areaId", 0 ,"10000");// 景区id String type = ParamUtil.getFilteredParameter(request, "kpiCycle", 0 ,"3"); Integer kpiCycle = Integer.parseInt(type); String startTime = ParamUtil.getFilteredParameter(request, "startTime", 0 ,"2013-02-01"); String endTime = ParamUtil.getFilteredParameter(request, "endTime", 0 ,"2015-05-01"); //定义所有游客列表和男女游客列表 List<TouristGender> allTouristGenderList = null;// List<TouristGender> femaleList = null;// List<TouristGender> maleList = null; float femalePopulation = 0; float malePopulation = 0; float otherPopulation = 0;//性别为其他的时候 保留 float sumPopulation = 0; Map<String,Object> queryMap = new HashMap<String,Object>(); queryMap.put("areaId", areaId); //按天统计 if(kpiCycle == Constants.VISITOR_COUNT_UNIT_DAY) { queryMap.put("startTime", startTime); queryMap.put("endTime", endTime); allTouristGenderList = touristGenderManager.getGenderByDay(queryMap); } //按月统计 if(kpiCycle == Constants.VISITOR_COUNT_UNIT_MONTH) { queryMap.put("startTime", startTime); queryMap.put("endTime", endTime); allTouristGenderList = touristGenderManager.getGenderByMonth(queryMap); } //获取所有游客男女和总人数 for(TouristGender touristGender : allTouristGenderList) { if(touristGender.getGender().endsWith("female")) { femalePopulation += touristGender.getPopulation(); } else if(touristGender.getGender().endsWith("male")) { malePopulation += touristGender.getPopulation(); } else { otherPopulation += touristGender.getPopulation(); } sumPopulation += touristGender.getPopulation(); } //男女占比 float femalePercent = (double) (femalePopulation / sumPopulation); float malePercent = (double) (malePopulation / sumPopulation); float otherPercent = (double) (otherPopulation / sumPopulation); //结果 Map<String,Object> resultMap = new HashMap<String,Object>(); resultMap.put("femalePoplation", femalePopulation); resultMap.put("malePopulation", malePopulation); resultMap.put("otherPopulation", otherPopulation); resultMap.put("sumPopulation", sumPopulation); resultMap.put("femalePercent", femalePercent); resultMap.put("malePercent", malePercent); resultMap.put("otherPercent", otherPercent); //返回JSON JSONObject resultJson = new JSONObject(); resultJson.put("GenderAnalysis", resultMap); returnJSON(resultJson.toString()); }}
mapper:
<?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.cmcc.monitor.dao.data.TouristGenderDao"> <sql id="Base_Column_List"> id, area_id, date_time, gender, population </sql> <resultMap type="com.cmcc.monitor.entity.data.TouristGender" id="GenderResultMap"> <result column="id" property="id" jdbcType="INTEGER" /> <result column="area_id" property="areaId" jdbcType="VARCHAR" /> <result column="date_time" property="dateTime" jdbcType="TIMESTAMP" /> <result column="gender" property="gender" jdbcType="VARCHAR" /> <result column="population" property="population" jdbcType="INTEGER" /> </resultMap> <select id="getGenderByDay" parameterType="java.util.Map" resultMap="GenderResultMap"> select <include refid="Base_Column_List" /> from t_mon_data_gender_day <where> <if test="areaId != null"> area_id = #{areaId} </if> <if test="startTime != null"> <![CDATA[and date_time >= #{startTime}]]> </if> <if test="endTime != null"> <![CDATA[and date_time <= #{endTime}]]> </if> </where> order by date_time </select> <select id="getGenderByMonth" parameterType="java.util.Map" resultMap="GenderResultMap"> select <include refid="Base_Column_List" /> from t_mon_data_gender_month <where> <if test="areaId != null"> area_id = #{areaId} </if> <if test="startTime != null"> <![CDATA[and date_time >= #{startTime}]]> </if> <if test="endTime != null"> <![CDATA[and date_time <= #{endTime}]]> </if> </where> order by date_time </select></mapper>
由于一些原因manager及其实现和dao我就不贴出来了,大概的逻辑就是
action都会继承WebActionSupport,在这里面会注入manager,manager的实现类又会调用在BaseDao这里面都注入的dao实现数据持久化。
大概如图:
- 构造url测试action与dao实例
- spring DAO 集成测试 实例
- JDBC Dao的实现与测试
- JDBC Dao的实现与测试
- service注入DAO组件 与 Action注入service区别
- Html.Action、html.ActionLink与Url.Action的区别
- 实例构造与结构构造(Java/C#)
- 静态构造函数与实例构造函数
- 实例构造器与类型构造器
- Action+Service +Dao Model
- Action/Service/DAO
- Action(controller) service DAO
- Action/Service/DAO简介
- Action+service+DAO
- Action/Service/DAO
- Action/Service/DAO
- 每日构造与冒烟测试
- 每日构造与冒烟测试
- 【spring框架】(一)spring简介
- 二叉搜索树的后序遍历序列
- java中的多线程问题
- 解决firefox弱临时 Diffie-Hellman 密钥的错误
- Hadoop常见错误及解决办法汇总
- 构造url测试action与dao实例
- 服务器硬件/系统信息查询
- 面试题16:翻转链表
- HDU 3743 Frosh Week(逆序对-BIT)
- liunx-fastboot命令行的使用方法
- java中将file文件对象转string
- 一个大公司程序员的牢骚
- 抗锯齿
- 强大的vim配置文件,让编程更随意