问题具体描述:
在前台页面表单里有一类型列表框和两个日期文本框,要求:可以根据类型查询或者根据时间段查询或者同事根据类型与时间段查询。
封装的属性类:
package com.jxc.util;
public class DetailProperty {
private String typename;
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
private String beginTime;
private String endTime;
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
}
dao层
//根据DetailProperty里属性查询进货入库信息
public List<Buyinfo> getById(DetailProperty property) {
Session session = HibernateSessionFactory.getSession();
List<Buyinfo> list=null;
try {
String hql="from Buyinfo b where b.goodstype.typename like :typename and "+
"b.buydate >= :beginTime and b.buydate <= :endTime";
Query query=session.createQuery(hql);
query.setProperties(property);
list = query.list();
} catch (HibernateException e) {
HibernateSessionFactory.closeSession();
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
action层
private String tname;
private String bTime;
private String eTime;
public String getbTime() {
return bTime;
}
public String geteTime() {
return eTime;
}
public void setbTime(String bTime) {
this.bTime = bTime;
}
public void seteTime(String eTime) {
this.eTime = eTime;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
// 根据DetailProperty里属性查询进货入库信息
public String getById() {
detailProperty = new DetailProperty();
if (tname == null || tname.equals("")) {
detailProperty.setTypename("%");
} else {
detailProperty.setTypename("%" +tname+ "%");
}
if (bTime == null || bTime.equals("")) {
detailProperty.setBeginTime("%");
} else {
detailProperty.setBeginTime("%" +bTime+ "%");
}
if (eTime == null || eTime.equals("")) {
detailProperty.setEndTime("%");
} else {
detailProperty.setEndTime("%" +eTime+ "%");
}
Map session = ActionContext.getContext().getSession();
List list = null;
try {
list = this.buyinfoBiz.getById(detailProperty);
System.out.println(list.size());
} catch (Exception e) {
e.printStackTrace();
}
session.put("buyinfo", list);
return "getbyid";
}
运行时错误提示:
java.lang.String cannot be cast to java.util.Date
不知道dao层的hql语句该怎样转化,应该就是hql语句的错误!
为啥public class DetailProperty这个类的时间字段用string呢。。直接用Date不好么
然后在action里把string转成Date 存到数据库里
看异常是需要Date类型
改一下get方法
[code="java"]
public Date getBeginTime() {
try{
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginTime);
}catch(Exceptione ex){
return null;
}
}
[/code]
时间的格式自己修改调整
楼主可通过 to_date(dateStr,formart)进行转换
一个很简单的方法
将你 的日期属性的类型改成Date类就可以了