hibernate时间段查询

9 篇文章 0 订阅
8 篇文章 0 订阅

java

@RequestMapping(value = "/searchDate", method = RequestMethod.POST)
@ResponseBody
public List<Uploadinfo> searchDate(@RequestParam("start") String start,@RequestParam("end") String end) {

    List<Uploadinfo> itemList = itemServiceImpl.dateRange(start,end);
    return itemList;
}
//使用HQL语句查询,预设字段
public List<Uploadinfo> dateRange(String start, String end) {
    // TODO Auto-generated method stub

    Date beginDate = java.sql.Date.valueOf(start);
    Date endDate = java.sql.Date.valueOf(end);
    String hql = "from Uploadinfo info where info.dataTime <:endDate and info.dataTime >=:beginDate";  
    Query query = itemDaoImpl.getQuery(hql);
    query.setDate("beginDate",beginDate);   
    query.setDate("endDate",endDate);
    List<Uploadinfo> infolist = query.list();
    return infolist;
}

重点在hql语句对时间的查询,由于数据库定义的是date类型,则需要将字符串类型转化为date类型
:参数名根据参数名称来设置参数
?根据参数位置来设置参数

String hql = "from Uploadinfo info where info.dataTime <? and info.dataTime >=?";
Query query= session.createQuery(hql);
query.setString(0,"20160606");
query.setString(1,"20160706");
List user = query.list();

js

前台使用datarangepicker插件

function dateInit(){
    $('#dateRange').daterangepicker({
        "ranges": {
            "Today": [
                "2016-05-04T05:40:05.879Z",
                "2016-05-04T05:40:05.879Z"
            ],
            "Yesterday": [
                "2016-05-03T05:40:05.879Z",
                "2016-05-03T05:40:05.879Z"
            ],
            "Last 7 Days": [
                "2016-04-28T05:40:05.879Z",
                "2016-05-04T05:40:05.879Z"
            ],
            "Last 30 Days": [
                "2016-04-05T05:40:05.879Z",
                "2016-05-04T05:40:05.879Z"
            ],
            "This Month": [
                "2016-04-30T16:00:00.000Z",
                "2016-05-31T15:59:59.999Z"
            ],
            "Last Month": [
                "2016-03-31T16:00:00.000Z",
                "2016-04-30T15:59:59.999Z"
            ]
        },
        "locale": {
            "format": "YYYY-MM-DD",
            "separator": "   ",
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "From",
            "toLabel": "To",
            "customRangeLabel": "Custom",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "一月",
                "二月",
                "三月",
                "四月",
                "五月",
                "六月",
                "七月",
                "八月",
                "九月",
                "十月",
                "十一月",
                "十二月"
            ],
            "firstDay": 1
        },
        "alwaysShowCalendars": true,
        "startDate": "2016-05-04",
        "endDate": "2016-06-03"
    }, function(start, end, label) {
        searchDate(start.format('YYYY-MM-DD'),end.format('YYYY-MM-DD'));
    });
}

function searchDate(start,end){
    $.ajax({
        url : "rest/Uploadinfo/searchDate",
        type : "post",
        dataType : 'json',
        data:{
            "start":start,
            "end":end
        },
        success : function(data) {
            tableShow(data);
        }
    });
}

具体见[hibernate时间段查询]一文,原文不定时更新(http://chen1218chen.github.io/2016/05/04/hibernate%E6%97%B6%E9%97%B4%E6%AE%B5%E6%9F%A5%E8%AF%A2/)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值