热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【Spring】整合mybatisspring

整合mybatis-springpow.xmlorg.mybatis

整合 mybatis-spring

pow.xml

<dependencies><dependency><groupId>org.mybatisgroupId><artifactId>mybatisartifactId><version>3.5.5version>dependency><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId><version>5.1.30version><scope>runtimescope>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><version>1.18.12version>dependency><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>4.11version><scope>testscope>dependency><dependency><groupId>log4jgroupId><artifactId>log4jartifactId><version>1.2.17version>dependency><dependency><groupId>org.aspectjgroupId><artifactId>aspectjweaverartifactId><version>1.9.5version>dependency><dependency><groupId>org.mybatisgroupId><artifactId>mybatis-springartifactId><version>2.0.5version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-aopartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-ormartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-oxmartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-jdbcartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-txartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-webartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-webmvcartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-aopartifactId><version>5.2.7.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-testartifactId><version>5.2.7.RELEASEversion>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;Overridepublic List<User> findAll() {return sqlSession.getMapper(UserMapper.class).findAll();}
}

spring声明式事务


事务四大特性ACID


  1. 原子性&#xff1a;事务要么都成功&#xff0c;要么都失败。
  2. 一致性&#xff1a;事务前后&#xff0c;保证业务的一致性。不会出现数据的损坏。
  3. 隔离性&#xff1a;并发操作应该隔离开&#xff0c;防止数据损坏。
  4. 持久化&#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>

推荐阅读
author-avatar
风中冰雨
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有