Spring-Mybatis整合笔记

tech2024-04-06  58

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"> <!-- DataSource:使用Spring的数据源替换Mybatis中的数据源 --> <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&amp;characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasoure" /> <!-- 绑定Mybatis配置文件 --> <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(); /** * SqlSession sqlSession = getSqlSession(); * 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="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> <!-- 结合AOP实现事务的织入 --> <!-- 配置事务通知的类 --> <!-- 配置事务的传播特性:propagation="REQUIRED" 默认为:REQUIRED --> <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切入 --> <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编程式业务: 需要在代码中进行事务管理
最新回复(0)