Spring 数据库
定义DataSource
<bean id="dataSource " class="org.apache.commons.dbcp.BasicDataSource ">
<property name="driver "><value></value> </property>
<property name="url "> <value></value> </property>
<property name="username "> <value></value> </property>
<property name="password "><value></value></property>
</bean>
测试;
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Spring的所有的模板类都是线程安全的.Spring的数据访问框架都结合了模板类.
使用JdbcTemplate模板:
使用JdbcTemplate 模板,每一个DAO类都需要配置一个JdbcTemplate的实例.
例如:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="**Dao1" class="**.**.*DAO1">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
</bean>
<bean id="**Dao2" class="**.**.*DAO2">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
</bean>
Spring 整合 Hibernate
(1)定义DataSource
<bean id="dataSource " class="org.apache.commons.dbcp.BasicDataSource ">
<property name="driver "><value></value> </property>
<property name="url "> <value></value> </property>
<property name="username "> <value></value> </property>
<property name="password "><value></value></property>
</bean>
(2) 定义SessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name="dataSource">
<ref local="dataSource_id"/>
</property>
<property name="hibernateProperties ">
<props>
<prop key="hibernate.dialect"></prop>
<prop key="hibernate.cache.provider_class"></prop>
<prop key="hibernate.query.factory_class"></prop>
<prop key="hibernate.connection.autocommit"></prop>
<prop key="hibernate.jdbc.batch_size"></prop>
<prop key="hibernate.jdbc.fetch_size"></prop>
<prop key="hibernate.cache.use_query_cache"></prop>
<prop key="hibernate.cache.use_second_level_cache"></prop>
<prop key="hibernate.show_sql"></prop>
</props>
</property>
<property name="mappingResources ">
<list>
<value>com/**/**/**/**.hbm.xml</value>
</list>
</property>
</bean>
当持久化类太多,mappingResources配置造成太多的value值;此时是用class path下的一个子路径来配置
mappingDirectoryLocations属性,Spring会找到这个路径下的每一个*.hbm.xml文件,来配置SessionFactory.
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath: /com/**/*/*</value>
</list>
</property>
</bean>
(3)接下来,需要一个模板类 .
<bean id="hibernateTemplate " class="org.springframework.orm.hibernate.HIbernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="*DAO" class="**.**.*DAO">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
使用Hibernate中的模板回调机制
HibernateTemplate和HibernateCallback
HibernateCallback接口只有一个方法
Object doInHibernate(Session session) throws HibernateException ,SQLException;
hibernateTemplate.update
.find
.load
等方法;
HibernateDaoSupport的子类
选择这种设计,就需要织入SessionFactory;
Hibernate事务
(4) 事务管理
<bean id="transactionManager " class="org.springframework.orm.hibernate.HibernateTransactionManager ">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
HibernateTransactionManager把事务管理委托给一个从Hibernate Session中取得的net.sf.hibernate.Transaction对象;
事务成功时,HibernateTransactionManager将调用Transaction对象的commit()方法.失败,调用rollback()方法;
(5)
<bean id="**Service" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean ">
<property name="proxyInterfaces">
<list>
<value>***.I**Service </value>
</list>
</property>
<property name="target">
<ref bean="**ServiceTarget"/>
</property>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="**attributeSource"/>
</property>
</bean>