<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:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
beans>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
configuration>
这里使用spring提供的数据源类,在创建sqlSessionFactory 时需要数据源
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${password}"/>
<property name="driverClassName" value="${driverClassName}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:org/mapper/*.xml"/>
bean>
如果不使用最原始的注册mapper方法,这个bean可以省略
注意: 使用构造器注入
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
a.新建mapper接口的实现类
public class UserMapperImp implements UserMapper{
// 调用接口方法需要SqlSessionTemplate
private final SqlSessionTemplate sqlSessionTemplate;
public UserMapperImp(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<User> selectById(int id) {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.selectById(id);
}
}
b.为实现类配置bean
<bean id="UserMapper1" class="org.mapper.UserMapperImp">
<constructor-arg name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
bean>
c.使用
ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring-config.xml");
UserMapper mapper = cpx.getBean("UserMapper1",UserMapper.class);
List<User> users = mapper.selectById(1);
a.实现类继承SqlSessionDaoSupport 实现mapper接口
public class UserMapperSupportImp extends SqlSessionDaoSupport implements UserMapper {
// 父类提供的方法 getSqlSessionTemplate()
public List<User> selectById(int id){
SqlSessionTemplate sqlSessionTemplate = getSqlSessionTemplate();
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.selectById(id);
}
}
b.为实现类配置bean
<bean id="UserMapper2" class="org.mapper.UserMapperSupportImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
a.为MapperFactoryBean配置bean
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="org.mapper.UserMapper"/>
bean>
b.使用
UserMapper mapper = cpx.getBean("userMapper",UserMapper.class);
a.声明命名空间
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
b.配置 base-package:需要扫描的包
<mybatis:scan base-package="org.mapper"/>
c.使用
UserMapper mapper1 = cpx.getBean("userMapper",UserMapper.class);
TestMapper mapper2 = cpx.getBean(TestMapper.class);
原子性; 过程原子
一致性: 前后数据库状态一致
隔离性: 事务之间隔离
持久性: 一旦提交,数据永久改变
<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:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
bean>
name : 需要配置事务的方法
propagation :一般为REQUIRED,
read-only:用于select,只读
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="deleteById" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="cut" expression="execution(public * org.mapper.*.*(..))" />
<aop:advisor advice-ref="myTx" pointcut-ref="cut"/>
aop:config>
目前不太理解为什么 tx:method 和切入点都在声明一种方法,感觉很多余。一个勉强的解释是
tx:method 是在声明一种规则,而aop:config在强调在哪里切入