`
lgzbj2006
  • 浏览: 28000 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Restrictions.sqlRestriction

阅读更多
了hibernate的查询,其中学到构建Criteria查询,在Criteria查询中 
使用sqlRestriction()方法来提供SQL语法作限定查询,如:
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.sqlRestriction(“{类名}.name LIKE(?)", "cater%", Hibernate.STRING));
List users = criteria.list();
其中Criteria对象由session创建,在Criteria对象中添加Restrictions.sqlRestriction来作为限定条件,在Restrictions.sqlRestriction()中有三个参数,第一个是String类型,是SQL语句的条件部分,第二个参数是参数值,第三个参数是类型.第二个和第三个参数都没什么,关键是第一个参数,"{类名}.name LIKE(?)" {}中的类名是表示对应PO类的别名.在上面写的语句中对应应该为User类的别名.
自己这么理解后,先是将其换为{User}.name...,报错,再加上包名还是报错,看了尹老拷的PPT,根据下面举的例子,应该是{alias}.name...,运行正常,在该成其他的,报错,看来那里还必须写成{alias},那alias究竟是什么意思呢?我英语不好,上网一搜:
      alias,别名,指能够代替表示某人或某物的称呼。比如,文学作品中的“笔名”就是作者真实姓名的一个alias。alias一词源于拉丁语alias,意思是“其他的某个东西”。在IT中,alias至少有下面两种意思:  1)在某些计算机操作系统以及程序语言中,用别名表示某个数据对象可以使含义清晰,具有可读性。可以仅定义一次数据对象,之后程序员可以用多个别名来引用数据对象。  2)苹果机的麦金托什操作系统中,aliaa是一个特定程序的桌面图标,也可以表示某个数据对象。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.happy.dao.BaseHibernateDAO;
import com.happy.entity.AwardNums;
import com.happy.test.util.HandleList;
/**
* @author happy
*
*/
public class Restriction extends BaseHibernateDAO {
public static void main(String[] args) {
Restriction query = new Restriction();
query.query();
}
/**
* 获取查询别名
*/
@SuppressWarnings("unchecked")
public void queryAlias() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 获取别名:eg:this
System.out.println(criteria.getAlias());
HandleList.print(criteria.list());
}
/**
* 对查询排序
*/
@SuppressWarnings("unchecked")
public void queryOrder() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
criteria.addOrder(Order.desc("id"));
HandleList.print(criteria.list());
}
/**
* query by Between
*/
@SuppressWarnings("unchecked")
public void queryBetween() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 第一个参数是属性,第一个是小的数值,第三个是大的数值
criteria.add(Restrictions.between("id", 17720820, 17720830));
HandleList.print(criteria.list());
}
/**
* query by idEq
*/
@SuppressWarnings("unchecked")
public void queryIdEq() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// id=17720820
criteria.add(Restrictions.idEq(17720820));
HandleList.print(criteria.list());
}
/**
* 忽略大小写查询query by ilike
*/
@SuppressWarnings("unchecked")
public void queryIlike() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 忽略大小写,05结束的
criteria.add(Restrictions.ilike("numbers", "05", MatchMode.END));
HandleList.print(criteria.list());
}
/**
* 正常like查询 query by like sql:this_.numbers like ?
*/
@SuppressWarnings("unchecked")
public void queryLike() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 05介绍的
criteria.add(Restrictions.like("numbers", "05", MatchMode.END));
HandleList.print(criteria.list());
}
/**
* 正常like查询 query by like sql:this_.numbers like ?
*/
@SuppressWarnings("unchecked")
public void queryLike2() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 05开始的
criteria.add(Restrictions.like("numbers", "05%"));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryInArray() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// new int[]错误的
Integer[] arrInt = new Integer[] { 17720808, 17720809, 17720810 };
criteria.add(Restrictions.in("id", arrInt));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryInList() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// new int[]错误的
List<Integer> list = new ArrayList<Integer>();
list.add(17720808);
list.add(17720809);
list.add(17720810);
criteria.add(Restrictions.in("id", list));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryInSet() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// new int[]错误的
Set<Integer> set = new TreeSet<Integer>();
set.add(17720808);
set.add(17720809);
set.add(17720810);
criteria.add(Restrictions.in("id", set));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryInMap() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
Map<String, Integer> map = new HashMap<String, Integer>();
// String不能命名重复,否者会过滤重复的
map.put("1", 17720808);
map.put("2", 17720809);
map.put("3", 17720810);
criteria.add(Restrictions.in("id", map.values()));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryIsEmpty() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// Property path [com.happy.entity.AwardNums.issue] does not reference a
// collection
criteria.add(Restrictions.isEmpty("issue"));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryIsNotEmpty() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// Property path [com.happy.entity.AwardNums.issue] does not reference a
// collection
criteria.add(Restrictions.isNotEmpty("issue"));
HandleList.print(criteria.list());
}
/**
* this_.id is null
*/
@SuppressWarnings("unchecked")
public void queryIsNull() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// this_.id is null
criteria.add(Restrictions.isNull("id"));
HandleList.print(criteria.list());
}
/**
* this_.id is not null
*/
@SuppressWarnings("unchecked")
public void queryIsNotNull() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// this_.id is not null
criteria.add(Restrictions.isNotNull("id"));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void querySizeEq() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// Unknown collection role: com.happy.entity.AwardNums.id
criteria.add(Restrictions.sizeEq("id", 15));
HandleList.print(criteria.list());
}
/**
* this_.id < 17720812
*/
@SuppressWarnings("unchecked")
public void querySqlRestriction() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
String alias = criteria.getAlias();
// _很重要的
criteria.add(Restrictions.sqlRestriction(alias + "_.id < 17720812"));
HandleList.print(criteria.list());
}
/**
* 仅需要:{alias}.id即可
*/
@SuppressWarnings("unchecked")
public void querySqlRestrictionEasy() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// {alias}.id
criteria.add(Restrictions.sqlRestriction("{alias}.id < 17720812"));
HandleList.print(criteria.list());
}
/**
* lower(this_.numbers) like lower(?)
*/
@SuppressWarnings("unchecked")
public void querySqlRestrictionType() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// lower(this_.numbers) like lower(?)
criteria.add(Restrictions
.sqlRestriction("lower({alias}.numbers) like lower(?)", "%09",
Hibernate.STRING));
HandleList.print(criteria.list());
}
/**
* where 1=1(可组合使用)
*/
@SuppressWarnings("unchecked")
public void queryConjunction() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 永真情况下查询
criteria.add(Restrictions.conjunction());
HandleList.print(criteria.list());
}
/**
* where 1=1(和conjunction一样还不知区别)
*/
@SuppressWarnings("unchecked")
public void queryDisjunction() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 永真情况下查询
criteria.add(Restrictions.disjunction());
HandleList.print(criteria.list());
}
/**
* where 1=1(和conjunction一样还不知区别)
*/
@SuppressWarnings("unchecked")
public void queryNaturalId() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 永真情况下查询
criteria.add(Restrictions.naturalId());
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryCompare() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// this_.id=? eq&&ne this_.id<>?
criteria.add(Restrictions.eq("id", 17719845));
// this_.id>=? ge&&le this_.id<=?
criteria.add(Restrictions.ge("id", 17719845));
// this_.id>? gt&&lt this_.id<?
criteria.add(Restrictions.gt("id", 17719845));
// this_.id<=?
criteria.add(Restrictions.le("id", 17719845));
// this_.id<?
criteria.add(Restrictions.lt("id", 17719845));
// this_.id<>?
criteria.add(Restrictions.ne("id", 17719845));
HandleList.print(criteria.list());
}
@SuppressWarnings("unchecked")
public void queryCompareProperty() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// this_.issue=this_.numbers eq&&ne this_.id<>?
criteria.add(Restrictions.eqProperty("id", "issue"));
// this_.id>=this_.issue ge&&le this_.id<=?
criteria.add(Restrictions.geProperty("id", "issue"));
// this_.id>this_.issue gt&&lt this_.id<?
criteria.add(Restrictions.gtProperty("id", "issue"));
// this_.id<=this_.issue
criteria.add(Restrictions.leProperty("id", "issue"));
// this_.id<this_.issue
criteria.add(Restrictions.ltProperty("id", "issue"));
// this_.id<>this_.issue
criteria.add(Restrictions.neProperty("id", "issue"));
HandleList.print(criteria.list());
}
/**
* this_.id=? or this_.id=?
*/
@SuppressWarnings("unchecked")
public void queryOr() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 或者查询
criteria.add(Restrictions.or(Restrictions.eq("id", 17719845),
Restrictions.eq("id", 17719846)));
HandleList.print(criteria.list());
}
/**
* this_.id=? and this_.issue=?
*/
@SuppressWarnings("unchecked")
public void queryAnd() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 并且查询
criteria.add(Restrictions.and(Restrictions.eq("id", 17719845),
Restrictions.eq("issue", "2011100")));
HandleList.print(criteria.list());
}
/**
* not this_.id=?
*/
@SuppressWarnings("unchecked")
public void query() {
Criteria criteria = getSession().createCriteria(AwardNums.class);
// 排除查询
criteria.add(Restrictions.not(Restrictions.eq("id", 17719841)));
HandleList.print(criteria.list());
}

分享到:
评论

相关推荐

    Restrictions用法.docx

    此外,`Restrictions.sqlRestriction`允许我们直接使用SQL片段作为条件,但这种方式降低了代码的可读性,通常只在特殊情况下使用。 综上所述,`Restrictions`类提供了丰富的条件构造方法,使得在Hibernate中构建...

    Restrictions用法.pdf

    10. `Restrictions.sqlRestriction`: 允许直接使用SQL片段进行限定,更灵活但也可能引入SQL注入风险。 11. `Order.asc`和`Order.desc`: 用于设置结果集的排序,`asc("age")`按年龄升序排序,`desc("age")`则是降序...

    Hibernate学习笔记

    在某些情况下,可能需要直接使用SQL语句进行查询,`Restrictions.sqlRestriction()`提供了这样的能力,如`Restrictions.sqlRestriction("lower({alias}.name) like lower(?)", "Erica%", Hibernate.STRING)`,其中`{...

    DetachedCriteria查询

    `criteria.add(Restrictions.sqlRestriction(" project_Id="+projectId+" connect by prior id= prev_ver ")).addxxx();` DetachedCriteria 查询提供了非常灵活的查询方式,可以满足各种复杂的查询需求。

    Oracle_start_with_connect_by_prior_用法[文].pdf

    Oracle 连接查询的使用方法 Oracle 连接查询是指使用 START WITH 和 CONNECT BY 语句来实现递归查询的方法,这种方法可以生成树形结构的数据。在 Oracle 中,START WITH 语句用于指定递归查询的开始记录,而 ...

    Hibernate-Criteria 模糊查询

    criteria.add(Restrictions.sqlRestriction("name LIKE '%Erica%'")); List&lt;User&gt; users = criteria.list(); ``` #### 五、进阶应用 ##### 1. 多表关联模糊查询 在实际应用中,往往需要跨表进行模糊查询,例如在...

    Hibernate criteria基本操作

    Criterion crit1 = Restrictions.sqlRestriction("{alias}.name=?", "刘伟", Hibernate.STRING); ``` 8. 统计函数查询: 可以设置投影(Projection)为统计函数,如 `max`、`min`、`rowCount`: ```java ...

    (3.0版本)自己写的struts2+hibernate+spring实例

    criteria.add(Restrictions.sqlRestriction("MENUITEM_ID in(select a.MENUITEM_ID from Wuxin_MENUITEM a connect by prior a.MENUITEM_ID = a.PARENT_ID"+ " start with a.MENUITEM_ID = '"+parentId+"')" )); ...

    hibernateSearch 搜索 索引列子源代码

    .add(Restrictions.sqlRestriction("{alias}.id IN (SELECT id FROM lucene_document WHERE " + luceneQuery.toString() + ")")) .list(); ``` ### 6. 进阶功能 - 使用 `@AnalyzerDef` 注解自定义分析器,对文本...

    Criteria用法

    - `sqlRestriction()`方法允许直接插入SQL片段,但请注意,这将使查询依赖于数据库的SQL语法,例如:`Restrictions.sqlRestrictionSql 限定查询,qbc 添加 sql` 7. **QBE(Query By Example)**: - 基于实例对象...

Global site tag (gtag.js) - Google Analytics