Spring中事务管理是它的核心之一,今天我们要讲解的就是spring中如何管理事务,下面一起来看看吧。
示例:
<</span> context: annotation - config / > <</span> context: component - scan base - package = "com.user.servie" / > <</span> bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > <</span> property name = "driverClassName" > <</span> value > com.mysql.jdbc.Driver < /</span > value > </</span> property > <</span> property name = "url" > <</span> value > jdbc: mysql: //localhost:3306/GuJin?characterEncoding=UTF-8</</span>value> </</span> property > <</span> property name = "username" > <</span> value > root < /</span > value > </</span> property > <</span> property name = "password" > <</span> value > 1111 < /</span > value > </</span> property > </</span> bean > <</span> bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <</span> property name = "dataSource" ref = "dataSource" > </</span> property > <</span> property name = "mapperLocations" value = "classpath:com/user/mapping/*.xml" > </</span> property > <</span> property name = "configLocation" value = "classpath:mybatis-config.xml" > </</span> property > </</span> bean > <</span> bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <</span> property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" > </</span> property > <</span> property name = "basePackage" value = "com.user.dao" / > </</span> bean > <</span> bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <</span> property name = "dataSource" ref = "dataSource" / > </</span> bean > <</span> tx: annotation - driven transaction - manager = "transactionManager" > </</span> tx: annotation - driven >
1、首先通过如下代码注解,将Service生命周期纳入Spring管理
</span> context: annotation - config / > <</span> context: component - scan base - package = "com.user.servie" / >
2、其二再开启事务注解扫描
<</span> tx: annotation - driven transaction - manager = "transactionManager" > </</span> tx: annotation - driven >
<</span> bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <</span> property name = "dataSource" ref = "dataSource" / > </</span> bean >
4、开始配置数据源
<</span> bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > <</span> property name = "driverClassName" > <</span> value > com.mysql.jdbc.Driver < /</span > value > </</span> property > <</span> property name = "url" > <</span> value > jdbc: mysql: //localhost:3306/GuJin?characterEncoding=UTF-8</</span>value> </</span> property > <</span> property name = "username" > <</span> value > root < /</span > value > </</span> property > <</span> property name = "password" > <</span> value > 1111 < /</span > value > </</span> property > </</span> bean >
5、调用配置文件
扫描配置文件mapper:
<</span> bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <</span> property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" > </</span> property > <</span> property name = "basePackage" value = "com.user.dao" / > </</span> bean >
PS:com.user.dao是接口interface包
6、扫描存放sql的mapper文件
<</span> bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <</span> property name = "dataSource" ref = "dataSource" > </</span> property > <</span> property name = "mapperLocations" value = "classpath:com/user/mapping/*.xml" > </</span> property > <</span> property name = "configLocation" value = "classpath:mybatis-config.xml" > </</span> property > </</span> bean > 其中: @1classpath: com / user / mapping /*.xml;*代表com/user/mapping路径下所有文件。 @2 classpath:mybatis-config.xml 配置了DTO类的指向。比如我的: <</span>configuration> <</span>typeAliases> <</span>typeAlias type="com.user.entity.User" alias="User" /> </</span>typeAliases> </</span>configuration>
User就代表了com.user.entity.User类对象,其目的是简化代码。
以上就是本篇文章的所有内容,关于java架构师还有想要疑问的话可以来关注我们网站了解详情。
推荐阅读: