Hibernate——HQL(六)

Get和Load查询的区别

get:就是返回一个对象,如果没有对象的话就返回一个null值

Session currentSession = HibernateUtil.getCurrentSession();
currentSession.beginTransaction();
User user = currentSession.get(User.class, 15L);
System.out.println(user);//get就是返回一个对象没有的话就返回null

currentSession.getTransaction().commit();
currentSession.close();

load:在执行load的时候存下一个代理对象,但并没有向数据库发送请求语句,只有当查询除了ID以外的值就会发送SQL语句,如果找不到对应的记录就会爆出一个异常:No row with the given identifier exists:

Session currentSession = HibernateUtil.getCurrentSession();
currentSession.beginTransaction();
User user = currentSession.load(User.class, 10L);
System.out.println(user.getUser_id());//load只是单纯的存下了一个代理对象 在执行的时候并没有向数据库里面做查询操作
try{
    System.out.println(user.getUser_name());//当调用除了ID之外的就会发送SQL语句 如果找不到 就会爆出一个异常:No row with the given identifier exists:
}catch (Exception e){
    System.out.println(e.getMessage());
}

currentSession.getTransaction().commit();
currentSession.close();

 

HQL

简单查询

Session session = HibernateUtil.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
Query query = session.createQuery("select u from User u order by user_id desc");
List<User> list = query.list();
for (User user : list) {
    System.out.println(user.getUser_name());//Char没有使用包装类 所以这里出了个错误
}

transaction.commit();
session.close();

条件查询

Session session = HibernateUtil.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
/**第一种通过
 * Query query = session.createQuery("select u from User u where user_id=?1");
 * query.setParameter(1,13L);
 * 位置来查询**/
Query query = session.createQuery("select u from User u where user_id=  :one");
query.setParameter("one",15L);
List<User> list = query.list();
for (User user : list) {
    System.out.println(user.getUser_name());
}

 

对象封装查询

    //投影查询
    Session session = HibernateUtil.getCurrentSession();
    Transaction transaction = session.getTransaction();
    transaction.begin();
    //单列查询
    /**
     * List<Object> list = session.createQuery("select u.user_name from User u").list();
     * System.out.println(list);
     */

    //多列查询

    /**List<Object[]> list = session.createQuery("select u.user_name,u.user_id,u.user_code from User u").list();
        for (Object[] objects : list) {
        System.out.println(Arrays.toString(objects));
        }
    }**/

    //封装到对象中 前提是要有相应的构造器
    List<User> list = session.createQuery("select new User(user_id,user_code,user_name) from User u").list();
    System.out.println(list);
    transaction.commit();
    session.close();



 

这个就是构造器

 

分页查询

Session session = HibernateUtil.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
Query query = session.createQuery("select u.user_name from User u");
query.setFirstResult(1);//角标
query.setMaxResults(2);//一次查多少
List<Object> list = query.list();
for (Object o : list) {
    System.out.println(o);
}
transaction.commit();
session.close();

统计查询

Session session = HibernateUtil.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
Object o = session.createQuery("select count(*) from User").uniqueResult();//这边返回的是一个uniqueResult()
System.out.println("查询的记录条数"+o);
transaction.commit();
session.close();

分组统计查询

Session session = HibernateUtil.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
Query query = session.createQuery("select user_name,count(*) from User group by user_name");
List<Object[]> list = query.list();
for (Object[] objects : list) {
    System.out.println(Arrays.toString(objects));
}

连接查询

Session session = HibernateUtil.openSession();
 Transaction transaction = session.getTransaction();
 transaction.begin();
/* //内连接
 Query query = session.createQuery("from User u inner join u.role");

 List<Object[]> list = query.list();
 System.out.println("我是"+list.size());
 for (Object[] objects : list) {

     System.out.println(Arrays.toString(objects));
 }*/
 List<User> list = session.createQuery("from User u inner join Fetch u.role").list();
 for (User user : list) {
     System.out.println(user);
 }

 transaction.commit();
 session.close();

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值