当前对象没有持久化标识OID, 没有被Session操作管理
当前对象有持久化标识OID,已经被Session操作管理
持久态对象会自动更新数据库数据(一级缓存)
当前对象有持久化标识OID,没有被纳入到Session操作管理
持久态对象有自动更新数据库的能力
缓存就是一块内存空间,将数据源(数据库或者文件)中的数据存放到缓存中,再次获取的时候,直接从缓存中获取,可以提升程序的性能!
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
public class TestSession {
/**
* @MethodName:test01
* @Description:测试hibernate一级缓存的存在
* @throws Exception
*/
@Test
public void test01() throws Exception {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCustName("jack");
// 保存用户,customer先往数据库中存一份,一级缓存中有一份
Serializable id = session.save(customer);
System.out.println(id);
// 获取对象,不会看到SQL语句,直接去一级缓存中查找
Customer c = session.get(Customer.class, id);
System.out.println(c.getCustName());
tx.commit();
session.close();
}
/**
* @MethodName:test02
* @Description:测试hibernate一级缓存的存在
* @throws Exception
*/
@Test
public void test02() throws Exception {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCustName("jack");
// 查询用户,先去数据库中查,查完放在一级缓存中一份
Customer c1 = session.get(Customer.class, 1l);
System.out.println(c1.getCustName());
// 再去查询用户,不会看到SQL语句,直接去一级缓存中查找
Customer c2 = session.get(Customer.class, 1l);
System.out.println(c2.getCustName());
tx.commit();
session.close();
}
}
import static org.junit.Assert.*;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
public class TestSession {
/**
* @MethodName:test04
* @Description:快照机制
* @throws Exception
*/
@Test
public void test04() throws Exception {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class, 1l);
customer.setCustName("隔壁老王");
tx.commit();
session.close();
}
}
thread
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
public static final Configuration config;
public static final SessionFactory sessionFactory;
static{
config= new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}
//绑定当前线程
public static Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
/**
* @ClassName: TestQuery
* @Description:测试批量查询
* @author jsz
* @date 2018年8月18日
*/
public class TestQuery {
/**
* @MethodName:test01
* @Description:测试query查询接口
* @throws Exception
*/
@Test
public void test01() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// from 实体类名
Query query = session.createQuery("from Customer");
List list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test02
* @Description:条件查询
* @throws Exception
*/
@Test
public void test02() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer where custName =?");
// hibernate的占位符从0开始
query.setString(0, "ccc");
List list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test03
* @Description:模糊查询
* @throws Exception
*/
@Test
public void test03() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer where custName like ?");
// hibernate的占位符从0开始
query.setString(0, "c%");
List list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test04
* @Description:分页查询
* @throws Exception
*/
@Test
public void test04() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
query.setFirstResult(1);// 从第几条开始查
query.setMaxResults(3);// 查几条
List list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test05
* @Description:查询单列数据
* @throws Exception
*/
@Test
public void test05() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select custName from Customer");
List
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.utils.HibernateUtils;
public class TestCriteria {
/**
* @MethodName:test01
* @Description:查询全部
* @throws Exception
*/
@Test
public void test01() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test02
* @Description:条件查询
* @throws Exception
*/
@Test
public void test02() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
/*
* == eq > gt >= ge < lt <= le <> ne in in like like
*
*/
criteria.add(Restrictions.eq("custName", "bbb"));
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test03
* @Description:模糊查询
* @throws Exception
*/
@Test
public void test03() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
/*
* == eq > gt >= ge < lt <= le <> ne in in like like
*
*/
criteria.add(Restrictions.like("custName", "b%"));
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test04
* @Description:分页查询
* @throws Exception
*/
@Test
public void test04() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setFirstResult(0);
criteria.setMaxResults(3);
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test05
* @Description:排序查询
* @throws Exception
*/
@Test
public void test05() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.addOrder(Order.desc("custId"));
List list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
/**
* @MethodName:test06
* @Description:聚合函数
* @throws Exception
*/
@Test
public void test06() throws Exception {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.rowCount());
Object obj = criteria.uniqueResult();
System.out.println(obj);
tx.commit();
}
}