• Hibernate HQL查询


    package cn.sasa.test;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    import cn.sasa.domain.account;
    import cn.sasa.utils.HibernateUtils;
    
    public class TestHQL {
        @org.junit.Test
        public void test() {
            Session session = HibernateUtils.getCurrentSession();
            Transaction tran = session.beginTransaction();
            
            String hql = "from account";//查询所有记录
            var query = session.createQuery(hql);
            var list = query.list();//返回多个记录
            //var list = query.uniqueResult();//返回一条记录
            System.out.println(list);
            
            String hql1 = "from account where id=:id";//查找一条记录
            var query1 = session.createQuery(hql1);
            query1.setParameter("id", 1);
            var acObj = (account)query1.uniqueResult();
            System.out.println(acObj);
            
            String hql2 = "from account where id=?0";//查找一条记录
            var query2 = session.createQuery(hql2);
            query2.setParameter(0, 1);
            var acObj2 = (account)query2.uniqueResult();
            System.out.println(acObj2);
            
            //分页
            String hql3 = "from account ";
            var query3 = session.createQuery(hql3);
            //limit ?,?
            query3.setFirstResult(0);
            query3.setMaxResults(3);
            var list3 = query3.list();
            System.out.println(list3);
            
            
            tran.commit();
            session.close();
        }
        
        
    }
  • 相关阅读:
    [置顶] 呼~~~~--历时几个星期终于搞好了HTTPS协议---阿里云
    云数据库连接权限等
    Developer连接Oracle报错“ORA-12541
    VUE项目使用
    数据库优化面试内容
    CentOS配置svn
    老年痴呆博客引导
    日常问题记录
    Spring项目定时任务
    程序员逻辑思维题解答;
  • 原文地址:https://www.cnblogs.com/SasaL/p/11653590.html
Copyright © 2020-2023  润新知