• hibernate Criteria查询


    Criteria对象提供了一种面向对象的方式查询数据库。
    Criteria对象需要使用Session对象来获得一个Criteria对象表示对一个
    持久化类的查询

    查询所有

        Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
            session.getTransaction().commit();
            

    where

    //1 
            Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            
            //条件 
            cri.add(Restrictions.eq("username", "u1"));
            
            
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
                      session.getTransaction().commit();
    //2 
            
            Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            
            //条件 这是and效果 
            cri.add(Restrictions.eq("username", "u1"));
            cri.add(Restrictions.eq("password","123"));
            
            
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
              session.getTransaction().commit();
    //3 
        
            Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            
            //条件 这是or效果
            cri.add(Restrictions.or(Restrictions.eq("username", "u1"),Restrictions.eq("username", "jack")));
            
            
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
              session.getTransaction().commit();
            
            // like 
            
            Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            /*MatchMode.ANYWHERE任何地方 MatchMode.START从开始 MatchMode.END表示u结尾的*/
            cri.add(Restrictions.like("username", "u", MatchMode.ANYWHERE));
            
            
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }    
             session.getTransaction().commit();

    分页

    cri.setFirstResult(0);
    cri.setMaxResults(5);

    分组与统计

    Creteria cri = session createCriteria(User.class);
        cri.setProjection(Projections.count("id"));
        Long count = (Long)cri.uniqueResult();
        syso(count);

    多个统计与分组 

    Creteria cri = session createCriteria(User.class);
    ProjectionList pList = Projections.projectionList();
    pList.add(Projections.max("id"));
    pList.add(Projections.min("id"));
    
    cri.setProjection(pList);
    
    Object[] array = (Object[]) cri.uniqueResult();
    System.out.println(array[0] + " : " +array[1]);

    排序 

    Session session = HibernateUtil.getSession();
            session.beginTransaction();
            Criteria cri = session.createCriteria(User.class);
            
            cri.addOrder(Order.desc("id"));
            cri.addOrder(Order.asc("username"));
    
            
            List<User> list = cri.list();
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
     session.getTransaction().commit();

    ------------------------------------------------

    ------------------------------------------------

    当然也可以使用原生sql

    Session session = HibernateUtil.getSession();
            session.beginTransaction();
            
            
            
            String sql = "select * from user";
            SQLQuery query = session.createSQLQuery(sql);
            List<Object[]> list = query.list();
            
            for(Object[] array : list) {
                System.out.println(array[0] + " : " + array[1]);
            }    
            
            
            
            
            
            String sql = "select * from user";
            SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
            List<User> list = query.list();
            
            for(User user : list) {
                System.out.println(user.getId() + " : " + user.getUsername());
            }
            
            
            
            
  • 相关阅读:
    条件判断
    字符串和编码
    排序算法 C++实现
    Ubuntu16.04下 pip的安装与使用
    剑指offer(19): 顺时针打印矩阵
    剑指offer(21):栈的压入、弹出序列
    派生类对象地址赋给基类指针后, 指针对基类和派生类的函数调用
    synergy: error while loading shared libraries: libdns_sd.so.1: cannot open shared object file
    OpenCV Error: Assertion failed + error: (-215) 使用ros opencv中的DNN模块报错
    《 MySQL必知必会 》下载 以及 Ubuntu16.04 下配置其使用的软件环境
  • 原文地址:https://www.cnblogs.com/itliucheng/p/4462731.html
Copyright © 2020-2023  润新知