01.搭建开发环境

02.基本查询

package com.gordon.test;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import com.gordon.domain.Book;
import com.gordon.utils.HibernateUtil; public class TestDemo1 { /**
* 分组查询
* 查询结果:
Hibernate:
select
publisher1_.name as col_0_0_,
count(*) as col_1_0_
from
t_book book0_,
t_publisher publisher1_
where
book0_.publisher_id=publisher1_.id
group by
book0_.publisher_id
[电子工业出版社, 3]
[北京大学出版社, 2]
[人民邮电出版社, 2]
*/
@Test
public void run5() {
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction(); List<Object[]> list = session.createQuery("select b.publisher.name, count(*) from Book b group by b.publisher").list();
for (Object[] book : list) {
System.out.println(Arrays.toString(book));
} transaction.commit();
} /**
* 聚合函数
* 查询结果:
Hibernate:
select
count(*) as col_0_0_
from
t_book book0_
Hibernate:
select
sum(book0_.price) as col_0_0_
from
t_book book0_
Hibernate:
select
max(book0_.price) as col_0_0_,
min(book0_.price) as col_1_0_
from
t_book book0_
Hibernate:
select
avg(book0_.price) as col_0_0_
from
t_book book0_
count: 7
sum: 330.04
Max:70.00 Min:31.00
avg: 47.15
*/
@Test
public void run4() {
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction(); String count = "select count(*) from Book";
Long c = (Long) session.createQuery(count).uniqueResult(); String sum = "select sum(b.price) from Book b";
Double s = (Double) session.createQuery(sum).uniqueResult(); String maxmin = "select max(b.price), min(b.price) from Book b";
Object[] mm = (Object[]) session.createQuery(maxmin).uniqueResult(); String avg = "select avg(b.price) from Book b";
Double a = (Double) session.createQuery(avg).uniqueResult(); DecimalFormat df = new DecimalFormat("00.00"); System.out.println("count: " + c);
System.out.println("sum: " + df.format(s));
System.out.println("Max:" + df.format(mm[0]) + " Min:" + df.format(mm[1]));
System.out.println("avg: " + df.format(a)); transaction.commit();
} /**
* 检索图书对象的部分属性
* 查询结果
Hibernate:
select
book0_.name as col_0_0_,
book0_.price as col_1_0_
from
t_book book0_
order by
book0_.price desc
[云计算技术及性能优化, 70.0]
[架构探险:轻量级微服务架构(下册), 63.2]
[中国冰雪梦, 54.0]
[生产微服务 , 44.0]
[VisualBasic2015实践教程 , 36.0]
[Photoshop图形图像处理, 31.84]
[C语言程序设计, 31.0]
*/
@Test
public void run3() {
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction(); List<Object[]> list = session.createQuery("select b.name, b.price from Book b order by b.price desc").list();
for (Object[] book : list) {
System.out.println(Arrays.toString(book));
} transaction.commit();
} /**
* 对查询结果进行排序
* 查询结果:
Hibernate:
select
book0_.id as id1_0_,
book0_.name as name2_0_,
book0_.price as price3_0_,
book0_.publisher_id as publishe4_0_
from
t_book book0_
order by
book0_.price desc
70.0, 云计算技术及性能优化
63.2, 架构探险:轻量级微服务架构(下册)
54.0, 中国冰雪梦
44.0, 生产微服务
36.0, VisualBasic2015实践教程
31.84, Photoshop图形图像处理
31.0, C语言程序设计
*/
@Test
public void run2() {
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction(); List<Book> list = session.createQuery("from Book b order by b.price desc").list();
for (Book book : list) {
System.out.println(book.getPrice() + ", " + book.getName());
} transaction.commit();
} /**
* 查询所有图书
* 查询结果:
Hibernate:
select
book0_.id as id1_0_,
book0_.name as name2_0_,
book0_.price as price3_0_,
book0_.publisher_id as publishe4_0_
from
t_book book0_
云计算技术及性能优化
C语言程序设计
中国冰雪梦
Photoshop图形图像处理
VisualBasic2015实践教程
生产微服务
架构探险:轻量级微服务架构(下册)
*/
@Test
public void run1() {
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction(); List<Book> list = session.createQuery("from Book").list();
for (Book book : list) {
System.out.println(book.getName());
} transaction.commit();
}
}

Hibernate- 基本查询的更多相关文章

  1. hibernate模糊查询

    hibernate模糊查询-Restrictions.ilike & Expression.like Criteria criteria = session.createCriteria(Ta ...

  2. Hibernate的查询方式总结

    Hibernate的查询方式大体有三种,分别是HQL QBC和SQL三种.在网上查阅一一些资料,做了一个简单的总结. 1. SQL sql 是面向数据库表查询,from 后面跟的是表名,where 后 ...

  3. atitit。 hb Hibernate sql 查询使用

    atitit. hb  Hibernate sql 查询使用 #----------返回list<map>法..这个推荐使用.      q.setResultTransformer(Tr ...

  4. Hibernate高级查询QBC条件设置——Restrictions用法 引自:http://www.cnblogs.com/evon168/archive/2010/10/29/1863059.html

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...

  5. Hibernate HQL查询:

    Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查 ...

  6. Hibernate HQL查询语句总结

    Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";L ...

  7. Hibernate的查询,二级缓存,连接池

    Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...

  8. hibernate sql查询转换成VO返回list

    hibernate sql查询转换成VO @Override public List<FenxiVo> getTuanDuiFenxiList(FenxiVo FenxiVo,Intege ...

  9. Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)

    Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...

  10. Hibernate【查询、连接池、逆向工程】

    前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式....到目前为止,我们都是使用一些简单的主键查询阿...使用HQL查询所有的数据....本博文主要讲解Hiberna ...

随机推荐

  1. Eclipse 调试器:零距离接触实战技巧

    http://my.oschina.net/willSoft/blog/37784调试的方法虽然千千万万,但归根结底,就是找到引发错误的代码.Eclipse调试器的目标是让程序员能对本地或远程程序进行 ...

  2. FA_会计中的折旧方式的种类(概念)

    2014-06-08 Created By BaoXinjian

  3. android使用JsonWriter拼json字符串

    JsonWriter使用 Example: 拼一个如下的json格式String {    [        {            "id": 912345678901,    ...

  4. TCP客户/服务器简单Socket程序

    建立一个 TCP 连接时会发生下述情形: 1. 服务器必须准备好接受外来的连接.这通常通过调用 socket.bind 和 listen 这三个函数来完成,我们称之为被动打开. 2. 客户通过调用 c ...

  5. 基于PHP规范的自动加载方式(composer配置)

    针对PHP这种编程语言,到目前FIG指定了五个规范,分别如下: PSR0:自动加载: PSR1:基本代码规范: PSR2:代码样式规范: PSR3:日志接口规范: PSR4:自动加载规范: 看上去PS ...

  6. Learning Django: the hard way (1)

    Learning Django: the hard way (1) What does "runserver" do? Django provides a light-weight ...

  7. sed匹配两种重要思想

    1,sed匹配两种重要思想 屏蔽思想:遮住不想要的 挑出思想:遮住所有,挑出想要的. 2,实例 屏蔽思想: [root@lanny ~]# sed -n '2p' file.txt |sed -r ' ...

  8. Objective-C之成魔之路【13-预处理程序】

    郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 预处理提供 ...

  9. 服务器搭建2 VSFTP搭建FTP服务器

    FTP服务器是平时应用最为广泛的服务之一.VSFTP是Very Secure FTP的缩写,意指非常安全的FTP服务.VSFTP功能强大,通过结合本地系统的用户认证模块及其多功能的配置项目,可以快速有 ...

  10. How to merge Scala Lists

    Scala List FAQ: How do I merge a List in Scala? NOTE: I wrote the solutions shown below a long time ...