spring、mybatis事务配置和控制

springmybatis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.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">
    <context:component-scan base-package="com.scsmsjk"></context:component-scan>

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 连接oracle数据库无需以下配置-->
        <property name="initialSize" value="${initialSize}" />
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}" />
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}" />
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}" />
    </bean>

    <!-- sqlSessionFactory配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/scsmsjk/mapper/*.xml"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
        <property name="typeAliasesPackage" value="com.nhinter.entity"/>
        <!-- <property name="plugins">
            <array>
                分页插件配置
                <bean id="paginationInterceptor" class="com.xyb2c.plugin.PaginationInterceptor"/>
            </array>
        </property> -->
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.scsmsjk.dao"></property>
    </bean>
    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

service层

package com.scsmsjk.serviceImp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.scsmsjk.dao.TsmsHassentMapper;
import com.scsmsjk.dao.TsmsSendingMapper;
import com.scsmsjk.entity.TsmsSending;
import com.scsmsjk.service.SmsService;


@Service
@Transactional(rollbackFor=Exception.class)
public class SmsServiceImp implements SmsService {
	
	@Autowired
	TsmsSendingMapper tsmssendDao;
	
	@Autowired
	TsmsHassentMapper tsmshassentDao;
	
	public List<TsmsSending>  selectAll(){
		return tsmssendDao.selectAll();
	}
	
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=false)
	public int deleteByPrimaryKey(Integer fId){
		int aa=tsmshassentDao.deleteByPrimaryKey(19);
		int cc=aa/0;
		return tsmssendDao.deleteByPrimaryKey(fId);
	}

}

  注意:千万不可加try{} catch(Exception erro){},否则影响事务的原子性。

转载于:https://www.cnblogs.com/Anders888/p/7208447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMyBatis是两个非常流行的Java开发框架,它们可以很好地结合使用。 首先,你需要在你的项目中引入SpringMyBatis的相关依赖。 对于Spring,你可以使用Maven或者Gradle来管理依赖。在你的项目的pom.xml(或者build.gradle)文件中,添加以下依赖: ```xml <!-- Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!-- SpringMyBatis的集成依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> ``` 接下来,你需要配置SpringMyBatis的相关配置文件。 首先是Spring配置文件(比如applicationContext.xml),你可以在其中配置Spring的上下文和其他相关的Bean。 ```xml <!-- 配置Spring的上下文 --> <context:annotation-config/> <context:component-scan base-package="com.example"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置MyBatis的SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 扫描MyBatis的Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 在上面的配置中,你需要根据你的数据库设置正确的数据源和连接信息。 接下来是MyBatis配置文件(比如mybatis-config.xml),你可以在其中配置MyBatis的相关设置。 ```xml <configuration> <settings> <!-- 开启驼峰命名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration> ``` 在上面的配置中,我们开启了MyBatis的驼峰命名转换,这样可以方便地将数据库中的下划线命名转换为Java中的驼峰命名。 最后,你需要创建Mapper接口和对应的Mapper XML文件来定义SQL语句和映射关系。 这样,你就完成了SpringMyBatis配置。在你的代码中,你可以使用Spring的依赖注入来获取MyBatis的Mapper接口,并使用它来进行数据库操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值