Spring 数据库

mowengaoboa 2010-03-15

Spring 数据库

定义DataSource

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driver"><value></value></property>

<propertyname="url"><value></value></property>

<propertyname="username"><value></value></property>

<propertyname="password"><value></value></property>

</bean>

测试;

DriverManagerDataSourcedataSource=newDriverManagerDataSource();

dataSource.setDriverClassName(driver);

dataSource.setUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);

Spring的所有的模板类都是线程安全的.Spring的数据访问框架都结合了模板类.

使用JdbcTemplate模板:

使用JdbcTemplate模板,每一个DAO类都需要配置一个JdbcTemplate的实例.

例如:

<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">

<propertyname="dataSource"><refbean="dataSource"/></property>

</bean>

<beanid="**Dao1"class="**.**.*DAO1">

<propertyname="jdbcTemplate"><refbean="jdbcTemplate"/></property>

</bean>

<beanid="**Dao2"class="**.**.*DAO2">

<propertyname="jdbcTemplate"><refbean="jdbcTemplate"/></property>

        </bean>

Spring 整合 Hibernate

(1)定义DataSource

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driver"><value></value></property>

<propertyname="url"><value></value></property>

<propertyname="username"><value></value></property>

<propertyname="password"><value></value></property>

</bean>

(2) 定义SessionFactory

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<propertyname="dataSource">

<reflocal="dataSource_id"/>

</property>

<propertyname="hibernateProperties">

<props>

<propkey="hibernate.dialect"></prop>

<propkey="hibernate.cache.provider_class"></prop>

<propkey="hibernate.query.factory_class"></prop>

<propkey="hibernate.connection.autocommit"></prop>

<propkey="hibernate.jdbc.batch_size"></prop>

<propkey="hibernate.jdbc.fetch_size"></prop>

<propkey="hibernate.cache.use_query_cache"></prop>

<propkey="hibernate.cache.use_second_level_cache"></prop>

<propkey="hibernate.show_sql"></prop>

</props>

</property>

<propertyname="mappingResources">

<list>

<value>com/**/**/**/**.hbm.xml</value>

</list>

</property>

</bean>

当持久化类太多,mappingResources配置造成太多的value值;此时是用classpath下的一个子路径来配置

mappingDirectoryLocations属性,Spring会找到这个路径下的每一个*.hbm.xml文件,来配置SessionFactory.

<beanid="sessionFactory"class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<propertyname="mappingDirectoryLocations">

<list>

<value>classpath:/com/**/*/*</value>

</list>

</property>

</bean>

(3)接下来,需要一个模板类.

<beanid="hibernateTemplate"class="org.springframework.orm.hibernate.HIbernateTemplate">

<propertyname="sessionFactory">

<refbean="sessionFactory"/>

</property>

</bean>

<beanid="*DAO"class="**.**.*DAO">

<propertyname="hibernateTemplate">

<refbean="hibernateTemplate"/>

</property>

</bean>

使用Hibernate中的模板回调机制

HibernateTemplate和HibernateCallback

HibernateCallback接口只有一个方法

ObjectdoInHibernate(Sessionsession)throwsHibernateException,SQLException;

hibernateTemplate.update

.find

.load

等方法;

HibernateDaoSupport的子类

    选择这种设计,就需要织入SessionFactory;

Hibernate事务  (4) 事务管理

<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">

<propertyname="sessionFactory">

<refbean="sessionFactory"/>

</property>

</bean>

HibernateTransactionManager把事务管理委托给一个从HibernateSession中取得的net.sf.hibernate.Transaction对象;

    事务成功时,HibernateTransactionManager将调用Transaction对象的commit()方法.失败,调用rollback()方法;

(5)

<bean id="**Service" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<propertyname="proxyInterfaces">

<list>

<value>***.I**Service</value>

</list>

</property>

<propertyname="target">

<refbean="**ServiceTarget"/>

</property>

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="transactionAttributeSource">

<refbean="**attributeSource"/>

</property>

</bean>

相关推荐

Gbug00 / 0评论 2010-10-08