在src下创建一个hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate3_day01property>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">123property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml"/>
session-factory>
hibernate-configuration>
映射文件只要是一个XML格式文件就可以.名字任意.
* 通常情况下名称规范:
* 实体类名称.hbm.xml
<hibernate-mapping>
<class name="cn.itcast.hibernate3.demo1.Customer" table="customer">
<id name="id" column="id">
<generator class="native"/>
id>
<property name="name" column="name" type="string"/>
<property name="age" column="age"/>
class>
hibernate-mapping>
//向数据库中插入一条记录
@Test
// 向数据库中插入一条记录
public void demo1(){
// 1.Hiberante框架加载核心配置文件(有数据库连接信息)
Configuration configuration = new Configuration().configure();
// 2.创建一个SessionFactory.(获得Session--相当连接对象)
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.获得Session对象.
Session session = sessionFactory.openSession();
// 4.默认的情况下,事务是不自动提交.
Transaction tx = session.beginTransaction();
// 5.业务逻辑操作
// 向数据库中插入一条记录:
Customer customer = new Customer();
customer.setName("任童");
customer.setAge(28);
session.save(customer);
// 6.事务提交
tx.commit();
// 7.释放资源
session.close();
sessionFactory.close();
}
session.save(customer);
Customer customer = (Customer)session.get(Customer.class ,1);
Customer customer = (Customer)session.load(Customer.class,1);
session.update(customer);
//修改有两种方式 :
// 5.1手动创建对象的方式
Customer customer = new Customer();
customer.setId(2);
customer.setName("苍老师");
session.update(customer);
//这种方式如果没有设置的属性,将这个属性的默认值存入了.(不好.)
//先查询在修改的方式(推荐方式)
Customer customer = (Customer) session.get(Customer.class, 1);
customer.setName("凤姐");
session.update(customer);
session.delete(customer);
删除记录有两种方式:
//手动创建对象的方式
Customer customer = new Customer();
customer.setId(2);
session.delete(customer);
//先查询再删除的方式
Customer customer = (Customer)session.get(Customer.class, 1);
session.delete(customer);
HQL:
HQL:Hibernate Query Language.
面向对象的写法:
Query query = session.createQuery("from Customer where name = ?");
query.setParameter(0, "苍老师");
Query.list();
QBC:
Query By Criteria.(条件查询)
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("name", "凤姐"));
List list = criteria.list();
SQL:
SQLQuery query = session.createSQLQuery("select * from customer");
List
subselect :发送子查询查询关联对象.(需要使用Query接口测试)
lazy:控制关联对象的检索是否采用延迟.
* 如果fetch是join的情况,lazy属性将会忽略.
向一级缓存存入数据的时候,放入一级缓存区和一级缓存快照区,当更新了一级缓存的数据的时候,事务一旦提交,比对一级缓存和快照区,如果数据一致,不更新,如果数据不一致,自动更新数据库
一级缓存是与session的生命周期相关的.session生命周期结束,一级缓存销毁了.
* clear()/evict()/flush()/refresh()管理一级缓存.
* clear() :清空一级缓存中所有的对象.
* evict(Object obj) :清空一级缓存中某个对象.
* flush() :刷出缓存.
* refresh(Object obj):将快照区的数据重新覆盖了一级缓存的数据.
1、类级别二级缓存区域
2、集合级别二级缓存区域
3、时间戳缓存区域
1、拷贝ehcache-1.5.0.jar到当前工程的lib目录下,依赖 backport-util-concurrent 和 commons-logging
2、开启二级缓存
<property name="hibernate.cache.use_second_level_cache">trueproperty>
3、指定缓存的供应商
"hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
4、配置缓存哪些实体类
在hibernate.cfg.xml文件中配置(建议)
<class-cache class="cn.itcast.c3p0.Customer" usage="read-write"/>
<class-cache class="cn.itcast.c3p0.Order" usage="read-write"/>
<collection-cache collection="cn.itcast.c3p0.Customer.orders" usage="read-write"/>
5、配置ehcache默认的配置文件ehcache.xml(名字固定)(放在类路径下)
<diskStore path="D:\cache" />
<cache name=""
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpriyThreadIntervalSeconds="120" />
ehcache>
cache元素的属性
name:设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxElementsInMemory :设置基于内存的缓存中可存放的对象最大数目
eternal:设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false
timeToIdleSeconds:设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds:设置对象生存最长时间,超过这个时间,对象过期。如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值
overflowToDisk:设置基于内在的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
diskPersistent 当jvm结束时是否持久化对象 true false 默认是false
diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
查询缓存依赖于二级缓存
查询缓存可以缓存属性!!!!
对于希望启用查询缓存的查询语句, 调用 Query 的 setCacheable(true) 方法
hibernate 配置文件中启用查询缓存
<property name=“hibernate.cache.use_query_cache">trueproperty>