在applicationContext.xml中对spring事务经行引用首先引入以下几个包
- ojdbc.jar
- spring-jdbc.jar
- spring-tx.jar
- commons-dbcp.jar
- commons-pool.jar
- aopalliance.jar
然后在applicationContext.xml中经行以下的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置数据库相关-事务-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 配置事务管理器“txManger"-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--增加对事务的支持-核心-->
<tx:annotation-driven transaction-manager="txManger"></tx:annotation-driven>
</beans>
其中dataSource的属性比较重要其链接
下面这个为如何使用spring对数据库进行连接的简单模板(大致内容同Java jdbc相类似)
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("H:\\spring-jdbc\\src\\applicationContext.xml");
DataSource dataSource= (DataSource) context.getBean("dataSource");
try {
Connection connection=dataSource.getConnection();
String spl="SELECT *from school_student";
PreparedStatement preparedStatement = connection.prepareStatement(spl);
ResultSet resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getObject(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
java xml事务配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="123"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启组件扫描-->
<!--配置事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<!--配置以什么开头所需要的操作 -->
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<!--配置事务的切面-->
<aop:config>
<aop:pointcut expression="execution(* Service.*.*(..))"
id="pt" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config>
<context:component-scan base-package="transcactionManagers,Service,dao"></context:component-scan>
</beans>
注解事务的话就
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
后在所需要的类上添加@Transactional注解即可
@Transactional注解:
1)应用事务的注解
2)定义到方法上: 当前方法应用spring的声明式事务
3)定义到类上: 当前类的所有的方法都应用Spring声明式事务管理;
4)定义到父类上: 当执行父类的方法时候应用事务。