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. 17.3.12--time模块

    import time  #导入time模块 应用的时候有两种方式来表示时间: 1)时间戳 2)格式化的时间str(字符串)   3)元祖(struct_time)以及calendar 2---tim ...

  2. MySQL--启动和关闭MySQL服务

    1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql 2 ...

  3. 网页滚动条CSS样式

    滚动条样式主要涉及到如下CSS属性: overflow属性: 检索或设置当对象的内容超过其指定高度及宽度时如何显示内容 overflow: auto; 在需要时内容会自动添加滚动条overflow: ...

  4. 【iOS入门】UITableView加载图片

    学习带图片的列表 官方 LazyTableImages demo  http://download.csdn.net/detail/jlyidianyuan/5726749 分析源码是学习的好方法. ...

  5. python 输入输出 条件判断 循环

    1.条件判断 score = int(input("请输入学生成绩:"))if score>100 and score <0: print("请输入正确的成绩 ...

  6. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)报错

    0 环境 系统环境:win10 1 正文 先检查Mapper接口与相关联xml文件是否对应,需要检查包名,namespace位置是否写对,curd时id名称等能否对应上 常规步骤: :检查mapper ...

  7. ruoyi StringUtils

    package com.ruoyi.common.utils; import java.util.Collection; import java.util.Map; import com.ruoyi. ...

  8. PAT甲级——1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...

  9. 前端-HTLM

    前端简介: 什么是前端? 任何与用户直接打交道的操作界面都可以被称为前端,如:网页界面,手机界面.... 前端的学习历程和内容: 要学习的内容: 三大重点: 1.Web服务的本质: 浏览器中敲入网址回 ...

  10. [GX/GZOI2019]与或和(单调栈+按位运算)

    首先看到与或,很显然想到按照位拆分运算.然后就变成了0/1矩阵,要使矩阵在当前位与为1,则矩阵全为1,如果是或为1,则是矩阵不全为0,然后求全为0/1的矩阵个数即可.记录c[i][j]表示以a[i][ ...