Spring-Mybatis
文档 http://mybatis.org/spring/zh/transactions.html
1.整合Mybatis
spring_Mybatis整合方式1:
1.编写数据源2.SqlSessionFactory3.SqlSessionTemplate
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="datasoure" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasoure" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/mashiro/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
4.给接口加实现类,注册到Spring中
package com
.mashiro
.mapper
;
import com
.mashiro
.pojo
.User
;
import org
.apache
.ibatis
.session
.SqlSession
;
import java
.util
.List
;
public class UserMapperImpl implements UserMapper {
private SqlSession sqlSession
;
public void setSqlSession(SqlSession sqlSession
) {
this.sqlSession
= sqlSession
;
}
@Override
public List
<User> userList() {
UserMapper mapper
= sqlSession
.getMapper(UserMapper
.class);
return mapper
.userList();
}
}
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.mashiro.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
5.测试
import com
.mashiro
.mapper
.UserMapper
;
import com
.mashiro
.pojo
.User
;
import org
.junit
.Test
;
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
public class MyTest {
@Test
public void Test01(){
ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper
= context
.getBean("userMapper", UserMapper
.class);
for (User user
: userMapper
.userList()) {
System
.out
.println(user
);
}
}
}
spring_Mybatis整合方式2:
package com
.mashiro
.mapper
;
import com
.mashiro
.pojo
.User
;
import org
.apache
.ibatis
.session
.SqlSession
;
import org
.mybatis
.spring
.support
.SqlSessionDaoSupport
;
import java
.util
.List
;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List
<User> userList() {
return getSqlSession().getMapper(UserMapper
.class).userList();
}
}
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper2" class="com.mashiro.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
2.事务
2.1.事务配置
xmlns:tx="http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="del" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.mashiro.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
2.2.Tips
声明式业务: AOP编程式业务: 需要在代码中进行事务管理