持久层 框架 hibernate mybatis
orm 框架
1、导包
2、建库建表
3、创建表的对应类
package com.stevezong.domain;
public class Customer {
/*
* CREATE TABLE `cst_customer` (
`cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT '联系人',
`cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
*/
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;
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 + "]";
}
}
4、编写映射xml文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5、编写主配置文件
hibernate.properties 文件中有可用的所有的键值对
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6、测试
package t;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.stevezong.domain.Customer;
public class TestH {
@Test
public void t1() {
Configuration conf = new Configuration().configure();
SessionFactory sessionFactry = conf.buildSessionFactory();
Session session = sessionFactry.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("ICETECH");
session.save(c);
tx.commit();
session.close();
sessionFactry.close();
}
}
1、映射文件配置
-->
2、hibernate 主配置文件
必选
可选
映射文件
路径
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
API:
1、Configuration 配置管理类对象
Configuration conf = new Configuration();
配置加载类用于在载主配置 orm元数据加载
>conf.configure();
有四种重载
一般都是用这个空参数的
加载src下的hibernate.cfg.xml 文件
>config.configure(“cn/config/hibernate.cfg.xml”); 加载指定路径下指定名称的主配置文件
config.buildSessionFactory(); 创建session的工厂对象
但是在不同版本的hibernate 获取session工厂的方式不一样了
=========================================================================================
-如果使用的是hibernate4.2之前的版本,那么方法就这么写:
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
-如果是hibernate4.3之后的版本,那么方法就这么写:
导入包更换:org.hibernate.boot.registry.StandardServiceRegistryBuilder;
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config .getProperties()).build();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
hibernate5之后连上面的包都可以省略了:
//创建配置对象(读取配置文档)
Configuration config = new Configuration().configure();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory();
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
=========================================================================================
2、SessionFactory session的工厂(或者说代表了这个hibernate.cfg.xml配置文件)就一个功能就是创建session对象
sessionfactory 负责保存和使用所有配置信息 消耗内存非常大
属于线程安全的对象设计
SessionFactory buildSessionFactory = configuration.buildSessionFactory();
1. sf.openSession(); 创建一个sesison对象
2. sf.getCurrentSession(); 创建session或取出session对象(这个相对上边功能更强大)获得一个与线程绑定的session对象
3. sf.close();关闭工厂方法
3、Session
表达hibernate 框架和数据库之间的连接会话 session类 核心对象
4、Transaction 事物对象
Transaction tx = session.getTransaction();
//开启事物并获得操作事物的对象()一般用这个
Transaction tx = session.beginTransaction();
tx.commit() 提交事务
tx.rollback() 回滚事务
5、增
Customer c = new Customer();
c.setCust_name("ICETECH");
session.save(c);
6、查
通过id
Customer customer = (Customer) session.get(Customer.class, 1l);
System.out.println(customer);
7、改 先查到转成对象 set属性 在updata
Customer customer = (Customer) session.get(Customer.class, 1l);
customer.setCust_name("Stevezong");
session.update(customer);
8、删 先查到
Customer customer = (Customer) session.get(Customer.class, 1l);
session.delete(customer);
hibernate中的实体规则
实体类创建的注意事项
1.持久化类提供无参数构造
2.成员变量私有,提供共有get/set方法访问.需提供属性
3.持久化类中的属性,应尽量使用包装类型
4.持久化类需要提供oid.与数据库中的主键列对应 ***********
5.不要用final修饰class
hibernate使用cglib代理生成代理对象.代理对象是继承被代理对象.如果被final修饰.将无法生成代理.
主键类型
自然主键(少见)
表的业务列中,有某业务列符合,必须有,并且不重复的特征时,该列可以作为主键使用.
代理主键(常见)
表的业务列中,没有某业务列符合,必须有,并且不重复的特征时,创建一个没有业务意义的列作为主键
主键生成策略
代理主键
identity : 主键自增.由数据库来维护主键值.录入时不需要指定主键.
sequence: Oracle中的主键生成策略.
increment(了解): 主键自增.由hibernate来维护.每次插入前会先查询表中id最大值.+1作为新主键值.
hilo(了解): 高低位算法.主键自增.由hibernate来维护.开发时不使用.
native:hilo+sequence+identity 自动三选一策略.
uuid: 产生随机字符串作为主键. 主键类型必须为string 类型.
自然主键
assigned:自然主键生成策略. hibernate不会管理主键值.由开发人员自己录入.
hibernate中的对象状态
对象分为三种状态
瞬时状态
没有id,没有在session缓存中
持久化状态
有id,在session缓存中
游离|托管状态
有id,没有在session缓存中
三种状态的转换图
hibernate进阶-一级缓存
缓存:提高效率.hibernate中的一级缓存也是为了提高操作数据库的效率.
提高效率手段1:提高查询效率
缓存
提高效率手段2:减少不必要的修改语句发送
快照
hibernate中的事务
事务
事务特性
a 原子性
c 一致性
i 隔离性
d 持久性
事务并发问题
1.脏读
2.不可重复度
3.幻|虚读
事务的隔离级别
读未提交- 123
读已提交 - 23
可重复读(mysql默认级别)-3
串行化 - 没有问题
知识点:如何在hibernate中指定数据库的隔离级别
## specify a JDBC isolation level
#hibernate.connection.isolation 4
1,2,4,8
0001 1 读未提交
0010 2 读已提交
0100 4 可重复读
1000 8 串行化
知识点2:在项目中如何管理事务
业务开始之前打开事务,业务执行之后提交事务. 执行过程中出现异常.回滚事务.
在dao层操作数据库需要用到session对象.在service控制事务也是使用session对象完成. 我们要确保dao层和service层使用的使用同一个session对象
在hibernate中,确保使用同一个session的问题,hibernate已经帮我们解决了. 我们开发人员只需要调用sf.getCurrentSession()方法即可获得与当前线程绑定的session对象
注意1: 调用getCurrentSession方法必须配合主配置中的一段配置
注意2:通过getCurrentSession方法获得的session对象.当事务提交时,session会自动关闭.不要手动调用close关闭.
crm项目中
Service层
public class CustomerServiceImpl implements CustomerService {
public void save(Customer customer) {
CustomerDao dao = new CustomerDaoImpl();
//在servier 层打开事物
Transaction transaction = HibernateUtils.getCurrentSession().beginTransaction();
try {
dao.save(customer);
} catch (Exception e) {
e.printStackTrace();
//回滚事物
transaction.rollback();
}
//提交事物
transaction.commit();
}
}
Dao层
public class CustomerDaoImpl implements CustomerDao {
public void save(Customer customer) {
//获得session
Session session = HibernateUtils.getCurrentSession();
//保存
session.save(customer);
}
}
hibernate中的批量查询(概述)
HQL查询-hibernate Query Language(多表查询,但不复杂时使用)
Hibernate独家查询语言,属于面向对象的查询语言
String hql = "select * from 对象的完整类名";
基本查询
@Test
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
List
System.out.println(customers);
//Customer customer = (Customer) query.uniqueResult();
tx.commit();
session.close();
}
@Test
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer where cust_id=2";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
//List
//System.out.println(customers);
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
tx.commit();
session.close();
}
@Test
条件查询
?号占位符
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer where cust_id=?";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//设置参数 index 从0开始 jdbc是从1(注意)
query.setLong(0, 2L);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
//List
//System.out.println(customers);
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
tx.commit();
session.close();
}
@Test
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer where cust_id=?";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//设置参数 index 从0开始 jdbc是从1(注意)
//query.setLong(0, 2L);
// 万能 不需要制定类型 自动转换
query.setParameter(0, 2L);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
//List
//System.out.println(customers);
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
tx.commit();
session.close();
}
命名占位符
@Test
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer where cust_id= :id";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//设置参数 index 从0开始 jdbc是从1(注意)
//query.setLong(0, 2L);
// 万能 不需要制定类型 自动转换
//query.setParameter(0, 2L);
//命名占位符 :名字
query.setParameter("id", 2l);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
//List
//System.out.println(customers);
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer);
tx.commit();
session.close();
}
分页查询
@Test
public void t4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//输入HQL语句
String hql = "from com.stevezong.domain.Customer ";
//根据HQL语句创建查询对象
Query query = session.createQuery(hql);
//设置参数 index 从0开始 jdbc是从1(注意)
//query.setLong(0, 2L);
// 万能 不需要制定类型 自动转换
//query.setParameter(0, 2L);
//命名占位符 :名字
//query.setParameter("id", 2l);
//起始位置 和limit一致
query.setFirstResult(0);
//查询多少
query.setMaxResults(2);
//根据查询对象获得查询结果 list 是多个 uniqueResult 单个
List
System.out.println(customers);
//Customer customer = (Customer) query.uniqueResult();
//System.out.println(customer);
tx.commit();
session.close();
}
Criteria查询(单表条件查询)
Hibernate自创的无语句面向对象查询
基本查询
@Test
public void t5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//查询所有的customer对象
Criteria criteria = session.createCriteria(Customer.class);
//结果为多个
List
//结果为1个
Customer Customer = (Customer) criteria.uniqueResult();
tx.commit();
session.close();
}
条件查询
> gt
>= ge
< lt
<= le
== eq
!= ne
in in
between and between
like like
is not null isNotNull
is null isNull
or or
and and
@Test
public void t5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//查询所有的customer对象
Criteria criteria = session.createCriteria(Customer.class);
// 添加查询参数 查询cuts_id 为1 的
criteria.add(Restrictions.eq("cust_id", 2l));
//结果为多个
//List
//结果为1个
Customer customer = (Customer) criteria.uniqueResult();
System.out.println(customer);
tx.commit();
session.close();
}
分页 查询
@Test
public void t5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//查询所有的customer对象
Criteria criteria = session.createCriteria(Customer.class);
// 添加查询参数 查询cuts_id 为1 的
//criteria.add(Restrictions.eq("cust_id", 2l));
//分页查询
criteria.setFirstResult(0);
criteria.setMaxResults(2);
//结果为多个
List
System.out.println(criterias);
//结果为1个
//Customer customer = (Customer) criteria.uniqueResult();
//System.out.println(customer);
tx.commit();
session.close();
}
设置查询总记录数(聚合函数)
@Test
public void t5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//查询所有的customer对象
Criteria criteria = session.createCriteria(Customer.class);
//设置查询的聚合函数 》》 总行数
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();
System.out.println(count);
// 添加查询参数 查询cuts_id 为1 的
//criteria.add(Restrictions.eq("cust_id", 2l));
//分页查询
//criteria.setFirstResult(0);
//criteria.setMaxResults(2);
//结果为多个
//List
//System.out.println(criterias);
//结果为1个
//Customer customer = (Customer) criteria.uniqueResult();
//System.out.println(customer);
tx.commit();
session.close();
}
原生SQL查询(复杂的业务查询)
基本查询
返回数组List
@Test
public void t6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String sql = "SELECT * FROM cst_customer";
SQLQuery query = session.createSQLQuery(sql);
List
for(Object[] sub: list) {
System.out.println(Arrays.toString(sub));
}
tx.commit();
session.close();
}
返回对象List
@Test
public void t6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String sql = "SELECT * FROM cst_customer";
SQLQuery query = session.createSQLQuery(sql);
//制定将结果集封装到那个对象中
query.addEntity(Customer.class);
List
for(Customer sub: list) {
System.out.println(sub);
}
tx.commit();
session.close();
}
条件查询
@Test
public void t6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String sql = "SELECT * FROM cst_customer WHERE cust_id=?";
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 2L);
//制定将结果集封装到那个对象中
query.addEntity(Customer.class);
List
for(Customer sub: list) {
System.out.println(sub);
}
tx.commit();
session.close();
}
分页查询
一对多|多对一
关系表达
表中的表达
表中的表达
实体中的表达
orm元数据中表达
一对多
多对一
操作
@Test
public void t7() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("ICETECH");
LinkMan l1 = new LinkMan();
LinkMan l2 = new LinkMan();
l1.setLkm_name("z");
l2.setLkm_name("c");
c.getLinkMens().add(l1);
c.getLinkMens().add(l2);
l1.setCustomer(c);
l2.setCustomer(c);
session.save(c);
session.save(l1);
session.save(l2);
tx.commit();
session.close();
}
@Test
//增加
public void t8() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer) session.get(Customer.class, 1l);
LinkMan lm = new LinkMan();
lm.setLkm_name("w");
lm.setCustomer(c);
c.getLinkMens().add(lm);
session.save(lm);
tx.commit();
session.close();
}
@Test
//删除
public void t9() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = (Customer) session.get(Customer.class, 1l);
LinkMan lm = (LinkMan) session.get(LinkMan.class, 3l);
c.getLinkMens().remove(lm);
lm.setCustomer(null);
tx.commit();
session.close();
}
操作关联属性
进阶操作
级联操作
在 映射文件的
结论: 简化操作.一定要用,save-update,不建议使用delete.
关系维护
inverse属性
性能优化 提供关系维护的性能
无论如何放弃,总有一放必须要维护关系
多的一方不能放弃维护关系的 外键字段就在多的一方
在保存时.两方都会维护外键关系.关系维护两次,冗余了.
多余的维护关系语句,显然是客户这一端在维护关系
多对多
关系表达
表中
对象中
orm元数据
操作
操作关联属性
@Test
//增
public void t11() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
User user = (User) session.get(User.class, 2l);
Role r =new Role();
r.setRole_name("CEO");
user.getRoles().add(r);
session.save(r);
tx.commit();
session.close();
}
@Test
//删
public void t12() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
User user = (User) session.get(User.class, 2l);
Role r =(Role) session.get(Role.class, 3l);
r.setRole_name("CEO");
user.getRoles().remove(r);
tx.commit();
session.close();
}
操作进阶
inverse属性
级联属性
HQL 操作
@Test
//排序
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//排序
String sql = "from Customer order by cust_id desc";
Query query = session.createQuery(sql);
List
System.out.println(list.size());
System.out.println(list);
tx.commit();
session.close();
}
@Test
//条件
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//排序
//String sql = "from Customer order by cust_id desc";
//条件1
//String sql = "from Customer where cust_id=?";
String sql = "from Customer where cust_id= :id";
Query query = session.createQuery(sql);
//条件1
//query.setParameter(0, 6L);
query.setParameter("id", 6L);
List
System.out.println(list.size());
System.out.println(list);
tx.commit();
session.close();
}
@Test
//分页
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//排序
//String sql = "from Customer order by cust_id desc";
//条件1
//String sql = "from Customer where cust_id=?";
//条件2
//String sql = "from Customer where cust_id= :id";
//分页
String sql = "from Customer";
Query query = session.createQuery(sql);
//条件1
//query.setParameter(0, 6L);
//条件2
//query.setParameter("id", 6L);
//分页
query.setFirstResult(0);
query.setMaxResults(2);
List
System.out.println(list.size());
System.out.println(list);
tx.commit();
session.close();
}
@Test
//聚合函数
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//排序
//String sql = "from Customer order by cust_id desc";
//条件1
//String sql = "from Customer where cust_id=?";
//条件2
//String sql = "from Customer where cust_id= :id";
//分页
//String sql = "from Customer";
//统计
//String sql ="select count(*) from Customer";
//String sql ="select max(cust_id) from Customer";
//String sql ="select sum(cust_id) from Customer";
//String sql ="select avg(cust_id) from Customer";
String sql ="select min(cust_id) from Customer";
Query query = session.createQuery(sql);
//条件1
//query.setParameter(0, 6L);
//条件2
//query.setParameter("id", 6L);
//分页
//query.setFirstResult(0);
//query.setMaxResults(2);
//统计
Number number = (Number) query.uniqueResult();
System.out.println(number);
//List
//System.out.println(list.size());
//System.out.println(list);
tx.commit();
session.close();
}
@Test
//投影查询
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//投影查询 就是查询属性
String sql ="select cust_name from Customer";
Query query = session.createQuery(sql);
List
System.out.println(list);
tx.commit();
session.close();
}
@Test
//投影查询
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//投影查询 就是查询属性
//String sql ="select cust_name from Customer";
String sql ="select cust_id,cust_name from Customer";
Query query = session.createQuery(sql);
List
System.out.println(list);
tx.commit();
session.close();
}
@Test
//投影查询 Customer要有对应的构造器
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//投影查询 就是查询属性
//String sql ="select cust_name from Customer";
//String sql ="select cust_id,cust_name from Customer";
String sql ="select new Customer(cust_id,cust_name) from Customer";
Query query = session.createQuery(sql);
List
System.out.println(list);
tx.commit();
session.close();
}
public Customer(Long cust_id, String cust_name) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
}
多表查询
@Test
//连接查询
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//投影查询 就是查询属性
String sql =" from Customer c inner join c.linkMens";
Query query = session.createQuery(sql);
List
for(Object[] sub:list) {
System.out.println(Arrays.toString(sub));
}
tx.commit();
session.close();
}
@Test
//连接查询
public void t13() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//String sql =" from Customer c inner join c.linkMens";
//迫切内连接
//迫切就是将内容放到一起
String sql =" from Customer c inner join fetch c.linkMens";
Query query = session.createQuery(sql);
List
for(Customer sub:list) {
System.out.println(sub);
}
tx.commit();
session.close();
}
QBC 查询
@Test
//离线查询
public void t13() {
//web层 组装 sql 将dc 一路向后传到DAO
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
dc.add(Restrictions.idEq(6L));
//++++++++++++++++++++++++++++++++++
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria c = dc.getExecutableCriteria(session);
List list = c.list();
System.out.println(list);
tx.commit();
session.close();
}
查询优化
类级别查询
get方法:没有任何策略.调用即立即查询数据库加载数据.
load方法: 应用类级别的加载策略
lazy(默认值):true, 查询类时,会返回代理对象.会在使用属性时,根据关联的session查询数据库.加载数据.
lazy:false. load方法会与get方法没有任何区别.调用时即加载数据.
结论:为了提高效率.建议使用延迟加载(懒加载)
注意:使用懒加载时要确保,调用属性加载数据时,session还是打开的.不然会抛出异常
关联级别查询
集合策略
关联属性策略
结论:为了提高效率.fetch的选择上应选择select. lazy的取值应选择 true. 全部使用默认值.
no-session问题解决: 扩大session的作用范围.
批量抓取