小记:
-
Stringhql="fromWifiTTerminalwhere1=1";
-
hql=hql+"andpwslike'%'"+pws1+
-
"'%'anduseStatuslike'%'"+useStatus1+
-
"'%'andnumlike'%'"+num1+
-
"'%'";
别这么干, 楼上的那种需要先过滤非法字符 像 '' 单引号,最好别这么干
至于楼主,你可以换种写法
hql=hql+" and pws like ? and useStatus like ? and num like ?";
给参数赋值的时候加上两边的%
paramValues[0]="%"+pws1+"%";
paramValues[1]="%"+useStatus1+"%";
paramValues[2]="%"+num1+"%";
anyasir(高级程序员)
2010-12-30
/**
* HQL查询的一个例子
*/
public static void hql()
{
Session s = null;
try
{
s = HibernateUtil.getSeesion();
//final String hql = "from User as u where u.name=?";
final String hql = "from User as u where u.name=:name";
final Query query = s.createQuery(hql);
//query.setString(0, "北京市"); //从0开始
query.setString("name", "北京市");
final List<User> list = query.list();
for (final User u : list)
{
System.out.println(u.getName());
}
}
finally
{
if (s != null)
{
s.close();
}
}
System.out.println("HQL完成");
}
HibernateUtil工具类
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 这是一个工具类, 快速取得session
*
*/
public class HibernateUtil
{
static SessionFactory sessionFactory = null;
static
{
final Configuration cfg = new Configuration();
cfg.configure(); //路径可以改变
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
/**
* 取得session
*
* @return session
*/
public static Session getSeesion()
{
return sessionFactory.openSession();
}
} /**
* HQL 分页查询
*/
public static void page()
{
Session s = null;
try
{
s = HibernateUtil.getSeesion();
final String hql = " from User "; //User是类名
final Query q = s.createQuery(hql);
q.setFirstResult(0); //从第0条开始
q.setMaxResults(10); //取出10条
final List list = q.list();
for (final User u : list)
{
System.out.println(u.getId() + " " + u.getName());
}
}
finally
{
s.clear();
}
}
模糊查询
public List<User> getUsers(String id){
List list=new ArrayList<User>();
String hql="from User as user where user.id like :id"; //参数名称查询
factory=DBHelper.getSessionFactory();
Session session=factory.openSession();
Transaction transaction=session.beginTransaction();
Query query=session.createQuery(hql);
query.setString("id", "%"+id+"%");
list=query.list();
transaction.commit();
session.close();
return list;
} //可以拼字符串
List students = session.createQuery("select s.id, s.name from Student s where s.name like '%1%'").list();
//Query query = session.createQuery("select s.id, s.name from Student s where s.name like ?");
//query.setParameter(0, "%1%");
//List students = query.list();
//可以使用?方式传递参数
//参数的索引从0开始
//传递的参数值,不用单引号引起来
//注意方法链编程
List students = session.createQuery("select s.id, s.name from Student s where s.name like ?")
.setParameter(0, "%1%")
.list();
//使用 :参数名称 的方式传递参数值
List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname")
.setParameter("myname", "%1%")
.list();
//使用 :参数名称 的方式传递参数值
List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname and s.id=:myid")
.setParameter("myname", "%1%")
.setParameter("myid", 12)
.list();
//支持in,需要使用setParameterList进行参数传递
List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
.setParameterList("myids", new Object[]{1, 2, 3, 4, 5})
.list();
//查询2008年2月创建的学生
List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=?")
.setParameter(0, "2008-02")
.list();
//查询2008-01-10到2008-02-15创建的学生
List students = session.createQuery("select s.id, s.name from Student s where s.createTime between ? and ?")
.setParameter(0, sdf.parse("2008-01-10 00:00:00"))
.setParameter(1, sdf.parse("2008-02-15 23:59:59"))
.list();
分享到:
相关推荐
Hibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询....
Hibernate hql查询语法总结 Hibernate hql查询语法总结
Hibernate中的关联查询实际上生成的是数据库表连接查询的SQL语句
hql模糊查询hql模糊查询hql模糊查询hql模糊查询
hibernate查询之HQLhibernate查询之HQLhibernate查询之HQL
hibernate hql各类查询范例
Hibernate HQL查询语句总结.docx 网络下载的
Hibernate HQL 深度历险 深入讲解Hibernate HQL 查询
Hibernate HQL基础练习小列子+数据库
hibernate HQL 查询教程 管理 Session ThreadLocal 接口的使用
Hibernate HQL命名参数和Query接口的分页 Hibernate HQL命名参数和Query接口的分页
hibernate持久化,hql查询 关联映射
Hibernate HQL.txt Hibernate HQL.txt Hibernate HQL.txt
对hibernate的hql进行了详尽的讲解
总结的hql的查询,感觉很实用 已经写成.java格式,直接复制到项目中就能看了
Hibernate 配备了一种非常强大的查询语言,这种语言看上去很像 SQL。但是不要被语法结构上的相似所迷惑,HQL 是非常有意识的被设计为完全面向对象的查询,它可以理解如继承、多态和关联之类的概念。
Hibernate查询语言HQL.PPT
Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码 Hibernate(HQL、QBC查询)源码
hibernate hql 语法学习的文档
Hibernate HQL 查询 Query 资料 3个word总结的资料。