hql基本查询语句

package com.etp.bbs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.etp.bbs.pojo.User;

public class UserDao_H
{

public List<User> getAllUser_01()
{
List<User> userList = null;
String hql = "from User";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
userList = query.list();
}
catch (Exception e)
{
// TODO: handle exception
}
return userList;
}

public List<User> getAllUserByPage(int pageNum)
{
int start = (pageNum - 1) * 2;
List<User> userList = null;
String hql = "from User";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(2);
userList = query.list();
}
catch (Exception e)
{
e.printStackTrace();
}
return userList;
}

public List getAllUser_02()
{
List userList = null;
String hql = "select u.userName,u.regTime from User u";
Session session = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
userList = query.list();
}
catch (Exception e)
{
// TODO: handle exception
}
return userList;
}

public User getUserById_01(int id)
{
User user = null;
String hql = "from User uwhere u.userId = ?";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setInteger(0, id);
user = (User) query.list().get(0);
}
catch (Exception e)
{
// TODO: handle exception
}
return user;
}

public User getUserById_02(int id)
{
User user = null;
String hql = "from User uwhere u.userId = ?";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setInteger(0, id);
user = (User) query.uniqueResult();
}
catch (Exception e)
{

}
return user;
}

public User getUserById_03(int id)
{
User user = null;
String hql = "from User uwhere u.userId = :id";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setInteger("id", id);
user = (User) query.uniqueResult();
}
catch (Exception e)
{
}
return user;
}

// 模糊查询
public List<User> getUserByUserName(String userName)
{
List<User> userList = null;
String hql = "from User uwhere u.uName like :userName";
Session session = null;
Transaction tran = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setString("userName", "%" + userName + "%");
userList = query.list();
}
catch (Exception e)
{

}
return userList;
}

// 取出字段
public List<String> getAllUserName()
{
List<String> nameList = null;
String hql = "select u.userName from User u";
Session session = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
nameList = query.list();
}
catch (Exception e)
{
// TODO: handle exception
}
return nameList;
}

// 计数
public long getUserCount()
{
long result = -1;
String hql = "select count(*) from User";
Session session = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
result = (Long) query.uniqueResult();
}
catch (Exception e)
{

}
return result;
}

// 关联
public User getUserTopicByUserId(int userId)
{
User user = null;
String hql = "select u from User u join fetch u.topics where u.userId = ?";
Session session = null;
try
{
session = HibernateUtil.getSession();
Query query = session.createQuery(hql);
query.setInteger(0, userId);
user = (User) query.uniqueResult();
}
catch (Exception e)
{

}
return user;
}
}

你可能感兴趣的:(HQL)