标签:sele rgb use lines 返回 fetch get test word
<?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">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名,在元素内部凡是需要书写完整类名的属性,可以直接输入类名 -->
<hibernate-mapping package="hibernate_day01.domain">
<!--
class元素:配置实体与表的对应关系
name:完整类名
table:数据库表名
-->
<class name="Customer" table="cst_customer">
<!--
id元素:配置主键映射的属性
name:填写主键对应属性名
column:填下表中的主键列名
-->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- property元素:除id以外的普通属性映射
name:填写属性名
column:填写列名,默认是name的属性值
not-null:配置该属性是否不能为空,默认值为false
length:配置数据库中列的长度,默认值:使用数据库类型的最大长度
-->
<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_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名,在元素内部凡是需要书写完整类名的属性,可以直接输入类名 -->
<hibernate-mapping package="hibernate_day01.domain">
<!--
class元素:配置实体与表的对应关系
name:完整类名
table:数据库表名
-->
<class name="Customer" table="cst_customer">
<!--
id元素:配置主键映射的属性
name:填写主键对应属性名
column:填下表中的主键列名
-->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- property元素:除id以外的普通属性映射
name:填写属性名
column:填写列名,默认是name的属性值
not-null:配置该属性是否不能为空,默认值为false
length:配置数据库中列的长度,默认值:使用数据库类型的最大长度
-->
<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_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password -->
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库名 -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">123</property>
<!-- 数据库方言
不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成
sql99标准:DDL 定义语言 库表的增删改查
DCL 控制语言 事务权限
DML 操纵语言 增删改查
注意:mysql选择方言时,选择最短的数据库方言
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- #hibernate.show_sql true
#hibernate.format_sql true -->
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hibernate.format_sql">true</property>
<!-- auto schema export 自动导出表结构 自动建表 -->
<!--
#hibernate.hbm2ddl.auto create-drop 自动建表 每次框架运行结束都会删除所有表
#hibernate.hbm2ddl.auto create 自动建表 每次框架运行都会创建新的表,以前表将会被覆盖,数据会丢失
#hibernate.hbm2ddl.auto update(推荐使用) 自动生成表,如果已存在不会再生成,如果表有改动,则执行改动
#hibernate.hbm2ddl.auto validate 校验,不自动生成,每次启动会校验数据库表是否正确。校验失败-->
<property name="hibernate.hbm2ddl.auto"></property>
<!-- 引入orm元数据
路径书写:填写src下的路径
-->
<mapping resource="hibernate_day01/domain/Customer.hbm.xml"/>
<!-- 指定hibernate操作数据库时的隔离级别
#hibernate.connection.isolation 1|2|4|8
0001 1 读未提交
0010 2 读已提交
0100 4 可重复读
1000 8 串行化
-->
<property name="hibernate.connection.isolation">4</property>
</session-factory>
</hibernate-configuration>
<hibernate-configuration>
<session-factory>
<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password -->
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库名 -->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">123</property>
<!-- 数据库方言
不同的数据库中,sql语法略有区别,指定方言可以让hibernate框架在生成sql语句时,针对数据库的方言生成
sql99标准:DDL 定义语言 库表的增删改查
DCL 控制语言 事务权限
DML 操纵语言 增删改查
注意:mysql选择方言时,选择最短的数据库方言
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- #hibernate.show_sql true
#hibernate.format_sql true -->
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hibernate.format_sql">true</property>
<!-- auto schema export 自动导出表结构 自动建表 -->
<!--
#hibernate.hbm2ddl.auto create-drop 自动建表 每次框架运行结束都会删除所有表
#hibernate.hbm2ddl.auto create 自动建表 每次框架运行都会创建新的表,以前表将会被覆盖,数据会丢失
#hibernate.hbm2ddl.auto update(推荐使用) 自动生成表,如果已存在不会再生成,如果表有改动,则执行改动
#hibernate.hbm2ddl.auto validate 校验,不自动生成,每次启动会校验数据库表是否正确。校验失败-->
<property name="hibernate.hbm2ddl.auto"></property>
<!-- 引入orm元数据
路径书写:填写src下的路径
-->
<mapping resource="hibernate_day01/domain/Customer.hbm.xml"/>
<!-- 指定hibernate操作数据库时的隔离级别
#hibernate.connection.isolation 1|2|4|8
0001 1 读未提交
0010 2 读已提交
0100 4 可重复读
1000 8 串行化
-->
<property name="hibernate.connection.isolation">4</property>
</session-factory>
</hibernate-configuration>
package hibernate_day01.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import hibernate_day01.domain.Customer;
//学习Configuration对象
//configuration功能:配置加载类,用于加载主配置,orm元数据加载
public class Demo {
@Test
public void fun1() {
//调用空参构造
Configuration conf = new Configuration().configure();
//读取指定主配置文件 空参加载方法,加载src下的“hibernate.cfg.xml”文件
//根据配置信息,创建sessionFactory对象,这个对象的功能是用于创建操作数据库核心对象session对象的工厂
//功能只有创建session对象
//注意:1、sessionFactory负责保存和使用所有配置信息,消耗内存资源非常大
// 2、sessionFactory属于线程安全的对象设计
//结论:保证在web项目中,只创建一个sessionFactory
SessionFactory sf = conf.buildSessionFactory();
//获得session 此处的session表达hibernate框架与数据库之间的连接(会话)
//JDBC年代的connection对象,还可以完成对数据库中数据的增删改查
//session是hibernate操作数据库的核心对象
//打开一个新的session对象
Session session = sf.openSession();
//session获得操作事务的Transaction对象
//开启事务并获得操作事务的tx对象
Transaction tx = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("朱展鸿");
session.save(c);
tx.commit();
session.close();
sf.close();
}
//根据id查询
@Test
public void fun2() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
System.out.println(customer);
tx.commit();
session.close();
sf.close();
}
//session的修改
@Test
public void fun3() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
customer.setCust_name("zzh");
session.update(customer);
tx.commit();
session.close();
sf.close();
}
//对象导航查询
@Test
public void testSelectObject(){
Session session = null;
Transaction transaction = null;
try {
//1.使用SessionFactory创建Session对象
//理解:类似于jdbc的连接数据库
session = HibernateUtils.getSessionObject();
//2.开启事务
transaction = session.beginTransaction();
//3.写具体的crud操作
//查询cid=3的客户,在查询他的所有联系人
//01.查询cid=3的客户
Customer customer = session.get(Customer.class, 3l);
//02.再查询cid=3的客户的所有联系人
//通过客户的联系人属性获取其所有的联系人
Set<LinkMan> linkMans = customer.getSetLinkMan();
for (LinkMan linkMan : linkMans) {
System.out.println(linkMan);
}
//4.提交事务
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
//5.回滚事务
transaction.rollback();
} finally {
//6.关闭资源 在使用了与本地线程绑定的session对象之后,就不需要手动关闭session了
session.close();
}
}
//删除操作
@Test
public void fun4() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
session.delete(customer);
tx.commit();
session.close();
sf.close();
}
}
package hibernate_day01.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import hibernate_day01.domain.Customer;
//学习Configuration对象
//configuration功能:配置加载类,用于加载主配置,orm元数据加载
public class Demo {
public void fun1() {
//调用空参构造
Configuration conf = new Configuration().configure();
//读取指定主配置文件 空参加载方法,加载src下的“hibernate.cfg.xml”文件
//根据配置信息,创建sessionFactory对象,这个对象的功能是用于创建操作数据库核心对象session对象的工厂
//功能只有创建session对象
//注意:1、sessionFactory负责保存和使用所有配置信息,消耗内存资源非常大
// 2、sessionFactory属于线程安全的对象设计
//结论:保证在web项目中,只创建一个sessionFactory
SessionFactory sf = conf.buildSessionFactory();
//获得session 此处的session表达hibernate框架与数据库之间的连接(会话)
//JDBC年代的connection对象,还可以完成对数据库中数据的增删改查
//session是hibernate操作数据库的核心对象
//打开一个新的session对象
Session session = sf.openSession();
//session获得操作事务的Transaction对象
//开启事务并获得操作事务的tx对象
Transaction tx = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("朱展鸿");
session.save(c);
tx.commit();
session.close();
sf.close();
}
//根据id查询
public void fun2() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
System.out.println(customer);
tx.commit();
session.close();
sf.close();
}
//session的修改
public void fun3() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
customer.setCust_name("zzh");
session.update(customer);
tx.commit();
session.close();
sf.close();
}
//对象导航查询
public void testSelectObject(){
Session session = null;
Transaction transaction = null;
try {
//1.使用SessionFactory创建Session对象
//理解:类似于jdbc的连接数据库
session = HibernateUtils.getSessionObject();
//2.开启事务
transaction = session.beginTransaction();
//3.写具体的crud操作
//查询cid=3的客户,在查询他的所有联系人
//01.查询cid=3的客户
Customer customer = session.get(Customer.class, 3l);
//02.再查询cid=3的客户的所有联系人
//通过客户的联系人属性获取其所有的联系人
Set<LinkMan> linkMans = customer.getSetLinkMan();
for (LinkMan linkMan : linkMans) {
System.out.println(linkMan);
}
//4.提交事务
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
//5.回滚事务
transaction.rollback();
} finally {
//6.关闭资源 在使用了与本地线程绑定的session对象之后,就不需要手动关闭session了
session.close();
}
}
//删除操作
public void fun4() {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 3l);
session.delete(customer);
tx.commit();
session.close();
sf.close();
}
}
<!--
id元素:配置主键映射的属性
name:填写主键对应属性名
column:填下表中的主键列名
-->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略
每条记录录入时,主键的生成规则
identity:主键自增,由数据库来维护主键值,录入时不需要指定主键
increment:主键自增,由hibernate来维护,每次插入前会先查询表中id最大值,+1作为新主键值
hilo:高低位算法,主键自增,由hibernate维护
sequence:Oracle中的主键生成策略
native:hilo+sequence+identity自动三选一策略
uuid:产生随机字符串作为主键,主键类型必须为字符串类型
assigned:自然主键生成策略,hibernate不会管理主键值,由开发人员自己录入
-->
<generator class="native"></generator>
</id>
<!--
id元素:配置主键映射的属性
name:填写主键对应属性名
column:填下表中的主键列名
-->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略
每条记录录入时,主键的生成规则
identity:主键自增,由数据库来维护主键值,录入时不需要指定主键
increment:主键自增,由hibernate来维护,每次插入前会先查询表中id最大值,+1作为新主键值
hilo:高低位算法,主键自增,由hibernate维护
sequence:Oracle中的主键生成策略
native:hilo+sequence+identity自动三选一策略
uuid:产生随机字符串作为主键,主键类型必须为字符串类型
assigned:自然主键生成策略,hibernate不会管理主键值,由开发人员自己录入
-->
<generator class="native"></generator>
</id>
public class Demo {
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer();//瞬时状态,没有id,没有与session关联
c.setCust_name("狗东");//瞬时状态
session.save(c);//持久化状态,有id,有关联
tx.commit();
session.close();//游离状态,有id,没关联
}
}
public class Demo {
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = new Customer();//瞬时状态,没有id,没有与session关联
c.setCust_name("狗东");//瞬时状态
session.save(c);//持久化状态,有id,有关联
tx.commit();
session.close();//游离状态,有id,没关联
}
}
<!-- 指定session与当前线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 指定session与当前线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
public class Demo {
//条件查询
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer ";//查询所有Customer对象
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//3、根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
//HQL语句中,不可能出现任何数据库相关的信息
@Test
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = 1 ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//问号占位符
@Test
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = ? ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置参数
query.setParameter(0, 1l);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//命名占位符
@Test
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = :cust_id ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置参数
query.setParameter("cust_id", 1l);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//分页
@Test
public void fun5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置分页信息
query.setFirstResult(1);
query.setMaxResults(2);//一次查询多少条
//3、根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
}
public class Demo {
//条件查询
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer ";//查询所有Customer对象
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//3、根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
//HQL语句中,不可能出现任何数据库相关的信息
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = 1 ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//问号占位符
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = ? ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置参数
query.setParameter(0, 1l);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//命名占位符
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer where cust_id = :cust_id ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置参数
query.setParameter("cust_id", 1l);
//3、根据查询对象获得查询结果
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//分页
public void fun5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//1、书写hql语句
String hql = " from Customer ";//cust_id是属性名,不是列名
//2、根绝hql创建查询对象
Query query = session.createQuery(hql);
//设置分页信息
query.setFirstResult(1);
query.setMaxResults(2);//一次查询多少条
//3、根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
}
//criteria查询
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
System.out.println(list);
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 fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
//添加查询参数 查询cust_id为1的customer对象
criteria.add(Restrictions.eq("cust_id", 1l));
//通过criteria对象获得查询结果
Customer c = (Customer) criteria.uniqueResult();
tx.commit();
session.close();
}
//分页查询
@Test
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setFirstResult(0);
criteria.setMaxResults(1);
List<Customer> list = criteria.list();
System.out.println(list);
tx.commit();
session.close();
}
//查询总记录数
@Test
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
//设置查询的聚合函数 总行数
criteria.setProjection(Projections.rowCount());
//执行查询
Long count = (Long) criteria.uniqueResult();
System.out.println(count);
tx.commit();
session.close();
}
//criteria查询
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
System.out.println(list);
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
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
//添加查询参数 查询cust_id为1的customer对象
criteria.add(Restrictions.eq("cust_id", 1l));
//通过criteria对象获得查询结果
Customer c = (Customer) criteria.uniqueResult();
tx.commit();
session.close();
}
//分页查询
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setFirstResult(0);
criteria.setMaxResults(1);
List<Customer> list = criteria.list();
System.out.println(list);
tx.commit();
session.close();
}
//查询总记录数
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
//设置查询的聚合函数 总行数
criteria.setProjection(Projections.rowCount());
//执行查询
Long count = (Long) criteria.uniqueResult();
System.out.println(count);
tx.commit();
session.close();
}
//原声mysql查询
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
@Test
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer where cust_id = ?";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 1l);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//分页查询
@Test
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer limit ?,?";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 0);
query.setParameter(1, 1);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//原声mysql查询
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer where cust_id = ?";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 1l);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//分页查询
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//书写sql语句
String sql = "select * from cst_customer limit ?,?";
//创建sql查询对象
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, 0);
query.setParameter(1, 1);
//指定将结果封装到哪个对象中
query.addEntity(Customer.class);
//调用方法查询结果
/*List<Object[]> list = query.list();
for(Object[] obj:list) {
System.out.println(Arrays.toString(obj));
}*/
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
<!-- 集合,一对多关系,在配置文件中的配置 -->
<!--
name属性:集合属性名
column属性:外键列名
class属性:与我关联的对象的完整包名
-->
<!--
级联操作:cascade
save-update:级联保存更新
dalete:级联删除(不建议使用,误操作后果严重)
all :save-update和delete同时配
级联操作:简化操作,简化代码
-->
<!-- inverse属性
true:customer不维护关系
false(默认值):customer维护关系
inverse属性:性能优化,提高关系维护的性能
原则:无论怎么放弃,总有一方必须要维护关系
一对多关系中:一的一方放弃,也只能是一的一方放弃,多的一方不能放弃
-->
<set name="LinkMen" cascade="sava-update" inverse="true">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan" />
</set>
<!-- 集合,一对多关系,在配置文件中的配置 -->
<!--
name属性:集合属性名
column属性:外键列名
class属性:与我关联的对象的完整包名
-->
<!--
级联操作:cascade
save-update:级联保存更新
dalete:级联删除(不建议使用,误操作后果严重)
all :save-update和delete同时配
级联操作:简化操作,简化代码
-->
<!-- inverse属性
true:customer不维护关系
false(默认值):customer维护关系
inverse属性:性能优化,提高关系维护的性能
原则:无论怎么放弃,总有一方必须要维护关系
一对多关系中:一的一方放弃,也只能是一的一方放弃,多的一方不能放弃
-->
<set name="LinkMen" cascade="sava-update" inverse="true">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan" />
</set>
<!-- 集合,多对一,在配置文件中的配置 -->
<!--
name属性:集合属性名
column属性:外键列名
class属性:与我关联的对象的完整包名
-->
<!-- 多的一方:不能放弃维护关系,外键字段在多的一方 -->
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
<!-- 集合,多对一,在配置文件中的配置 -->
<!--
name属性:集合属性名
column属性:外键列名
class属性:与我关联的对象的完整包名
-->
<!-- 多的一方:不能放弃维护关系,外键字段在多的一方 -->
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
public class Demo {
@Test
public void fun1() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
Customer c = new Customer();
c.setCust_name("狗东");
LinkMan lm1 = new LinkMan();
lm1.setLkm_name("刘强东");
LinkMan lm2 = new LinkMan();
lm2.setLkm_name("奶茶妹");
//表示一对多,客户下有多个联系人
c.getLinkMen().add(lm1);
c.getLinkMen().add(lm2);
//表示多对一,多个联系人对应一个客户,如果客户放弃维护关系,则以下两行代码可省略
lm1.setCustomer(c);
lm2.setCustomer(c);
//将对象转化为持久化状态
//session.save(lm1);
//session.save(lm2);
session.save(c);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
//添加联系人
@Test
public void fun2() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
//获得需要操作的客户对象
Customer c = session.get(Customer.class, 4l);
//创建新的联系人
LinkMan lm1 = new LinkMan();
lm1.setLkm_name("马云");
//将新的联系人添加到客户当中
c.getLinkMen().add(lm1);
lm1.setCustomer(c);
session.save(lm1);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
//移除联系人
@Test
public void fun3() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
//获得需要操作的客户对象
Customer c = session.get(Customer.class, 4l);
//获得要移除的联系人对象
LinkMan lm = session.get(LinkMan.class, 3l);
c.getLinkMen().remove(lm);
lm.setCustomer(null);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
}
public class Demo {
public void fun1() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
Customer c = new Customer();
c.setCust_name("狗东");
LinkMan lm1 = new LinkMan();
lm1.setLkm_name("刘强东");
LinkMan lm2 = new LinkMan();
lm2.setLkm_name("奶茶妹");
//表示一对多,客户下有多个联系人
c.getLinkMen().add(lm1);
c.getLinkMen().add(lm2);
//表示多对一,多个联系人对应一个客户,如果客户放弃维护关系,则以下两行代码可省略
lm1.setCustomer(c);
lm2.setCustomer(c);
//将对象转化为持久化状态
//session.save(lm1);
//session.save(lm2);
session.save(c);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
//添加联系人
public void fun2() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
//获得需要操作的客户对象
Customer c = session.get(Customer.class, 4l);
//创建新的联系人
LinkMan lm1 = new LinkMan();
lm1.setLkm_name("马云");
//将新的联系人添加到客户当中
c.getLinkMen().add(lm1);
lm1.setCustomer(c);
session.save(lm1);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
//移除联系人
public void fun3() {
//1、获得session
Session session = HibernateUtils.openSession();
//2、开启事务
Transaction tx = session.beginTransaction();
//3、操作
//获得需要操作的客户对象
Customer c = session.get(Customer.class, 4l);
//获得要移除的联系人对象
LinkMan lm = session.get(LinkMan.class, 3l);
c.getLinkMen().remove(lm);
lm.setCustomer(null);
//4、提交事务
tx.commit();
//5、关闭资源
session.close();
}
}
<!-- 多对多表达式 -->
<!--
name:集合属性名
table:配置中间表名
key
|column:外键名,别人引用我的外键列名,此类对应表的外键列名
class:this类与哪个类是多对多关系
column:与this类有多对多关系的类的外键列名
-->
<set name="roles" table="sys_user_role">
<key column="user_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
<!-- 多对多表达式 -->
<!--
name:集合属性名
table:配置中间表名
key
|column:外键名,别人引用我的外键列名,此类对应表的外键列名
class:this类与哪个类是多对多关系
column:与this类有多对多关系的类的外键列名
-->
<set name="roles" table="sys_user_role">
<key column="user_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
<!-- 使用inverse属性
true:放弃维护外键关系
结论:将来在开发中,如果遇到多对多关系,一定要选择一方放弃维护关系
一般谁来放弃维护关系要看业务方向,例如录入员工时,需要为员工指定所属角色,
那么业务方向就是由员工维护角色,角色不需要维护与员工的关系,角色放弃维护
-->
<set name="users" table="sys_user_role" inverse="true">
<key column="role_id"></key>
<many-to-many class="User" column="user_id"></many-to-many>
</set>
<!-- 使用inverse属性
true:放弃维护外键关系
结论:将来在开发中,如果遇到多对多关系,一定要选择一方放弃维护关系
一般谁来放弃维护关系要看业务方向,例如录入员工时,需要为员工指定所属角色,
那么业务方向就是由员工维护角色,角色不需要维护与员工的关系,角色放弃维护
-->
<set name="users" table="sys_user_role" inverse="true">
<key column="role_id"></key>
<many-to-many class="User" column="user_id"></many-to-many>
</set>
package com.zzh.hibernate.hql;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.zzh.hibernate.domain.Customer;
import com.zzh.hibernate.utils.HibernateUtils;
public class Demo {
//基本查询
@Test
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer ";
String hql2 = " from com.zzh.hibernate.domain.Customer";//完整写法
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//顺序查询
@Test
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer order by cust_id desc";
String hql2 = " from com.zzh.hibernate.domain.Customer";//完整写法
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
@Test
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer where cust_id = ? ";
String hql2 = " from Customer where cust_id = :id ";//完整写法
Query query = session.createQuery(hql);
query.setParameter(0, 1l);
//query.setParameter("id", 2l);
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//分页查询
@Test
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(2);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//统计查询 count sum avg max min
@Test
public void fun5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " select count(*) from Customer";
String hql2 = " select sum(cust_id) from Customer";
String hql3 = " select avg(cust_id) from Customer";
String hql4 = " select max(cust_id) from Customer";
String hql5 = " select min(cust_id) from Customer";
Query query = session.createQuery(hql3);
Number number = (Number) query.uniqueResult();
System.out.println(number);
tx.commit();
session.close();
}
//投影查询
@Test
public void fun6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " select cust_name from Customer";
String hql1 = " select cust_name,cust_id from Customer";
String hql2 = " select new Customer(cust_id,cust_name) from Customer";
Query query = session.createQuery(hql2);
List<Object[]> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
}
package com.zzh.hibernate.hql;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.zzh.hibernate.domain.Customer;
import com.zzh.hibernate.utils.HibernateUtils;
public class Demo {
//基本查询
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer ";
String hql2 = " from com.zzh.hibernate.domain.Customer";//完整写法
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//顺序查询
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer order by cust_id desc";
String hql2 = " from com.zzh.hibernate.domain.Customer";//完整写法
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//条件查询
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer where cust_id = ? ";
String hql2 = " from Customer where cust_id = :id ";//完整写法
Query query = session.createQuery(hql);
query.setParameter(0, 1l);
//query.setParameter("id", 2l);
Customer c = (Customer) query.uniqueResult();
System.out.println(c);
tx.commit();
session.close();
}
//分页查询
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(2);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
//统计查询 count sum avg max min
public void fun5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " select count(*) from Customer";
String hql2 = " select sum(cust_id) from Customer";
String hql3 = " select avg(cust_id) from Customer";
String hql4 = " select max(cust_id) from Customer";
String hql5 = " select min(cust_id) from Customer";
Query query = session.createQuery(hql3);
Number number = (Number) query.uniqueResult();
System.out.println(number);
tx.commit();
session.close();
}
//投影查询
public void fun6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " select cust_name from Customer";
String hql1 = " select cust_name,cust_id from Customer";
String hql2 = " select new Customer(cust_id,cust_name) from Customer";
Query query = session.createQuery(hql2);
List<Object[]> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
}
//HQL的多表查询
public class Demo2 {
//回顾-原生SQL
//交叉连接-笛卡儿积(避免)
// select * from A,B
//内连接
// |-隐式内连接
// select * from A,B where b.aid = a.id
// |-显式外连接
// select * from A inner join B on b.aid = a.id
//外连接
// |-左外
// select * from A left [outer] join B on b.aid = a.id
// |-右外
// select * from A right [outer] join B on b.aid = a.id
//-------------------------------------------------------------------
//HQL的多表查询
//内连接(迫切)
//外连接
// |-左外(迫切)
// |-右外(迫切)
//基本语法
@Test
//HQL内链接=>将连接两端的对象分别返回,封装到一个数组中
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c inner join c.linkMen ";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for(Object[] obj :list) {
System.out.println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
@Test
//HQL迫切内链接=>将连接的两端对象封装到一个对象中
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c inner join fetch c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
@Test
//HQL左外连接
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c left join c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
@Test
//HQL右外连接
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c right join c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
//HQL的多表查询
public class Demo2 {
//回顾-原生SQL
//交叉连接-笛卡儿积(避免)
// select * from A,B
//内连接
// |-隐式内连接
// select * from A,B where b.aid = a.id
// |-显式外连接
// select * from A inner join B on b.aid = a.id
//外连接
// |-左外
// select * from A left [outer] join B on b.aid = a.id
// |-右外
// select * from A right [outer] join B on b.aid = a.id
//-------------------------------------------------------------------
//HQL的多表查询
//内连接(迫切)
//外连接
// |-左外(迫切)
// |-右外(迫切)
//基本语法
//HQL内链接=>将连接两端的对象分别返回,封装到一个数组中
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c inner join c.linkMen ";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for(Object[] obj :list) {
System.out.println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
//HQL迫切内链接=>将连接的两端对象封装到一个对象中
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c inner join fetch c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
//HQL左外连接
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c left join c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
//HQL右外连接
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer c right join c.linkMen ";
Query query = session.createQuery(hql);
List<Customer> c = query.list();
System.out.println(c);
tx.commit();
session.close();
}
/ 离线Criteria查询
public class Demo2 {
@Test
public void fun1() {
//----------------------------------------------------
// 假设这里是Web层或Service层的操作
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
// 注意这里是用DetachedCriteria类的静态方法forClass创建离线Criteria对象
detachedCriteria.add(Restrictions.idEq(13l));
// 传递到下面就是Dao层的操作
//----------------------------------------------------
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 使离线Criteria与Session关联(上线)获取可执行Criteria对象
List<Customer> list = criteria.list();
System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close();
}
}
/ 离线Criteria查询
public class Demo2 {
public void fun1() {
//----------------------------------------------------
// 假设这里是Web层或Service层的操作
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
// 注意这里是用DetachedCriteria类的静态方法forClass创建离线Criteria对象
detachedCriteria.add(Restrictions.idEq(13l));
// 传递到下面就是Dao层的操作
//----------------------------------------------------
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
Criteria criteria = detachedCriteria.getExecutableCriteria(session);// 使离线Criteria与Session关联(上线)获取可执行Criteria对象
List<Customer> list = criteria.list();
System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close();
}
}
//懒加载|延迟加载
public class Demo {
@Test
//get方法:立即加载,执行方法时立刻发送sql语句查询结果
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
System.out.println(c);
tx.commit();
session.close();
}
@Test
//load方法:是在执行时,不发送任何sql语句,返回一个对象,使用该对象时,才执行查询
//延迟加载:仅仅获得没有使用,不会查询,在使用时才进行查询
//是否对类进行延迟加载:可以通过在class元素上配置lazy属性来控制
//lazy="true" :加载时,不查询,使用时才查询
//lazy="false" :加载时立即查询
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.load(Customer.class, 3l);
System.out.println(c);
tx.commit();
session.close();
}
}
//懒加载|延迟加载
public class Demo {
//get方法:立即加载,执行方法时立刻发送sql语句查询结果
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
System.out.println(c);
tx.commit();
session.close();
}
//load方法:是在执行时,不发送任何sql语句,返回一个对象,使用该对象时,才执行查询
//延迟加载:仅仅获得没有使用,不会查询,在使用时才进行查询
//是否对类进行延迟加载:可以通过在class元素上配置lazy属性来控制
//lazy="true" :加载时,不查询,使用时才查询
//lazy="false" :加载时立即查询
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.load(Customer.class, 3l);
System.out.println(c);
tx.commit();
session.close();
}
}
<!--
lazy属性:决定是否延迟加载
true:延迟加载
false:立即加载
extra:继续懒惰
fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
select(默认值):单表查询加载
join:使用多表查询加载集合
subselect:使用子查询加载集合
bacth-size="3":抓取集合的数量为3
抓取客户的集合时,一次抓取三个客户
-->
<set name="linkMen" lazy="true" fetch="select" bacth-size="3">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan" />
</set>
<!--
lazy属性:决定是否延迟加载
true:延迟加载
false:立即加载
extra:继续懒惰
fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
select(默认值):单表查询加载
join:使用多表查询加载集合
subselect:使用子查询加载集合
bacth-size="3":抓取集合的数量为3
抓取客户的集合时,一次抓取三个客户
-->
<set name="linkMen" lazy="true" fetch="select" bacth-size="3">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan" />
</set>
@Test
//集合级别的关联
//fetch:select 单表查询
//lazy:true 使用时才加载集合数据
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
@Test
//集合级别的关联
//fetch:select 单表查询
//lazy:false 立即加载集合数据
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
@Test
//集合级别的关联
//fetch:select 单表查询
//lazy:extra 极其懒惰,与懒加载效果基本一致,如果只获得集合的size,则只查询集合的size(count语句)
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
@Test
//集合级别的关联
//fetch:join 多表查询
//lazy:true|false|extra 失效,立刻加载
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen.size());
System.out.println(linkMen);
tx.commit();
session.close();
}
@Test
//fetch:subselect 子查询
//lazy:true 使用时才加载集合数据
public void fun6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer ";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
for(Customer c:list) {
System.out.println(c);
System.out.println(c.getLinkMen().size());
System.out.println(c.getLinkMen());
}
tx.commit();
session.close();
}
//集合级别的关联
//fetch:select 单表查询
//lazy:true 使用时才加载集合数据
public void fun1() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
//集合级别的关联
//fetch:select 单表查询
//lazy:false 立即加载集合数据
public void fun2() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
//集合级别的关联
//fetch:select 单表查询
//lazy:extra 极其懒惰,与懒加载效果基本一致,如果只获得集合的size,则只查询集合的size(count语句)
public void fun3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen);
tx.commit();
session.close();
}
//集合级别的关联
//fetch:join 多表查询
//lazy:true|false|extra 失效,立刻加载
public void fun4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get(Customer.class, 1l);
Set<LinkMan> linkMen = c.getLinkMen();
System.out.println(linkMen.size());
System.out.println(linkMen);
tx.commit();
session.close();
}
//fetch:subselect 子查询
//lazy:true 使用时才加载集合数据
public void fun6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer ";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
for(Customer c:list) {
System.out.println(c);
System.out.println(c.getLinkMen().size());
System.out.println(c.getLinkMen());
}
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.zella.domain">
<!--配置表与实体(java实体对象)之间的关系-->
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"/>
</id>
<property name="lkm_name"/>
<property name="lkm_gender"/>
<property name="lkm_phone"/>
<property name="lkm_mobile"/>
<property name="lkm_email"/>
<property name="lkm_qq"/>
<property name="lkm_position"/>
<property name="lkm_memo"/>
<!--
fetch 决定加载的sql语句
select: 使用单表查询
join : 多表查询
lazy 决定加载时机
false: 立即加载
proxy: 由customer的类级别加载策略决定.
-->
<many-to-one name="customer" column="lkm_cust_id" class="Customer" lazy="false" fetch="select"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.zella.domain">
<!--配置表与实体(java实体对象)之间的关系-->
<class name="LinkMan" table="cst_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"/>
</id>
<property name="lkm_name"/>
<property name="lkm_gender"/>
<property name="lkm_phone"/>
<property name="lkm_mobile"/>
<property name="lkm_email"/>
<property name="lkm_qq"/>
<property name="lkm_position"/>
<property name="lkm_memo"/>
<!--
fetch 决定加载的sql语句
select: 使用单表查询
join : 多表查询
lazy 决定加载时机
false: 立即加载
proxy: 由customer的类级别加载策略决定.
-->
<many-to-one name="customer" column="lkm_cust_id" class="Customer" lazy="false" fetch="select"/>
</class>
</hibernate-mapping>
标签:sele rgb use lines 返回 fetch get test word
原文地址:https://www.cnblogs.com/zzh1997/p/9601090.html