步骤:

1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息

  1. jdbc.properties

    jdbc.user=root
  2. jdbc.password=123456
  3. jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
  4. jdbc.driver=com.mysql.jdbc.Driver

2.创建spring的配置文件,配置Spring和Mybatis整合相关

  1. ApplicationContext.xml
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context.xsd
  16. http://www.springframework.org/schema/tx
  17. http://www.springframework.org/schema/tx/spring-tx.xsd">
  18.  
  19. </beans>

3.在spring的配置文件中加载jdbc.properties文件,通过<context:property-placeholder>标签

  1.  
  1. ApplicationContext.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <context:property-placeholder location="classpath:jdbc.properties"/>
  18. </beans>

4.在spring配置文件中配置数据源(数据库连接池)

  1.  
  1. ApplicationContext.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <context:property-placeholder location="classpath:jdbc.properties"/>
  18.  
  19. <bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource">
  20. <property name="driverClassName" value="${jdbc.driver}"/>
  21. <property name="url" value="${jdbc.url}"/>
  22. <property name="username" value="${jdbc.user}"/>
  23. <property name="password" value="${jdbc.password}"/>
  24. </bean>
  25. </beans>

5.在spring配置文件中配置SqlSessionFactoryBean

作用:用于指定Xxxmapper.xml的位置,然后根据xml文件生成代理对象

其作用相当于原来mybatis主配置文件,原来mybatis主配置文件有的功能,在这里都可以实现,如取别名、开启二级缓存等。

  1.  
  1. ApplicationContext.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <context:property-placeholder location="classpath:jdbc.properties"/>
  18.  
  19. <bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource">
  20. <property name="driverClassName" value="${jdbc.driver}"/>
  21. <property name="url" value="${jdbc.url}"/>
  22. <property name="username" value="${jdbc.user}"/>
  23. <property name="password" value="${jdbc.password}"/>
  24. </bean>
  25.  
  26. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  27. <property name="dataSource" ref="dataSource"/>
  28. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  29. <property name="mapperLocations" value="classpath:com/xj/mapper/*Mapper.xml"/>
  30. </bean>
  31. </beans>

6.在spring配置文件中配置MapperScannerConfigurer

作用:扫描dao层的接口类,将第五步动态生成的代理对象加入到ioc容器中。

  1.  
  1. ApplicationContext.xml
  1.  

  1. ?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <context:property-placeholder location="classpath:jdbc.properties"/>
  18.  
  19. <bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource">
  20. <property name="driverClassName" value="${jdbc.driver}"/>
  21. <property name="url" value="${jdbc.url}"/>
  22. <property name="username" value="${jdbc.user}"/>
  23. <property name="password" value="${jdbc.password}"/>
  24. </bean>
  25.  
  26. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  27. <property name="dataSource" ref="dataSource"/>
  28. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  29. <property name="mapperLocations" value="classpath:com/xj/mapper/*Mapper.xml"/>
  30. </bean>
  31.  
  32. <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  33. <property name="basePackage" value="com.xj.mapper"/>
  34. </bean>
  35. </beans>

相关依赖:

  1. <dependencies>
  2. <!--mybatis相关依赖-->
  3. <dependency>
  4. <groupId>org.mybatis</groupId>
  5. <artifactId>mybatis</artifactId>
  6. <version>3.4.5</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>5.1.6</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>druid</artifactId>
  16. <version>1.0.31</version>
  17. </dependency>
  18.  
  19. <!--单元测试-->
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.12</version>
  24. </dependency>
  25.  
  26. <!--spring相关依赖-->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-context</artifactId>
  30. <version>5.0.2.RELEASE</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-tx</artifactId>
  35. <version>5.0.2.RELEASE</version>
  36. </dependency>
  37.  
  38. <!--spring整合mybatis相关依赖-->
  39. <dependency>
  40. <groupId>org.mybatis</groupId>
  41. <artifactId>mybatis-spring</artifactId>
  42. <version>1.3.0</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-jdbc</artifactId>
  47. <version>5.0.2.RELEASE</version>
  48. </dependency>
  49. </dependencies>

二、基于xml的声明式事务配置

步骤:

1.配置自动扫描的包:为了把Service扫描到IOC容器中

  1. <context:component-scan base-package="com.xj.service"/>

2.配置事务管理器

  1. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  2. <property name="dataSource" ref="dataSource"/>
  3. </bean>

3.配置事务的通知

  1. <!-- 配置事务通知 -->
  2. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  3. <!-- 配置事务属性 -->
  4. <tx:attributes>
  5. <!-- 查询方法:配置只读属性,让数据库知道这是一个查询操作,能够进行一定优化 -->
  6. <tx:method name="find*" read-only="true"/>
  7. <!-- 增删改方法:配置事务传播行为、回滚异常 -->
  8. <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
  9. </tx:attributes>
  10. </tx:advice>
  1. propagation属性:
    REQUIRED:默认值,表示当前方法必须工作在事务中,如果当前线程上没有已经开启的事务,则自己开新事务。如果有已开启的事务,那么就使用这个已有的事务。
        顾虑:用别人的事务有可能“被”回滚。
  1. REQUIRES_NEW:建议使用的值,表示不管当前线程上有没有事务,都要自己开事务,在自己的事务中运行。
    好处:不会受到其他事务回滚的影响。

4.配置事务的切面

  1. <!-- 配置事务切面 -->
  2. <aop:config>
  3. <aop:pointcut id="txPointCut" expression="execution(* *..*ServiceImpl.*(..))"/>
  4. <!-- 将切入点表达式和事务通知关联起来 -->
  5. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
  6. </aop:config>

spring整合mybatis,ioc容器及声明式事务配置的更多相关文章

  1. Spring boot 入门五:springboot 开启声明式事务

    springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事务.这里以spring整合myb ...

  2. 【Spring】——声明式事务配置详解

    项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...

  3. spring学习笔记(22)声明式事务配置,readOnly无效写无异常

    在上一节内容中.我们使用了编程式方法来配置事务,这种优点是我们对每一个方法的控制性非常强.比方我须要用到什么事务,在什么位置假设出现异常须要回滚等.能够进行非常细粒度的配置.但在实际开发中.我们可能并 ...

  4. JAVAWEB 一一 框架整合(SSH,Spring+Struts2+Hibernate IOC/DI AOP声明式事务处理 定时任务)

    package org.springframework.orm.hibernate3; import java.io.Serializable; import java.util.List; impo ...

  5. Spring:(三) --常见数据源及声明式事务配置

    Spring自带了一组数据访问框架,集成了多种数据访问技术.无论我们是直接通过 JDBC 还是像Hibernate或Mybatis那样的框架实现数据持久化,Spring都可以为我们消除持久化代码中那些 ...

  6. Spring(四)-- JdbcTemplate、声明式事务

    1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils.   JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcT ...

  7. Spring Boot2 系列教程 (十) | 实现声明式事务

    前言 如题,今天介绍 SpringBoot 的 声明式事务. Spring 的事务机制 所有的数据访问技术都有事务处理机制,这些技术提供了 API 用于开启事务.提交事务来完成数据操作,或者在发生错误 ...

  8. Spring声明式事务配置详解

    Spring支持编程式事务管理和声明式的事务管理. 编程式事务管理 将事务管理代码嵌到业务方法中来控制事务的提交和回滚 缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码 声明式事务管理 一般情 ...

  9. 基于XML文档的声明式事务配置

    <!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.t ...

随机推荐

  1. IIS7.5配置对PHP的支持

    以下环境是 Windows server2008R2 IIS7.5 一般情况下,windows server系统默认是仅支持IIS+asp 或 IIS+aspx 的 搭配的,但是有时候我们的网站程序是 ...

  2. Windows核心编程 第25章 未处理异常和C ++异常(下)

    这一节东西比较少,本应该归并在上一节里,但是昨天太晚了.就先把那些东西分为上了.这节里面就一个问题,C++异常与结构性异常的对比(try和__try的区别): C++异常与结构性异常的对比 S E H ...

  3. 在网页添加 Live2D 看板娘

    只需要将以下代码粘贴到 标签中即可 <!--看板娘--> <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jqu ...

  4. 【Redis】redis异步消息队列+Spring自定义注解+AOP方式实现系统日志持久化

    说明: SSM项目中的每一个请求都需要进行日志记录操作.一般操作做的思路是:使用springAOP思想,对指定的方法进行拦截.拼装日志信息实体,然后持久化到数据库中.可是仔细想一下会发现:每次的客户端 ...

  5. 微信小程序中的加载更多(即列表分页)

    1.app.json中: "window": { "enablePullDownRefresh": true //是否开启当前页面下拉刷新 } 2.wxml中: ...

  6. mysql.data.entityframeworkcore 已弃用

    转官网有方案: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html General R ...

  7. 【BUAA软工】Visual Lab Online——功能规格说明书

    项目 内容 班级:北航2020春软件工程 博客园班级博客 作业:明确和撰写软件的功能规格说明书 功能规格说明书 当前版本:v1.0 修订历史: 版本号 修订时间 修订说明 v1.0 2020/04/0 ...

  8. 手写Spring MVC框架(二) 实现访问拦截功能

    前言 在上一篇文章中,我们手写了一个简单的mvc框架,今天我们要实现的功能点是:在Spring MVC框架基础上实现访问拦截功能. 先梳理一下需要实现的功能点: 搭建好Spring MVC基本框架: ...

  9. 面试侃集合 | LinkedBlockingQueue篇

    面试官:好了,聊完了ArrayBlockingQueue,我们接着说说LinkedBlockingQueue吧 Hydra:还真是不给人喘口气的机会,LinkedBlockingQueue是一个基于链 ...

  10. [Linux] Shell 脚本实例(超实用)

    文件操作 为文件(test.sh)增加执行权限 chmod +x test.sh 列出当前文件夹下所有文件(每行输出一个) 1 #!/bin/bash 2 dir=`ls ./` 3 for i in ...