Hibernate实现模糊查询的方法
来源:互联网 发布:java aop 日志记录 编辑:程序博客网 时间:2024/05/16 08:40
大家都知道使用本地SQL进行模糊查询用关键字 LIKE 对于在Hibernate里怎么实现呢?
一、绑定参数法:
- Session session=HibernateUtil.getSessionFactory
- ().getCurrentSession();
- session.beginTransaction();
- String strSQL="from Classes as a where a.classno like :name";
- Query query = session.createQuery(strSQL);
- query.setString("name", "%"+OId+"%");
- List result=query.list();
Session session=HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction();String strSQL="from Classes as a where a.classno like :name";Query query = session.createQuery(strSQL); query.setString("name", "%"+OId+"%"); List result=query.list();
二、在面向对象的查询语言 HQL 中
- Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- List result=session.createQuery("from Classesas 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 Classesas a where a.classno
- like :name").setParameter("pid",OId).list();
Session session=HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();List result=session.createQuery("from Classes as a where a.classno like :name").setParameter("pid",OId).list();
Hibernate模糊查询参数化的问题
from Project o where 1=1 and o.isDeleted=? and o.prjName like ?; query.setString(i, "%"+实际查询条件+"%"); 注意参数中第一个百分号的左边与第二个百分号的右边都没有单引号,这和平时写SQL语句是不同的,要特别关注一下。另外还应该了解一下俩点的区别
HQL:from Project o where 1=1 and PRJ_NAME like '%strCond%';//这里PRJ_NAME 应该是数据库表中的实际字段名
HQL:from Project o where 1=1 and o.PRJ_NAME like '%strCond%';//这里PRJ_NAME 应该是实体类的属性名
HQL:from Project o where 1=1 and PRJ_NAME like '?';//这里的?不视为占位参数HQL:from Project o where 1=1 and PRJ_NAME like ?;//设置参数值时会自动在参数值两边加上单引号。
- Hibernate实现模糊查询的方法
- Hibernate实现模糊查询的方法
- Hibernate实现模糊查询的方法
- 实现模糊查询的方法
- Hibernate中实现模糊查询
- PHP模糊查询的实现方法
- mybatis 模糊查询的实现方法
- Hibernate分页的实现(支持模糊查询)
- hibernate实现多条件组合的模糊查询
- 关于hibernate的模糊查询.
- hibernate的模糊查询和sql查询
- DetachedCriteria离线查询离线查询---实现模糊查询 Hibernate
- js 实现对JSON数组模糊查询的方法
- JPA模糊查询的方法
- Mybatis的模糊查询方法
- mybatis 模糊查询的方法
- Oracle模糊查询的实现
- jQuery实现的模糊查询
- inkscape 详解
- FreeMarker 对null值的处理技巧
- this和getApplicationContext区别
- 高级程序员装逼指南
- 将smartform转换成pdf并上传
- Hibernate实现模糊查询的方法
- C++ 对象的内存布局(下)【转】
- C++编程思想学习——模版介绍
- 生成自己的 tclsh
- Gallery和GridView浅析
- 在SQL Server Oracle MySQL当数据库中查出某值为NULL怎么办?
- C#中参数传递
- Theme Optimization Quick Reference
- ESFramework 开发手册(01) -- 发送和处理信息