Mapper.XML基础使用及折线图语句

最近学习JAVA,但是java的lamada没有.NET那么好使(估计是我没熟练使用java原因,嘿嘿)。所以只好写原生mysql语句啦。

一、先定义好要用的方法

public interface SpotVehicleMapper extends BaseMapper<SpotVehicle> {
//这个就是待会要是用的方法,先设置好参数
    List<SpotVehicleVo> getListQuery(Integer groupId, Integer type, Integer timeType, String dataTime);
}

二、开始写Mapper.XML

<select id="getListQuery"  resultType="ym.apps.model.vo.spot.SpotVehicleVo">
        select
        <choose>
            <when test="timeType==0">
                FROM_UNIXTIME(create_time,'%Y-%m') doctime,
            </when>
            <when test="timeType==1">
                FROM_UNIXTIME(create_time,'%Y-%m-%d') doctime,
            </when>
            <when test="timeType==2">
                FROM_UNIXTIME(create_time,'%Y-%m-%d %h') doctime,
            </when>
        </choose>
        count(*) num
        from spot_vehicle a where 1=1
        <choose>
            <when test="timeType==0">
                and FROM_UNIXTIME(a.create_time,'%Y')=date_format(#{dataTime},'%Y')
            </when>
            <when test="timeType==1">
                and FROM_UNIXTIME(a.create_time,'%Y-%m')=date_format(#{dataTime},'%Y-%m')
            </when>
            <when test="timeType==2">
                and FROM_UNIXTIME(a.create_time,'%Y-%m-%d')=date_format(#{dataTime},'%Y-%m-$d')
            </when>
        </choose>
        <if test="type!=null and type!=''">
            and a.type=#{type}
        </if>
        <if test="groupId!=null and groupId!=''">
            and a.monitor_id in (select m.id from spot_monitor m where m.group_id=#{groupId})
        </if>
        group by a.monitor_id,a.type,doctime
        order by doctime desc
    </select>

看着很多其实写成sql语句很短,其中有些字段前段并不会使用,所以需要处理一下。下面是sql语句

select FROM_UNIXTIME(create_time,'%Y-%m') doctime,count(*) num from spot_vehicle a where 1=1 and FROM_UNIXTIME(a.create_time,'%Y')=date_format('日期','%Y') and a.monitor_id in (select m.id from spot_monitor m where m.group_id='主键') group by a.monitor_id,a.type,doctime order by doctime desc

好吧,还是有点长

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屑莹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值