Hibernate学习-07:CRUD(增删改查)操作之查询多条/所有记录HQL、QBC、SQL三种方式
2017-04-19 11:14
706 查看
查询所有:(环境同
)
在这里呢,先给大家演示一下hibernate中查询多条记录的几种方式,具体的细节后面再讲。
1、HQL查询:
修改测试类:
可加查询条件,例如:
2、QBC查询:
修改测试类:
添加查询条件:
3、SQL查询:
修改测试类:
每次打印都是一个数组形式,跟前面两种不同。
如果在使用SQL查询的时候,要返回对象,怎么做呢?示例如下:
三种查询多条记录的方式就介绍到这,后续的学习中我们还会进一步讲解。
Hibernate学习-03
)在这里呢,先给大家演示一下hibernate中查询多条记录的几种方式,具体的细节后面再讲。
1、HQL查询:
修改测试类:
@Test //查询所有:HQL //HQL:Hibernate query language,Hibernate查询语言,面向对象的查询 public void demo7(){ Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //操作 Query query=session.createQuery("from Customer");//此处Customer是类名,可加包名,也可以省略(因为配置文件中配置了) List<Customer> list = query.list(); for (Customer customer:list){ System.out.println(customer); } //提交事务 tx.commit(); //释放资源 session.close(); sessionFactory.close(); }运行测试:
可加查询条件,例如:
Query query=session.createQuery("from Customer where name = ?");//此处Customer是类名,可加包名,也可以省略(因为配置文件中配置了) query.setString(0, "武松"); List<Customer> list = query.list(); for (Customer customer:list){ System.out.println(customer); }也可以使用自定义的名字来代替问号占位符:
Query query=session.createQuery("from Customer where name =:aaa");//此处Customer是类名,可加包名,也可以省略(因为配置文件中配置了) query.setString("aaa", "武松");
2、QBC查询:
修改测试类:
@Test //查询所有:QBC //QBC:Query By Criteria public void demo8(){ Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //操作 Criteria criteria=session.createCriteria(Customer.class); List<Customer> list = criteria.list(); for(Customer customer:list){ System.out.println(customer); } //提交事务 tx.commit(); //释放资源 session.close(); sessionFactory.close(); }运行测试,不再赘述。
添加查询条件:
//操作 Criteria criteria=session.createCriteria(Customer.class); criteria.add(Restrictions.eq("name", "武松"));//Restriction提供了一些限制函数,用于设置查询条件 List<Customer> list = criteria.list(); for(Customer customer:list){ System.out.println(customer); }
3、SQL查询:
修改测试类:
@Test //查询所有:SQL public void demo9(){ Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //操作 SQLQuery sqlQuery=session.createSQLQuery("select * from customer");//此处Customer是表名,createSQLQuery()函数接收sql语句为参数 List<Object[]> list = sqlQuery.list(); //这个list跟之前的list泛型类型不一样,此时每条记录被封装成一个数组,多条记录被放到一个包含多个数组的list集合中 for(Object[] objs:list){ System.out.println(Arrays.toString(objs)); } //提交事务 tx.commit(); //释放资源 session.close(); sessionFactory.close(); }运行结果:
每次打印都是一个数组形式,跟前面两种不同。
如果在使用SQL查询的时候,要返回对象,怎么做呢?示例如下:
@Test //查询所有:SQL public void demo9(){ Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //操作 /*SQLQuery sqlQuery=session.createSQLQuery("select * from customer");//此处Customer是表名,createSQLQuery()函数接收sql语句为参数 List<Object[]> list = sqlQuery.list(); //这个list跟之前的list泛型类型不一样,此时每条记录被封装成一个数组,多条记录被放到一个包含多个数组的list集合中 for(Object[] objs:list){ System.out.println(Arrays.toString(objs)); }*/ SQLQuery sqlQuery=session.createSQLQuery("select * from customer"); sqlQuery.addEntity(Customer.class);//往Customer对象中去封装 List<Customer> list = sqlQuery.list(); for(Customer customer:list){ System.out.println(customer); } //提交事务 tx.commit(); //释放资源 session.close(); sessionFactory.close(); }运行结果:
三种查询多条记录的方式就介绍到这,后续的学习中我们还会进一步讲解。
相关文章推荐
- Hibernate三种数据操作方式HQL-Criteria-Sql
- Hibernate中的三种查询HQL、Criteria、Sql学习(二)
- Hibernate学习-03:入门案例(CRUD(增删改查)操作之添加记录)
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate中的三种查询语句(hql,sql,qbc)
- Hibernate中的三种查询HQL、Criteria、Sql学习(一)
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
- Hibernate学习-05:CRUD(增删改查)操作之修改记录
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
- Hibernate学习-04:CRUD(增删改查)操作之根据主键查询
- Hibernate 的三种查询方式:HQL、Criteria、Sql
- Hibernate中的三种查询HQL、Criteria、Sql学习(三)
- Hibernate总的来说共有三种查询方式:HQL、QBC和SQL三种