1.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: tx= " http://www.springframework.org/schema/tx"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: aop= " http://www.springframework.org/schema/aop"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd" >
< 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/mybatis?useSSL=true& useUnicode=true& characterEncoding=utf8" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " mapperLocations" value = " classpath*:**/**Mapper.xml" />
</ bean>
< bean name = " transactionManager"
class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: annotation-driven/>
</ beans>
2.mapper接口,mapper实现类(@Transactional),mapper.xml
public interface BlogMapper {
void setSelect ( @Param ( "author" ) String author, @Param ( "id" ) int id) ;
}
public class BlogImp extends SqlSessionDaoSupport implements BlogMapper {
@Transactional
@Override
public void setSelect ( String author, int id) {
getSqlSession ( ) . getMapper ( BlogMapper . class ) . setSelect ( author, id) ;
int a = 10 / 0 ;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD com.Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.mapper.BlogMapper" >
< update id = " setSelect" >
update blog set author=#{author} where id=#{id}
</ update>
</ mapper>
4.测试类
public class Test7 {
@Test
public void name ( ) {
ApplicationContext context = new ClassPathXmlApplicationContext ( "applicationconfig.xml" ) ;
BlogMapper mapper = ( BlogMapper ) context. getBean ( "blogImp" ) ;
mapper. setSelect ( "ld" , 5 ) ;
}
}
5.结果是加了注解,事务配置了xml,是会回滚的