Hibernate进阶学习4
Hibernate进阶学习4
深入学习hibernate的查询语句

测试HQL查询
package com.hibernate.test; import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.List; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 测试HQL语句(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
*/
public class HibernateTest {
/**
* 排序查询
*/
@Test
public void test1() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer order by cust_id";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 统计查询
*/
@Test
public void test2() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "select count(*) from Customer";
String hql1 = "select sum(cust_id) from Customer";
String hql2 = "select avg(cust_id) from Customer";
String hql3 = "select max(cust_id) from Customer";
String hql4 = "select min(cust_id) from Customer";
//2)创建查询对象
Query query = session.createQuery(hql2);
//3)执行查询
Number number = (Number) query.uniqueResult(); System.out.println(number); /*******************************************************/ tx.commit();
session.close();
} /**
* 投影查询
*/
@Test
public void test3() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "select new Customer(cust_id,cust_name) from Customer";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
}
}
package com.hibernate.test; import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.Arrays;
import java.util.List; /**
* @author: XDZY
* @date: 2018/11/19 14:35
* @description: 测试HQL多表查询(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
*/
public class HibernateTest2 {
/**
* 内连接
*/
@Test
public void test1() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer c inner join c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Object[]> list = query.list(); for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
} /*******************************************************/ tx.commit();
session.close();
} /**
* 迫切内连接
* 将查询到的关联的对象也封装到查询的对象中
*/
@Test
public void test2() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
String hql = "from Customer c inner join fetch c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Customer> list = query.list(); System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 左(右)外连接
*/
@Test
public void test3() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //1)书写HQL语句
//String hql="from Customer c left join c.linkMens";
String hql = "from Customer c right join c.linkMens";
//2)创建查询对象
Query query = session.createQuery(hql);
//3)执行查询
List<Object[]> list = query.list(); for (Object[] obj : list) {
System.out.println(Arrays.toString(obj));
} /*******************************************************/ tx.commit();
session.close();
}
}
测试Criteria查询
package com.hibernate.test; import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.*;
import org.junit.Test; import java.util.List; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 测试criteria语句(hibernate独有的无语句的全面向对象的查询语法)(适合单表查询)
*/
public class HibernateTest3 {
/**
* 排序查询
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ Criteria criteria = session.createCriteria(Customer.class);
//criteria.addOrder(Order.asc("cust_id"));
criteria.addOrder(Order.desc("cust_id"));
List list = criteria.list();
System.out.println(list); /*******************************************************/ tx.commit();
session.close();
} /**
* 离线查询
* 就是在不创建session的情况下也能进行数据库操作(比如在service和web层调用)
*/
@Test
public void test1() {
//创建离线对象
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
dc.add(Restrictions.idEq(3L)); //创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ Criteria criteria = dc.getExecutableCriteria(session);
List list = criteria.list();
System.out.println(list); /*******************************************************/ tx.commit();
session.close();
}
}
测试类级别加载策略
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
<class name="Customer" table="cst_customer" lazy="false">
<id name="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property> <!-- lazy属性:决定是否延迟加载
true(默认值):延迟加载,懒加载
false:立即加载
extra:极其懒惰
fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
select(默认值):单表查询加载
join:使用多表查询加载集合
subselect:使用子查询加载集合 -->
<!-- batch-size:抓取集合的数量为3
抓取客户的集合时,一次抓取几个客户的联系人集合 -->
<set name="linkMens" batch-size="3">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_gender"></property>
<property name="lkm_name"></property>
<property name="lkm_phone"></property>
<property name="lkm_email"></property>
<property name="lkm_qq"></property>
<property name="lkm_mobile"></property>
<property name="lkm_memo"></property>
<property name="lkm_position"></property> <!-- fetch属性:决定加载的sql语句
select:使用单表查询
join:多表查询
lazy属性:决定加载时机
false:立即加载
proxy:由customer的类级别加载策略决定 -->
<many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="join" lazy="proxy">
</many-to-one>
</class>
</hibernate-mapping>
package com.hibernate.test; import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.*;
import org.junit.Test; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 类级别加载策略
*/
public class HibernateTest4 {
/**
* 懒加载|延迟加载
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //立即加载
//Customer customer = session.get(Customer.class, "2L"); //延迟加载:查询时只返回一个代理对象,在使用时,根据关联的session查询数据库返回结果
//为了更好的性能,建议延迟加载;延迟加载貌似只是推迟了查询
Customer customer = session.load(Customer.class, "2L");
System.out.println(customer); /*******************************************************/ tx.commit();
session.close();
}
}
测试关联级别加载策略
package com.hibernate.test; import com.hibernate.domain.Customer;
import com.hibernate.domain.LinkMan;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import java.util.Set; /**
* @author: XDZY
* @date: 2018/11/16 10:26
* @description: 关联级别加载策略
*/
public class HibernateTest5 {
/**
* lazy与fetch的使用
*/
@Test
public void test() {
//创建Session对象
Session session = HibernateUtils.openSession();
//开启事务并获取事务对象
Transaction tx = session.beginTransaction(); /********************* 数据库操作 **********************/ //立即加载
Customer customer = session.get(Customer.class, "2L"); Set<LinkMan> linkMens = customer.getLinkMens();
System.out.println(linkMens.size());
System.out.println(linkMens); /*******************************************************/ tx.commit();
session.close();
}
}
Hibernate进阶学习4的更多相关文章
- Hibernate进阶学习3
Hibernate进阶学习3 测试hibernate的多表关联操作(一对多,多对一,多对多) 表之间的关系主要在类与元数据配置文件中体现 package com.hibernate.domain; i ...
- 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)
[SSH进阶之路]Struts + Spring + Hibernate 进阶开端(一) 标签: hibernatespringstrutsssh开源框架 2014-08-29 07:56 9229人 ...
- Hibernate基础学习2
Hibernate基础学习2 测试hibernate的一级缓存,事务以及查询语句 1)Hibernate的一些相关概念 hibernate的一级缓存 1)缓存是为了提高该框架对数据库的查询速度 2)一 ...
- PHP程序员进阶学习书籍参考指南
PHP程序员进阶学习书籍参考指南 @heiyeluren lastmodify: 2016/2/18 [初阶](基础知识及入门) 01. <PHP与MySQL程序设计(第4版)> ...
- Matlab 进阶学习记录
最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal = proposal_config('image_means', ...
- (Hibernate进阶)Hibernate系列——总结篇(九)
这篇博文是hibernate系列的最后一篇,既然是最后一篇,我们就应该进行一下从头到尾,整体上的总结,将这个系列的内容融会贯通. 概念 Hibernate是一个对象关系映射框架,当然从分层的角度看,我 ...
- zuul进阶学习(二)
1. zuul进阶学习(二) 1.1. zuul对接apollo 1.1.1. Netflix Archaius 1.1.2. 定期拉 1.2. zuul生产管理实践 1.2.1. zuul网关参考部 ...
- ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改
ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...
- Struts2进阶学习4
Struts2进阶学习4 自定义拦截器的使用 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <! ...
随机推荐
- GOPS 2018全球运维大会上海站 参会感悟梳理
今天遇到很多优秀的讲师.业界的大伽,很开心 现在把get到的信息梳理一下:(1)想解决性能问题,一定要在缓存上下功夫:[nginx上有好多文章可以做,真是博大精深呢<深入理解Nginx:模块开发 ...
- 前台异步传过来的URL中获取token/获取string链接中的token
1.链接例子: string url = "http://domainName:port/1/2/3/4.htm?sysCode=1001020&token=QXJzemR3YXlW ...
- 动态LINQ(Lambda表达式)构建
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- python面试题——网络编程和并发
1.简述 OSI 七层协议. 物理层(电信号.比特流) 基于电器特性发送高低电压(电信号) RJ45.IEEE802.3 数据链路层(数据帧) 定义了电信号的分组方式,分组方式后来形成了统一的标准,即 ...
- 一本通 1260:【例9.4】拦截导弹(Noip1999)
拦截导弹(Noip1999) 经典dp题目,这个做法并非最优解,详细参考洛谷导弹拦截,想想200分的做法. #include <iostream> #include <cstdio& ...
- svg的基本图形与属性【小尾巴的svg学习笔记1】
因为项目有可能用到, 所以学习了一下,做此笔记,图截自慕课网,侵删. 一.基本图形 1.矩形 x,y定义矩形的左上角坐标: width,height定义矩形的长度和宽度: rx,ry定义矩形的圆角半径 ...
- Android Studio 小技巧(1):如何导入AAR文件
1. 导入AAR.JAR文件 File- > New -> New Module 这样子AAR文件就导入了,然后在app中的build.gradle中做如下添加 dependencies ...
- Debug view 是个好工具
有时候不用 VS 调试, 在 代码里面加入 Debug.Writeline(" Debug information!!"); 这个时候打开 debug view 就可以检测出输出 ...
- (C#) 线程之 AutoResetEvent, EventHandle.
AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...
- Android 监听APP进入后台或切换到前台方案对比
在我们开发的过程中,经常会遇到需要我们判断app进入后台,或者切换到前台的情况.比如我们想判断app切换到前台时,显示一个解锁界面,要求用户输入解锁密码才能继续进行操作:我们想判断app切换到后台,记 ...