搭建和配置Spring与jdbc整合的环境

来源:互联网 发布:哈希算法java 编辑:程序博客网 时间:2024/04/29 18:56
1、使用Spring+JDBC集成步骤如下:
1)配置数据源,如:
 <bean id="dataSource" class="orgrivae.apache.commons.dbcp.datasources" destroy-method="close">    <property name="driverClass" value="org.gjt.mm.mysql.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/><property name="user" value="root"/><property name="password" value="root"/><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="1"/><!--连接池中保留的最小连接数。--><property name="minPoolSize" value="1"/><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="300"/><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="60"/><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="5"/><!--每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60"/></bean>
2)配置事物:配置事物时,需要在xml配置文件中引入用于声明事物的tx命名空间,事物的配置方式有两种:注解方式和基于xml配置方式
3)注解方式进行配置事物
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!--使用基于注解方式配置事务 --><tx:annotation-driven transaction-manager="txManager"/>
bean中注解声明
@Service @Transactional
Publci class PersonServiceBean implemnets PersonService{.....}
在进行注解方式配置事物的时候,首先要在xml中规定好命名空间,主要是下面三个:
1、)xmlns:tx="http://www.springframework.org/schema/tx"
2、)http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
3、)http://www.springframework.org/schema/tx

采用@Transactional注解方式使用事物

2、Spring集成的jdbc编码和事务管理

1)、如果我们没有用@Transactional进行注解的形式,那么方法中的语句会在各自的事物进行执行,无法保证在同一个事物执行,可能会出现一些问题
2)、加上这个注解,告诉注解器,这个类里面的方法都是应该在同一事物进行管理


0 0