hibernate框架学习笔记12:查询优化

类级别查询优化:

创建一个实体类:

package domain;

import java.util.HashSet;
import java.util.Set; //客户实体
public class Customer { private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//使用set集合,表达一对多关系
private Set<LinkMan> linkMens = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMens() {
return linkMens;
}
public void setLinkMens(Set<LinkMan> linkMens) {
this.linkMens = linkMens;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
}
}

自定义工具类:

package utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils {
private static SessionFactory sf; static{
//1 创建,调用空参构造
Configuration conf = new Configuration().configure();
//2 根据配置信息,创建 SessionFactory对象
sf = conf.buildSessionFactory();
} //获得session => 获得全新session
public static Session openSession(){
//3 获得session
Session session = sf.openSession(); return session; }
//获得session => 获得与线程绑定的session
public static Session getCurrentSession(){
//3 获得session
Session session = sf.getCurrentSession(); return session;
}
}

测试类示例:

package lazy;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import domain.Customer;
import utils.HibernateUtils; //懒加载|延迟加载
public class Demo { @Test
// get方法 : 立即加载.执行方法时立即发送sql语句查询结果
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//---------------------------------------------------- Customer c = session.get(Customer.class, 2l); System.out.println(c);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
// load方法(默认):是在执行时,不发送任何sql语句.返回一个对象.使用该对象时,才执行查询.
// 延迟加载: 仅仅获得没有使用.不会查询.在使用时才进行查询.
// 是否对类进行延迟加载: 可以在配置文件中通过在class元素上配置lazy属性来控制.
//lazy:true 加载时,不查询.使用时才查询b
//lazy:false 加载时立即查询.
public void fun2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//---------------------------------------------------- Customer c = session.load(Customer.class, 2l); //----------------------------------------------------
tx.commit();
session.close();
System.out.println(c); } }

为了提高效率,建议使用延迟加载,要确保调用属性时候,session是打开的,否则会报出异常

关联级别优化:

配置文件:集合的属性配置

lazy属性: 决定是否延迟加载
true(默认值): 延迟加载,懒加载
false: 立即加载
extra: 极其懒惰

fetch属性: 决定加载策略.使用什么类型的sql语句加载集合数据
select(默认值): 单表查询加载
join: 使用多表查询加载集合
subselect:使用子查询加载集合

结论:为了提高效率.fetch的选择上应选择select. lazy的取值应选择 true. 全部使用默认值.

上一篇:C语言中 指针、引用和取值


下一篇:以Windows服务方式运行.NET Core程序