Hibernate查询

2009-08-18 19:40

大概就这5种查询方式:

package com.cstp.hibernate;

import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class QueryUser {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
     Logger log = Logger.getAnonymousLogger();
   Configuration cfg = new Configuration().configure();//from xml
        SessionFactory factory = cfg.buildSessionFactory();
  
   Session session = factory.openSession();
   log.info("==========1=============");
//=================1========================
   SQLQuery sq = session.createSQLQuery("select * from test");
   List<Object[]> list1 = sq.list();
   //list1 的结构([username,password],[username,password],[username,password],...)
   for(Object[] obj:list1){
    for(Object o:obj){
    log.info(o.toString());
   }
    log.info("************");
   }
   log.info("==========2=============");
//=================2========================
   Criteria ct=session.createCriteria(com.cstp.hibernate.User.class);
   ct.add(Restrictions.eq("username", "chenlong"));
   List<User> list2 = ct.list();
   for(User user:list2){
    log.info("username:"+user.getUsername() +"/tpassword:"+ user.getPassword());
   }
   log.info("===========3============");
   //=================3========================  
   DetachedCriteria dc=DetachedCriteria.forClass(com.cstp.hibernate.User.class);
   dc.add(Restrictions.eq("password", "123"));
   List<User> list3 = dc.getExecutableCriteria(session).list();
   for(User user:list3){
    log.info("username:"+user.getUsername() +"/tpassword:"+ user.getPassword());
   }
   log.info("============4===========");
//=================4========================  
   //Query query = session.createQuery("from User test");
   Query query = session.createQuery("from User");//2种写法都可以
  
   List<User> list4 = query.list();
   for(User user:list4){
    log.info("username:"+user.getUsername() +"/tpassword:"+ user.getPassword());
   }
   log.info("============5===========");
//=================5 取得一个已被填充了属性的对象,符合条件的有多条,取第一条========================

   User user = (User) session.load(User.class, new String("402890ac232ba99801232ba999a70001"));
   log.info("username:"+user.getUsername() +"/tpassword:"+ user.getPassword());
}

}
运行结果:

2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ==========1=============
Hibernate: select * from test
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 402890ac232ba99801232ba999a70001
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: chenlong
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:00:18.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:00:18.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ************
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 402890ac232ba9e001232ba9e1fb0001
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: hww
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:00:36.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:00:36.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ************
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 402890ac232baa3c01232baa3e250001
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: liwei
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 333333
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:01:00.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 12:01:00.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ************
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 402890ac232c9eec01232c9eee280001
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: zhu
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 123456
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 16:28:16.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: 2009-08-18 16:28:16.0
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ************
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ==========2=============
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_, this_.createTime as createTime0_0_, this_.expireTime as expireTime0_0_ from test this_ where this_.username=?
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:chenlong password:123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ===========3============
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_, this_.createTime as createTime0_0_, this_.expireTime as expireTime0_0_ from test this_ where this_.password=?
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:chenlong password:123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:hww password:123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ============4===========
Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.password as password0_, user0_.createTime as createTime0_, user0_.expireTime as expireTime0_ from test user0_
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:chenlong password:123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:hww password:123
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:liwei password:333333
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:zhu password:123456
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: ============5===========
2009-8-18 19:27:28 com.cstp.hibernate.QueryUser main
信息: username:chenlong password:123

 

1方式:尽管sql语句写法灵活,但是结果显然不好。

2方式,3方式:只能加where限制条件,不能固定自己想查哪些字段,就是select * from 。。。(但是也没影响,不用的字段值放那不用也行)。

4方式:用的HQL语言:sql语句也灵活。

HQL 语法与SQL一致,区别在于:
   1 如果查询一个对象的结果集,可以省略select部分,只用from
   2 from 的内容可以是Mapping配置中的类名也可以是Mapping配置中的表名
   3 使用group by 的时候,select不可以省略
   4 分组查询结果不会映射成对象

5方式:要取得数据库的一条记录作为对象也很不错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值