hibernate动态多条件带参数查询 criteria

hanmeixiaoxue 2012-09-23 04:14:15
我有一个多条件查询,而且没有条件查询都动态的传参,于是想到hibernate的criteria,我项目是SSH 的。我遇到的问题是: 1:在用between查询的时候,明明包括在内的产品查询不出来。
2:ge 这个本意是:">=",可以却把所有小于指定值 的产品查询出来了。
3:查询字段都是varchar型,数据库是mysql5.1,hibernate3.3,spring2.5;
4:我主要查询了价格字段,该字段也是varchar类型的,
5:我感觉这个诡异的是,查询结果严重不准,本在between里的产品,只能显示部分。我程序里都用trim()消除了空间,给我的感觉倒是,hibernate的criteria在读取数据库时候,好像自己添加了空格。varchar是变成的char型,在说hibernate底层好像一般用的jdbc,其他如:hql查询却没有问题啊
请高手指定!!!
下面我提出关键代码。
1:前台多条件查询,就如“中关村”电脑网站上的那些随机条件选择递交一样。
2:action,接受数据,调用数据层。代码如下:

public String findProductByFiveIterm() {
String result = "success";
try {
HttpServletRequest request = ServletActionContext.getRequest();
ApplicationContext appContext = GetContext.getContext();
ProductService productService = (ProductService) appContext
.getBean("productService");
CountyService countyService = (CountyService) appContext
.getBean("countyService");
// 这里只针对County对象下的查询
String address = request.getParameter("address");
County county = null;
if (address != null && !"".equals(address)
&& !"null".equals(address)) {
int countyId = Integer.parseInt(address);
county = countyService.getCounty(countyId);
}
String isEnable = request.getParameter("isEnable");
boolean bool = true;
if (isEnable != null && !"".equals(isEnable)) {
bool = Boolean.parseBoolean(isEnable);
}
String price = request.getParameter("price");
String price_f = null;
String price_s = null;
//分离出价格区间
if (price != null && !"".equals(price) && !"null".equals(price)) {
String[] price_a = price.split("-");
price_f = price_a[0];
price_s = price_a[1];
}
String house = request.getParameter("house");
String acreage = request.getParameter("acreage");
String acreage_f = null;
String acreage_s = null;
if (acreage != null && !"".equals(acreage)
&& !"null".equals(acreage)) {
String[] acreage_a = acreage.split("-");
acreage_f = acreage_a[0];
acreage_s = acreage_a[1];
}
//产品分类
String tag = request.getParameter("tag");
int first = 0;
int length = 30;
if (first > 0) {
first = (first - 1) * length;
}
if (this.list != null) {
this.list.clear();
}
list = productService.findByFiveIterm(first, length, bool, county,
price_f, price_s, acreage_f, acreage_s, house, tag);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}


3:action需要调用的数据层(对应的接口就不写了)
public List<Product> findByFiveIterm(int first, int length,
boolean isenable, County county, String price_f, String price_s,
String acreage_f, String acreage_s, String house, String tag) {
List<Product> list = null;
Session session = getHibernateTemplate().getSessionFactory()
.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria crit = session.createCriteria(Product.class);
if (county != null && !"null".equals(county)) {
crit.add(Restrictions.eq("county", county));
}
if (house != null && !"".equals(house) && !"null".equals(house)) {
crit.add(Restrictions.eq("house", house));
}
if (tag != null && !"".equals(tag) && !"null".equals(tag)) {
crit.add(Restrictions.eq("tag", tag));
}
if (price_f != null && price_s != null) {
if ("1000".equals(price_f) && "2000".equals(price_s)) {
crit.add(Restrictions.ge("price", new String("1000")));
} else {
crit.add(Restrictions.between("price", price_f, price_s));
}
}
if (acreage_f != null && acreage_s != null) {
if ("800".equals(acreage_f) && "1000".equals(acreage_s)) {
crit.add(Restrictions.ge("acreage", "800"));
} else {
crit.add(Restrictions.between("acreage", acreage_f, acreage_s));
}
}
crit.add(Restrictions.eq("isenable", isenable));
crit.setFirstResult(first);
crit.setMaxResults(length);
list = crit.list();
tx.commit();
return list;
}

以上 就是主要代码,每个if语句就是前台的一个条件,如果为空,就不添加该查询条件
...全文
140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧