构造url测试action与dao实例

27 篇文章 0 订阅
23 篇文章 1 订阅

从页面获取操作粒度和开始与结束时间,统计男女比例。

  • 最初打算在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实现数据持久化。
大概如图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值