Hibernate 之 Criteria

 

package cn.sasa.test;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import antlr.collections.List;
import cn.sasa.domain.account;
import cn.sasa.utils.HibernateUtils;

public class TestCriteria {
    @org.junit.Test
    public void test() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction tran = session.beginTransaction();
        
        Criteria criteria = session.createCriteria(account.class);
//        var aclist = criteria.list();
//        System.out.println(aclist);
        
        criteria.add(Restrictions.eq("id", 1));
        //cri.add(Restrictions.or(Restrictions.eq("id", 2)));
        var obj = criteria.list();
        System.out.println(obj);
        
        //查询记录数
        criteria.setProjection(Projections.rowCount());
        var rowCount = criteria.uniqueResult();
        System.out.println(rowCount);
        
        //分页
        Criteria criteria1 = session.createCriteria(account.class);
        criteria1.setFirstResult(2);
        criteria1.setMaxResults(3);
        var list1 = criteria1.list();
        System.out.println(list1);
        
        tran.commit(); 
        session.close();
    }
    
    
}

 

posted @ 2019-10-11 16:59  SasaL  Views(136)  Comments(0Edit  收藏  举报