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

展开全部

这里只写了DAO和业务62616964757a686964616fe59b9ee7ad9431333264623331逻辑组件、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";

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论

打赏作者

Gains iFortune

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值