mybatis-spring的集成

  源码下载(数据库使用derby,具体数据库结构参考这里

  在src下新建applicationContext.xml

  配置内容:数据源、SqlSessionFactory、Mapper、事务处理

一、配置SqlSessionFactory

  需要两个参数:数据源和Mybatis的配置文件路径

  这样SpringIOC容器就会初始化这个SqlSessionFactoryBean,解析Mybatis配置文件连同数据源一同保存在SpringBean里

    <!-- 配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
</bean> <!-- 配置SQLSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

  jdbc.properties

driver=org.apache.derby.jdbc.ClientDriver
url=jdbc:derby://localhost:1527/E:/my/derby/mydb

  在spring中已经初始化数据源,所以在mybatis配置文件中就无需再进行配置

  mybatis.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 每种属性按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>

  有些书中介绍了配置SqlSessionTemplate,当同时设置了SqlSessionFactory和SqlSessionTemplate,系统就会用SqlSessionTemplate覆盖掉SqlSessionFactory,相比较而言,笔者还是喜欢采用Mapper接口的编程方式。

二、配置mapper(这里采用自动扫描的形式)

  采用MapperScannerConfigurer配置,属性:

    basePackage:指定让Spring自动扫描的包

    annotationClass:表示如果类被这个注解标识的时候才进行扫描

    sqlSessionFactoryBeanName:指定在spring中定义sqlSessionFactory的bean名称。如果被定义,sqlSessionFactory将不起作用

    sqlSessionTemplateBeanName:指定在spring中定义sqlSessionTemplate的bean名称。如果被定义,sqlSessionFactoryBeanName将不起作用

    markerInterface:指定是实现了什么接口就认为它是mapper

 <!-- 接口方式 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
//在sqlSessionFactory中加载映射文件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 加载映射文件 -->
<property name="mapperLocations" value="classpath*:/com/example/mapper/*Mapper.xml"></property>
</bean>

三、配置事务

    <!--  配置jdbc事务管理器,完成数据的完整性和一致性 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用声明式事务管理方式 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

  配置service层

    <!-- 启动spring注解 -->
<context:annotation-config/>
<!-- 扫描注解所在的包 -->
<context:component-scan base-package="com.example"/>

  在service业务层中使用@Transaction注解便可开启事务,事务使用见上篇

附:别人写的SqlSessionFactoryBean

-------end-------

(五)mybatis-spring的集成的更多相关文章

  1. SSM框架开发web项目系列(五) Spring集成MyBatis

    前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...

  2. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...

  3. Spring同时集成JPA与Mybatis

    @ 目录 ORM Spring ORM Spring ORM 同时集成JPA与Mybatis 一.创建一个SpringBoot项目 二.建立用户信息登记表 三.Web应用项目集成mysql 四.添加S ...

  4. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二

    接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...

  5. Spring Boot 集成 Mybatis

    原文:https://github.com/x113773/testall/issues/9 方式一:mybatis-spring-boot-starter---这种方式比较简单,具体步骤如下:1. ...

  6. SSM学习(二)mybatis和spring的集成

    上一篇文章大概搭建了一下ssm的框架,其实还是不完整,我们往项目中添加了spring和mybatis的配置文件,还差一个spring mvc的配置文件,在resource中在新建一个Applicati ...

  7. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  8. Spring Boot集成MyBatis的2种方式

    目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默 ...

  9. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  10. Spring Boot 集成 MyBatis和 SQL Server实践

    概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...

随机推荐

  1. JunOS SRX firewal Web authentication(转)

    转载自:https://srxasa.wordpress.com/2011/12/11/junos-srx-firewal-web-authentication/ JunOS SRX firewal ...

  2. Codeforces Round #555 (Div. 3) B. Long Number 【仔细读题】

    B. Long Number time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. [原]排错实战——使用process explorer替换任务管理器

    原Aha总结注册表process explorersysinternalsprocess monitor 前言 一般,我们会使用任务管理器查看系统中有哪些进程正在运行,强制杀掉某个进程.可是系统自带的 ...

  4. Java clone方法的使用

    浅克隆 Person p2 = (Person) p1.clone(); clone()方法使用后得到p2,p2和p1指向不同的地址.但是如果p1中的属性是引用类型,那么不再对这个引用类型进行复制,而 ...

  5. cygwin下命令行下切换目录

    比我们正常切换目录多个挂载的文件夹 cygdrive

  6. dubbo使用Spring配置暴露服务和使用Spring配置引用远程服务

    提供者: <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-servic ...

  7. E、阔力梯的树

    题:https://ac.nowcoder.com/acm/contest/4010/E?&headNav=acm 分析:dsu.贪心方法:考虑插入一个值x,对总体贡献,若查找在序列中左边有值 ...

  8. UML-设想

    样例:

  9. maven 设置pom 指定jdk版本

    <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</acti ...

  10. vue中axios的post请求使用form表单格式发送数据

    vue使用插件qs实现 (qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库.) 在jquery中的ajax的方法已将此封装,所以不需要再次序列化 1. 安装   在项目中使用命令行工具输 ...