使用hql语句怎样实现多条件查询
页面有三个条件查询文本框,分别按姓名,年龄,性别查询(条件可选填)查询出来的结果要用list保存,因为要在页面循环显示出查询结果。请问下这条hql语句该怎么写?是hql语...
页面有三个条件查询文本框,分别按姓名,年龄,性别查询(条件可选填)
查询出来的结果要用list保存,因为要在页面循环显示出查询结果。
请问下这条hql语句该怎么写?
是hql语句,不是sql语句。 展开
查询出来的结果要用list保存,因为要在页面循环显示出查询结果。
请问下这条hql语句该怎么写?
是hql语句,不是sql语句。 展开
展开全部
这里只写了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<User> findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List<User> list = (List<User>) query.list();
session.close();
return list;
}
public List<User> 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<User>好源袭 findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}
public User update(User user) {
userDao.update(user);
return user;
}
public List<User> 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<User> list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String findUserByRequest(){
List<User> 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<User> list = userServer.findUserByRequest(user);
user = list.get(0);
ServletActionContext.getRequest().setAttribute("list", list);
return "update";
}
public String updateSure() {
userServer.update(user);
return "list";
}
}
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<User> findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List<User> list = (List<User>) query.list();
session.close();
return list;
}
public List<User> 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<User>好源袭 findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}
public User update(User user) {
userDao.update(user);
return user;
}
public List<User> 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<User> list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String findUserByRequest(){
List<User> 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<User> list = userServer.findUserByRequest(user);
user = list.get(0);
ServletActionContext.getRequest().setAttribute("list", list);
return "update";
}
public String updateSure() {
userServer.update(user);
return "list";
}
}
展开全部
可以直接通过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();
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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
hql语句??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询