publicListgetTime(DeviceHistorybeTime,DeviceHistoryendTime)throwsException{Listl=newArrayList();Sessions=this.getSession();Queryq=s.createQuery("fromDeviceHistorydwhered....
public List getTime(DeviceHistory beTime,DeviceHistory endTime)throws Exception {
List l=new ArrayList();
Session s = this.getSession();
Query q=s.createQuery("from DeviceHistory d where d.DId=:dd and d.deviceTime between :aa and :bb");
q.setDate("aa", beTime.getDeviceTime());
q.setDate("bb", endTime.getDeviceTime());
l=q.list();
s.close();
return l;
}
public static void main(String[] args) throws Exception{
List l= new ArrayList();
DeviceHistoryDao dgd=new DeviceHistoryDao();
ToChange tc=new ToChange();
DeviceHistory dg=new DeviceHistory();
dg.setDId(1);
// dg.setId(6);
dg.setDeviceTime(tc.charToDate("2008-10-06 13:00"));
DeviceHistory dg1=new DeviceHistory();
dg1.setDId(1);
// dg1.setId(8);
dg1.setDeviceTime(tc.charToDate("2008-10-06 16:00"));
l=dgd.getTime(dg,dg1);
System.out.println("list=="+l.size());
}
public Date charToDate(String s){
DateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm");
Date dd=new Date(0);
try {
if(s!=null&&s.trim().length()>0){
dd = (Date) df.parse(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return dd;
}
public class DeviceHistory {
// Fields
private Integer id;
private Integer DId;
private String lng;
private String lat;
private Float speed;
private String info;
private Date deviceTime;
}
真的很不好意思!昨天弄了半天没弄好!再加上晚上已经很晚了哈!所以急了一些!这里向大家道歉!还有你的方法我试了不行!真的很不好意思!
我用这个差寻double范围都好着呢!就唯独date类型查询不出来,得到的list为0;
我这里将我操作的方法,测试的方法,日期类型的转换方法,传递豆里的属性都贴出来了,希望大家能帮我下哈!
我也查了很多的资料可每一个能通过测试取得数据的。
beTime.getDeviceTime()获取的是java.util.Date;类型,在public List getTime(DeviceHistory beTime,DeviceHistory endTime)方法里我少了 q.setInteger("dd", beTime.getDId());这里是我粘贴的时候没有粘上去。对不起哈!如果需要我可以把我的数据也贴上来!
我用的是hibernate没有用其他的框架!还有to_Date我试了在mysql里不行,直接报错的
展开