博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate- 基本查询
阅读量:6452 次
发布时间:2019-06-23

本文共 4477 字,大约阅读时间需要 14 分钟。

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
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
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
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
list = session.createQuery("from Book").list(); for (Book book : list) { System.out.println(book.getName()); } transaction.commit(); }}

 

转载地址:http://ecgwo.baihongyu.com/

你可能感兴趣的文章
mitdump爬取当当网APP图书目录
查看>>
Mac 环境 下使用Charles 抓包Http/Https请求
查看>>
大型网站系统架构的演化
查看>>
CF912A Tricky Alchemy
查看>>
[ZJOI2009]狼和羊的故事 BZOJ1412
查看>>
IntelliJ IDEA 如何在同一个窗口创建多个项目--超详细教程
查看>>
codeforces 891 b
查看>>
韩梦微助手
查看>>
错误:SSL peer shut down incorrectly
查看>>
Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数,其他函数)...
查看>>
GCC编译器代码优化
查看>>
I2C与OV9650的问题
查看>>
小程序分包加载
查看>>
initWithFrame: 和 initWithCoder: 区别
查看>>
从用户感知谈软件性能测试
查看>>
nodejs基础 -- EventEmitter
查看>>
python 解析html
查看>>
老菜鸟学习:Javascript 将html转成pdf
查看>>
java多线程通信问题
查看>>
树莓派常用小命令
查看>>