MyBatis 接收数据库中没有的字段(记一次工作总结)

来源:互联网 发布:ubuntu wifi 编辑:程序博客网 时间:2024/06/07 09:57

问题描述:浏览器页面想要通过E-hcart表格,展示一些数据。这些数据需要从数据库中计算而来。但是在数据库中没有的字段。比如说要查询某些记录。如车辆的在线数量,离线数量和无数据的数量。这些数据是通过MySql的聚合函数得到的。
sql语句如下:

SELECT    sum(        updatetime > date_sub(now(), INTERVAL 0.5 DAY)    ) AS online,    sum(updatetime IS NULL) nodata,    count(1) - sum(        updatetime > date_sub(now(), INTERVAL 0.5 DAY)    ) - sum(updatetime IS NULL) AS offline,COUNT(1)FROM    cx_onecdataWHERE    kdgs = '顺丰'AND cartype LIKE '%gps%'

这里写图片描述

查询的结果如上图。
注意,这些统计的字段都是数据库中没有的。
那么问题来了,该如何获取这些数据呢?
问题的解决方法:在MyBaits中生成相对应的字段。比如,我的实体类中是这样的(也就是DO):

public class Sfcar {    private Integer online;    private Integer nodata;    private Integer offline;    public Integer getOnline() {        return online;    }    public void setOnline(Integer online) {        this.online = online;    }    public Integer getNodata() {        return nodata;    }    public void setNodata(Integer nodata) {        this.nodata = nodata;    }    public Integer getOffline() {        return offline;    }    public void setOffline(Integer offline) {        this.offline = offline;    }

MyBatis中对应的映射文件是这样配置的:

    <!--  GPS 车辆统计  -->  <select id="findbySFCarGPS" resultMap="BaseResultMap">    SELECT        sum(            updatetime &gt; date_sub(now(), INTERVAL 0.5 DAY)        ) AS online,            sum(updatetime IS NULL) nodata,            count(1) - sum(                updatetime &gt; date_sub(now(), INTERVAL 0.5 DAY)            ) - sum(updatetime IS NULL) AS offline        FROM            cx_onecdata        WHERE            kdgs = '顺丰'        AND cartype LIKE '%gps%'  </select>

这里有两个注意的点。一是:返回结果类型是resultMap=”BaseResultMap”。二,MyBatis是跟实体类映射,而不是跟数据库对应的表映射,所以可以直接写想要的Sql。最好用这个Do 来接收想要的数据。
至此,可以完美解决字段不对应的问题。
最后页面显示的效果:
这里写图片描述
数据通过渲染得到想要的效果。

说明一下:以前数据中获取不对应的字段时,我是用List集合里面放Map集合,获取所要的数据。

这里写图片描述
,用来解码的代码如:

        List<String> legend = new ArrayList<String>();        List<Map> serisData=new ArrayList<Map>();        List<TreeMap<String,Integer>> data = car_Service.selectCarTypeCount();        int count = 0;        List<String> car_typename= new ArrayList<String>();        List<String> car_typevalue = new ArrayList<String>();        //      Map<String,String> mapdata= new HashMap<>();        //  遍历以Key - Value 形式遍历        for (Map<String, Integer> map : data){             Map tarMap =new HashMap();            for (Map.Entry<String, Integer> k : map.entrySet()){                 count++;                if (count % 2 == 0) {                    car_typename.add(k.getValue()+"");                }else{                    car_typevalue.add(k.getValue()+"");                }            }             for (int i = 0; i < car_typename.size(); i++) {                // 获取 List 中的 String 数组元素。                String cartype_name = car_typename.get(i);                String cartype_value = car_typevalue.get(i);                //              mapdata.put(cartype_name, cartype_value);                if (i== car_typename.size()-1) {                    legend.add(cartype_name);                    tarMap.put("value", cartype_value);                    tarMap.put("name", cartype_name);                    serisData.add(tarMap);                }            }        }        // 纵坐标        List<SeriesVO> series = new ArrayList<>();        series.add(new SeriesVO("总数比较", "pie",serisData));        EchartDataVO targetdata = new EchartDataVO(legend,null, series);        // 获取 目标数据 JSON 格式        return targetdata;

至此问题解决!
《end》

阅读全文
0 0
原创粉丝点击