Hibernate Criteria 笔记

MatchMode的四种匹配模式

ANYWHERE

Match the pattern anywhere in the string

匹配字符串的任意部分,转换成sql为 columnName like '%value%'

END

Match the end of the string to the pattern

匹配字符串的结束部分,转换成sql为columnName like %value

EXACT

Match the entire string to the pattern

匹配整个字符串,转换成sql为columnName like value

START

Match the start of the string to the pattern

匹配字符串的开始部分,转换成sql为columnName like value%

Restrictions 设置具体的条件

常用的静态方法
eq(String propertyName, Object value)
转换成sql为propertyName=value

in(String propertyName, Object[] values)
转换成sql为propertyName in (values[0],values[1],....,values[len-1])

isNotNull(String propertyName)
转换成sql为propertyName is not null

isNull(String propertyName)
转换成sql为propertyName is null

like(String propertyName, Object value)
转换成sql为propertyName like value

le(String propertyName, Object value)
转换成sql为propertyName <= value

lt(String propertyName, Object value)
转换成sql为propertyName < value

ge(String propertyName, Object value)
转换成sql为propertyName >= value

gt(String propertyName, Object value)
转换成sql为propertyName > value

ne(String propertyName, Object value)
转换成sql为propertyName != value

sqlRestriction(String sql,Object value,Type type)
eg:
Restrictions.sqlRestriction(“lower({alias}.name) like lower(?)”, “Fritz%”, Hibernate.STRING)
{alias}在运行的时候会被替换成表的别名,不需要手动调整

Conjunction 、Disjunction

Conjunction
将多个过滤条件以逻辑与的格式拼接在一起
Disjunction
将多个过滤条件以逻辑或的格式拼接在一起

由于ConjunctionDisjunction的构造函数是被protected和缺省值修饰的,所以无法在org.hibernate.criterion包的外部进行调用

创建Conjunction、Disjunction对象

Conjunction conjunction = Restrictions.conjunction();
Disjunction disjunction = Restrictions.disjunction();

Projections 处理结果

List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount(), "catCountByColor" )
        .add( Projections.avg("weight"), "avgWeight" )
        .add( Projections.max("weight"), "maxWeight" )
        .add( Projections.groupProperty("color"), "color" )
    )
    .addOrder( Order.desc("catCountByColor") )
    .addOrder( Order.desc("avgWeight") )
    .list();
List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount().as("catCountByColor") )
        .add( Property.forName("weight").avg().as("avgWeight") )
        .add( Property.forName("weight").max().as("maxWeight") )
        .add( Property.forName("color").group().as("color" )
    )
    .addOrder( Order.desc("catCountByColor") )
    .addOrder( Order.desc("avgWeight") )
    .list();

Criteria

创建Criteria对象

Criteria criteria = session.createCriteria()

add(Criterion criterion) 添加过滤条件
list() 得到结果
addOrder(Order order) 为结果添加排序

Order 设置排序规则
升序
public static Order asc(String propertyName)
降序
public static Order desc(String propertyName)

demo

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%") )
    .addOrder( Order.asc("name") )
    .addOrder( Order.desc("age") )
    .setMaxResults(50)
    .list();
List cats = sess.createCriteria(Cat.class)
    .add( Property.forName("name").like("F%") )
    .addOrder( Property.forName("name").asc() )
    .addOrder( Property.forName("age").desc() )
    .setMaxResults(50)
    .list();

以上两个demo是Hibernate官网提供的,同样的效果不同的写法

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值