作者:风中冰雨 | 来源:互联网 | 2023-09-25 10:09
整合 mybatis-spring pow.xml
< dependencies> < dependency> < groupId> org.mybatis groupId> < artifactId> mybatis artifactId> < version> 3.5.5 version> dependency> < dependency> < groupId> mysql groupId> < artifactId> mysql-connector-java artifactId> < version> 5.1.30 version> < scope> runtime scope> dependency> < dependency> < groupId> org.projectlombok groupId> < artifactId> lombok artifactId> < version> 1.18.12 version> dependency> < dependency> < groupId> junit groupId> < artifactId> junit artifactId> < version> 4.11 version> < scope> test scope> dependency> < dependency> < groupId> log4j groupId> < artifactId> log4j artifactId> < version> 1.2.17 version> dependency> < dependency> < groupId> org.aspectj groupId> < artifactId> aspectjweaver artifactId> < version> 1.9.5 version> dependency> < dependency> < groupId> org.mybatis groupId> < artifactId> mybatis-spring artifactId> < version> 2.0.5 version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-aop artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-context artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-orm artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-oxm artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-jdbc artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-tx artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-web artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-webmvc artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-aop artifactId> < version> 5.2.7.RELEASE version> dependency> < dependency> < groupId> org.springframework groupId> < artifactId> spring-test artifactId> < version> 5.2.7.RELEASE version> dependency> dependencies>
1.编写数据源配置 2.sqlSessionFactory 3.sqlSessionFactoryTemplate 4.需要给接口加实现类【可以充当service层】 5.将自己写的实现类注入到spring中 6.测试使用
spring-dao.xml 配置数据源、sqlSessionFactory、sqlSessionFactoryTemplate
< beans xmlns &#61; " http://www.springframework.org/schema/beans" xmlns: xsi&#61; " http://www.w3.org/2001/XMLSchema-instance" xmlns: aop&#61; " http://www.springframework.org/schema/aop" xmlns: tx&#61; " http://www.springframework.org/schema/tx" xmlns: context&#61; " http://www.springframework.org/schema/context" xsi: schemaLocation&#61; " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd" > < bean id &#61; " ds" class &#61; " org.springframework.jdbc.datasource.DriverManagerDataSource" > < property name &#61; " driverClassName" value &#61; " com.mysql.jdbc.Driver" /> < property name &#61; " url" value &#61; " jdbc:mysql://localhost:3306/mybatis" /> < property name &#61; " username" value &#61; " root" /> < property name &#61; " password" value &#61; " root" /> bean> < bean id &#61; " sessionFactory" class &#61; " org.mybatis.spring.SqlSessionFactoryBean" > < property name &#61; " dataSource" ref &#61; " ds" > property> < property name &#61; " configLocation" value &#61; " mybatis-config.xml" /> < property name &#61; " mapperLocations" value &#61; " com/wei/dao/*Mapper.xml" /> bean> < bean id &#61; " sqlSession" class &#61; " org.mybatis.spring.SqlSessionTemplate" > < constructor-arg index &#61; " 0" ref &#61; " sessionFactory" /> bean> beans>
applicationContext.xml 导入子配置文件&#xff0c;并注入实现类到spring
< beans xmlns &#61; " http://www.springframework.org/schema/beans" xmlns: xsi&#61; " http://www.w3.org/2001/XMLSchema-instance" xmlns: aop&#61; " http://www.springframework.org/schema/aop" xsi: schemaLocation&#61; " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd" > < import resource &#61; " classpath:spring-dao.xml" /> < bean id &#61; " userMapper" class &#61; " com.wei.dao.UserMapperImpl" > < property name &#61; " sqlSession" ref &#61; " sqlSession" /> bean> beans>
添加实现类UserMapperImpl.class
public class UserMapperImpl implements UserMapper { private SqlSessionTemplate sqlSession; public void setSqlSession ( SqlSessionTemplate sqlSession) { this . sqlSession &#61; sqlSession; } &#64;Override public List< User> findAll ( ) { return sqlSession. getMapper ( UserMapper. class ) . findAll ( ) ; } }
spring声明式事务 事务四大特性ACID 原子性&#xff1a;事务要么都成功&#xff0c;要么都失败。 一致性&#xff1a;事务前后&#xff0c;保证业务的一致性。不会出现数据的损坏。 隔离性&#xff1a;并发操作应该隔离开&#xff0c;防止数据损坏。 持久化&#xff1a;事务一旦提交成功&#xff0c;无论出现什么错误&#xff0c;结果都不会被影响&#xff0c;被持久化写到存储器中。 事务的7种传播行为 事务传播行为&#xff08;propagation behavior&#xff09;指的就是当一个事务方法被另一个事务方法调用时&#xff0c;这个事务方法应该如何进行。
参考优秀文章 &#xff1a;事务属性之7种传播行为 https://blog.csdn.net/soonfly/article/details/70305683
结合AOP实现事务的织入 保证一个业务的前后的一致性。不会因为错误而改变了只运行一半的数据。 配置在spring-dao下
< bean id &#61; " transactionManager" class &#61; " org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name &#61; " dataSource" ref &#61; " ds" /> bean> < tx: advice id &#61; " txAdvice" transaction-manager &#61; " transactionManager" > < tx: attributes> < tx: method name &#61; " add" propagation &#61; " REQUIRED" /> < tx: method name &#61; " delete" propagation &#61; " REQUIRED" /> < tx: method name &#61; " update" propagation &#61; " REQUIRED" /> < tx: method name &#61; " query" read-only &#61; " true" /> < tx: method name &#61; " *" propagation &#61; " REQUIRED" /> tx: attributes> tx: advice> < aop: config> < aop: pointcut id &#61; " txPointCut" expression &#61; " execution(* com.wei3.mapper.*.*(..))" /> < aop: advisor advice-ref &#61; " txAdvice" pointcut-ref &#61; " txPointCut" /> aop: config>