博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rhythmk 学习 Hibernate 09 - Hibernate HQL
阅读量:6239 次
发布时间:2019-06-22

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

1、初始数据

@Test	public void test01() {		Session session = null;		try {			session = HibernateUtil.getSessionFactory().openSession();			session.beginTransaction();			for (int i = 0; i < 100; i++) {				book entity = new book();				entity.setBookCode("book-code-" + i);				entity.setBookname("book-name" + i);				entity.setPrice(i);				session.save(entity);			}			session.getTransaction().commit();		} catch (HibernateException e) {			e.printStackTrace();		} finally {			if (session != null)				session.close();		}	}

  

2、查询对象

@Test	public void test02() {		Session session = null;		// form 对象名 非 表名		String sql = " from book ";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			List
list = (List
) query.list(); for (book b : list) { System.out.println(b); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }

  

3、查询单字段

@Test	public void test03() {		/*		 * 查询单字段		 */		Session session = null;		String sql = "  select bookname from book ";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			List
list = (List
) query.list(); for (String b : list) { System.out.println(b); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }

  

4、查询指定字段

@Test	public void test04() {		/*		 * 查询多字段,且指定条数Object[]		 */		Session session = null;		String sql = "  select   bookname,bookCode from book ";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			// 指定5条			query.setMaxResults(5);			List
list = (List
) query.list(); for (Object[] b : list) { System.out.println(b[0] + "," + b[1]); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }

  

5、返回Map对象

@Test	public void test05() {		/*		 * 查询多字段,且指定Map		 */		Session session = null;		String sql = "  select   new Map(bookname,bookCode) from book ";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			query.setMaxResults(5);			List list = (List) query.list();			for (Map b : list) {				System.out.println((String) b.get("0") + ","						+ (String) b.get("1"));			}		} catch (HibernateException e) {			e.printStackTrace();		} finally {			if (session != null)				session.close();		}	}

  

6、序列化参数

   6.1、

@Test	public void test06() {		Session session = null;		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			query.setInteger("book_id", 5);			query.setDouble("price", 5);			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as			// col_1_0_ from t_book book0_ where book0_.book_id>? and			// book0_.price>?			List
list = (List
) query.list(); for (Object[] b : list) { System.out.println(b[0] + "," + b[1]); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }

  

   6.2、

@Test	public void test07() {		Session session = null;		String sql = "  select   bookname,bookCode from book   where book_id>:book_id and price>:price";		try {			session = HibernateUtil.getSessionFactory().openSession();			Query query = session.createQuery(sql);			query.setInteger(0, 5);			query.setDouble(1, 5);			// Hibernate: select book0_.bookname as col_0_0_, book0_.bookCode as			// col_1_0_ from t_book book0_ where book0_.book_id>? and			// book0_.price>?			List
list = (List
) query.list(); for (Object[] b : list) { System.out.println(b[0] + "," + b[1]); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }

  

7、执行SQL

@Test	public void test08_jdbc() {		Session session = null;//Rhythmk 		String sql = "  select bookCode,bookname from t_book  where  book_id>? and price>?";		try {			session = HibernateUtil.getSessionFactory().openSession();			SQLQuery query = session.createSQLQuery(sql);			query.setInteger(0, 5);			query.setDouble(1, 5);			List
list = (List
) query.list(); for (Object[] b : list) { System.out.println(b[0] + "," + b[1]); } } catch (HibernateException e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }}

  http://pan.baidu.com/s/1hqGDNq0

 

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

你可能感兴趣的文章
django无法加载admin的静态内容的问题(Centos7+Nginx+uwsgi环境下)
查看>>
windows 2008 启用.NET Framework 3.5
查看>>
Linux -- Ubuntu搭建java开发环境
查看>>
MVC视图中Html常见的辅助方法
查看>>
分享一下刚刚HP电话面试。。。。。。。。我估计我挂了,不过还是要来分享一下...
查看>>
PT 转 PX
查看>>
平凡世界里的万千思绪
查看>>
(二)java环境搭建
查看>>
深入推荐引擎相关算法 - 协同过滤2
查看>>
mybatis逆向工程之配置
查看>>
使用.NET 4.0+ 操作64位系统中的注册表
查看>>
剑指offer——面试题26:判断二叉树B是否为二叉树A的子结构
查看>>
scrapy主动退出爬虫的代码片段
查看>>
ny12 喷水装置(二)
查看>>
C\C++语言细节(2)
查看>>
Jenkins持续部署-自动生成版本号
查看>>
设计模式--代理模式
查看>>
javascript基础知识--最基础的
查看>>
[转] vue自定义组件(通过Vue.use()来使用)即install的使用
查看>>
[转] 函数声明和函数表达式——函数声明的声明提前
查看>>