使用hql语句怎样实现多条件查询

2024-11-05 03:31:56
推荐回答(4个)
回答1:

这里只写了DAO和业务逻辑组件、ACTION的具体实现类,PO和和接口自己应该会写吧,HQL采用的是结合SQL的那种写法,增删改查全在里面了,修改下马上就能跑了,不清楚再问,我裂链详细解答,因为这个是我第一给回答问题,分给我吧。呵呵

package dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pojo.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
//根据标识属性加载User实例
public User get(String sysid)
{
return (User)getHibernateTemplate().get(User.class, sysid);
}
//持久化友兄指定的User实例
public String save(User user)
{
return (String)getHibernateTemplate().save(user);
}
public void update(User user)
{
getHibernateTemplate().update(user);
}
public void delete(User user)
{
getHibernateTemplate().delete(user);
}
public void delete (String sysid)
{
getHibernateTemplate().delete(get(sysid));
}

public List findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List list = (List) query.list();
session.close();
return list;

}
public List findUserByRequest(User user) {
// TODO Auto-generated method stub
return null;
}

}

package service.impl;
import java.util.List;

import pojo.User;
import dao.impl.UserDao;
public class UserServiceImpl implements UserService
{

private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public User save(User user) {
userDao.save(user);
return user;
}

public User delete(String sysid) {
userDao.delete(sysid);
return userDao.get(sysid);
}

public List好源袭 findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}

public User update(User user) {
userDao.update(user);
return user;
}

public List findUserByRequest(User user) {
String hsql="from User ";
String where = " where 1=1";
if (user.getSysid()!=null && !"".equals(user.getSysid())) {
where+=" and sysid='"+user.getSysid()+"'";
}
if(null!=user.getName() && !"".equals(user.getName())){
where+=" and name='"+user.getName()+"'";
}
hsql+=where;
return userDao.findAll(hsql);
}

public User delete(User user) {
userDao.delete(user);
return user;
}

}

//ACTION
package action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import pojo.User;
import service.impl.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

private User user;
private UserService userServer;

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserServer() {
return userServer;
}
public void setUserServer(UserService userServer) {
this.userServer = userServer;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return findAll();
}

// public String list(){
public String findAll(){
List list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String findUserByRequest(){
List list = userServer.findUserByRequest(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String add()
{
return "add";
}
public String addSure()
{
userServer.save(user);
return "list";
}
public String deleteBySysid()
{
userServer.delete(user);
return "list";
}

public String showUpdate() {

List list = userServer.findUserByRequest(user);
user = list.get(0);
ServletActionContext.getRequest().setAttribute("list", list);
return "update";
}

public String updateSure() {
userServer.update(user);
return "list";
}
}

回答2:

可以直接通过HQL语言闭弯,通过弊态袭java对象的形式来实现数据库多条件租兄查询。
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno
like " '%"+OId+"%'").list();
[sql] view plaincopyprint?
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like " '%"+OId+"%'").list();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like " '%"+OId+"%'").list();

回答3:

username=Ext.getCmp('链烂皮碧nameid').getValue();
age=Ext.getCmp('ageid').getValue();
query="from 表名 where 1=1";
if(name!=''&&name!=null){
query=query+'and name='+username;
}
if(age!=''&&age!=null){
query=query+'棚握漏and age='+age;
}

回答4:

hql语句??