SessionFactory在Hibernate中的作用
来源:互联网 发布:竞价调价软件 编辑:程序博客网 时间:2024/06/08 15:08
1 .先来说一下Configuration的作用
Configuration的构造方法: 将Hibernate的配置信息(hibernate.cfg.xml)读入到内存.
2.SessionFactory的作用
把Configuration对象中的所有配置信息拷贝到SessionFactory的缓存中,创建后不再与Configuration对象关联.
Hibernate的配置信息举例:
hibernate.cfg.xml:<?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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql:///crm</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory></hibernate-configuration>
3.注入问题
dataSource数据源是注入给sessionFactory的,然后关联到session中.
sessionFactory需要注入给DAO, 做项目时,经常是让DAO层继承HibernateDaoSupport. 因为HibernareDaoSupport有个属性就是sessionFactory, 所以向DAO类中注入sessionFactory时,就是向hibernateDaoSupport中注入了.
eg:
DAO类:
public class ItemsDaoImpl extends HibernateDaoSupport implements ItemsDao { @Override public List<Items> findAll() { // TODO Auto-generated method stub return (List<Items>) this.getHibernateTemplate().find("from Items"); } @Override public Items findById(Integer id) { // TODO Auto-generated method stub return getHibernateTemplate().get(Items.class, id); } @Override public void update(Items item) { // TODO Auto-generated method stub getHibernateTemplate().update(item); }}
applicationContext.xml(下面例子是spring和hernate的配置文件整合成一个的演示)
<!-- spring相关配置 --> <!-- 1. 加载jdbc.prpperties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 3.SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> <property name="mappingLocations" value="classpath:com/qx/domain/*.hbm.xml"></property> </bean> <!-- 4. 平台事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> <property name="dataSource" ref="dataSource"></property> </bean>
阅读全文
0 0
- SessionFactory在Hibernate中的作用
- SessionFactory在Hibernate中的作用
- Hibernate中的二级缓存SessionFactory
- Hibernate中SessionFactory的作用
- hibernate在ssh中的作用
- hibernate中的sessionFactory接口讲解
- hibernate中的sessionfactory和session
- HiberNate 中 conFiguration 和 sessionFactory 的作用
- Hibernate在企业中的实战作用
- struts2、hibernate、spring在SSH中的作用
- SessionFactory (org.hibernate.SessionFactory)
- hibernate中的SessionFactory,Session分别表示什么
- hibernate中的SessionFactory的openSession与getCurrentSession
- spring 自动注入Hibernate中的SessionFactory 探索
- hibernate SessionFactory
- Hibernate SessionFactory
- Hibernate SessionFactory
- hibernate中的Inverse作用
- 五、Linux——结构体
- html 如何引入一个公共的头部和底部(不使用模版引擎如jsp、freemarker、velocity的方式)
- ZCMU-1898 Trainsorting
- 《菜鸟反攻战》第一章----java注解
- 1073 约瑟夫环(递推)
- SessionFactory在Hibernate中的作用
- Python 命令汇总
- hdu 1166 (单点更新+区间求和+裸题)
- C++的预处理(Preprocess)
- 丑数
- httpd-apache服务
- 不用开安卓游戏也能肝……脱机挂揭秘与简记(1)
- POJ 3162 Walking Race 笔记
- 运算符优先级