spring Boot与Mybatis整合优化详解

SpringBoot官方文档http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

关于spring-boot与mybatis整合优化方面的介绍,就是Mybatis-Spring-boot-starter的介绍:

1、取消spring-mybatis.xml配置

①自动检测已存在的Datasource

之前,需要在spring-mybatis.xml中配置datasource的Bean,现在只需要在application.yml中配置到spring.datasource节点下就可以。因为mybatis-spring-boot支持自动检测已存在的Datasource。

②将创建并注册SqlSessionFactoryBean实例,并传入Datasource。

在mybatis中,sqlsession可以有SqlSessionFactory创建;而在mybatis-spring中则需要SqlSessionFactoryBean来创建,并传入datasource。

如:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="configLocation">
  <value>classpath:mybatis/mapper.xml</value>
 </property>
 <property name="dataSource" ref="dataSource" />
</bean> 

现在,mybatis-spring-boot支持自动创建并注册SqlSessionFactoryBean,所以以上的配置都不需要了。

③将从SqlSessionFactoryBean中创建并注册SqlSessionTemplate

SqlSessionTemplate是SqlSession的实现类,较SqlSession的默认实现类DefaultSqlSession来说,是线程安全的。

在mybatis-spring中需要如下配置:

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
 <constructor-arg index="0" ref="sqlSessionFactory" />
</bean> 

现在,mybatis-spring-boot支持自动创建并注册SqlSessionTemplate,所以不需要以上配置了。

SqlSession对象注入,如下:

@Autowired
private SqlSession sqlSession; 

::真不知道既然下面④都能注入mappers了,那还要SqlSession对象有什么用。。::

④自动扫描mappers,将其关联到SqlSessionTemplate,并将mappers注册到spring容器中,以便注入到我们的beans中。

默认情况下,mybatis-spring-boot将搜索被@Mapper注释标注的mappers。

文档中描述可以用mybatis-spring提供的@MapperScan标注,但我不会用。

Mybatis-Spring文档中解释@MapperScan注释跟配置MapperScannerConfigurer是同样的效果:

public @interface MapperScan Use this annotation to register MyBatis mapper interfaces when using Java Config. It performs when same work as MapperScannerConfigurer via MapperScannerRegistrar.
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.example.mappers" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> 

现在,mybatis-spring-boot支持使用@Mapper注释标注mappers接口类了,所以就不需要上述配置。

::其实感觉上述配置还是挺好的,不用每个mapper接口都注释@Mapper。。。::

@Mapper标注使用如下:

@Mapper
public interface UserMapper {
 UserInfo queryUser(@Param(value = "userId") int id);
}

那么在mybatis-spring-boot中需要配置的是mapper.xml目录:

mybatis:
 mapper-locations: classpath:mapper/*.xml 

总结

以上所述是小编给大家介绍的spring Boot与Mybatis整合优化详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Spring boot怎么整合Mybatis

    最近刚接触spring boot,正是因为他的及简配置方便开发,促使我下定决心要用它把之前写的项目重构,那么问题来了,spring boot怎么整合mybatis呢,下面几个配置类来搞定. 在我的代码当中是实现了数据库读写分离的,所以代码仅做参考,如有需要可以加我微信:benyzhous [后续更新] 1.文件结构 DataBaseConfiguration.Java用来获取数据库连接配置信息,配置从application.properties中读取 MybatisConfiguration.j

  • Spring Boot整合mybatis(一)实例代码

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot-entity,pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:s

  • 详解Spring Boot整合Mybatis实现 Druid多数据源配置

    一.多数据源的应用场景 目前,业界流行的数据操作框架是 Mybatis,那 Druid 是什么呢? Druid 是 Java 的数据库连接池组件.Druid 能够提供强大的监控和扩展功能.比如可以监控 SQL ,在监控业务可以查询慢查询 SQL 列表等.Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,能够提供基于 Filter-Chain 模式的插件体系. 2. DruidDataSource 高效可管理的数据库连接池 3. SQLParser 当业务数据量达

  • Spring Boot+Mybatis的整合过程

    依赖配置 结合前面的内容,这里我们要嵌入数据库的操作,这里以操作MySQL为例整合Mybatis,首先需要在原来的基础上添加以下依赖 <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1<

  • springboot与mybatis整合实例详解(完美融合)

    简介 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目.它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用.Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合.大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来. Spring Boot 包含的特性如下: 创建可以独立运行的 Spring 应用. 直接嵌入 Tomc

  • Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例

    前言 距离第一篇 Spring Boot 系列的博文 3 个月了.虽然 XML 形式是我比较推荐的,但是注解形式也是方便的.尤其一些小系统,快速的 CRUD 轻量级的系统. 这里感谢晓春 http://xchunzhao.tk/ 的 Pull Request,提供了 springboot-mybatis-annotation 的实现. 一.运行 springboot-mybatis-annotation 工程 然后Application 应用启动类的 main 函数,然后在浏览器访问: http

  • Spring Boot整合MyBatis操作过程

    1.加入mybatis-spring-boot-stater的Maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> 2.配置数据源 在src/main/re

  • spring Boot与Mybatis整合优化详解

    SpringBoot官方文档http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 关于spring-boot与mybatis整合优化方面的介绍,就是Mybatis-Spring-boot-starter的介绍: 1.取消spring-mybatis.xml配置 ①自动检测已存在的Datasource 之前,需要在spring-mybatis.xml中配置datasource的Bean,现在只需要在applicat

  • Spring Boot示例代码整合Redis详解

    目录 Redis 简介 Redis 优势 Redis与其他key-value存储有什么不同 添加Redis依赖包 配置Redis数据库连接 编写Redis操作工具类 测试 Redis 简介 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据

  • spring mvc 组合mybatis框架实例详解

    说明 本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回数据. 项目结构 包依赖 与说明 pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://

  • Spring boot Thymeleaf配置国际化页面详解

    目录 1.编写多语言国际化配置文件 2.编写配置文件 3.定制区域信息解析器 4.页面国际化使用 5.整合效果测试 1.编写多语言国际化配置文件 在项目的类路径resources下创建名称为i18n的文件夹,并在该文件夹中根据需要编写对应的多语言国际化文件login.properties.login_zh_CN.properties和login_en_US.properties文件 login.properties login.tip=请登录login.username=用户名login.pas

  • Spring boot admin 服务监控利器详解

    目录 一.简介 二.搭建 1.服务端 2.客户端 3.启动项目 4.客户端配置 3.微服务 3.1.服务端 3.2.客户端 4.我的微服务预警发送其他服务状态信息思路 一.简介 用于对 Spring Boot 应用的管理和监控.可以用来监控服务是否健康.是否在线.以及一些jvm数据等等.Spring Boot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用 http 通讯方式实现数据交

  • spring boot的maven配置依赖详解

    本文介绍了spring boot的maven配置依赖详解,分享给大家,具体如下: 我们通过引用spring-boot-starter-parent,添加spring-boot-starter-web 可以实现web项目的功能,当然不使用spring-boot-start-web,通过自己添加的依赖包也可以实现,但是需要一个个添加,费时费力,而且可能产生版本依赖冲突.我们来看下springboot的依赖配置: 利用pom的继承,一处声明,处处使用.在最顶级的spring-boot-dependen

  • spring boot + jpa + kotlin入门实例详解

    spring boot +jpa的文章网络上已经有不少,这里主要补充一下用kotlin来做. kotlin里面的data class来创建entity可以帮助我们减少不少的代码,比如现在这个User的Entity,这是Java版本的: @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String firstName; private S

  • spring boot(四)之thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎. thymeleaf介绍 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可

  • Spring boot跨域设置实例详解

    定义:跨域是指从一个域名的网页去请求另一个域名的资源 1.原由 公司内部有多个不同的子域,比如一个是location.company.com ,而应用是放在app.company.com , 这时想从 app.company.com去访问 location.company.com 的资源就属于跨域 本人是springboot菜鸟,但是做测试框架后端需要使用Springboot和前端对接,出现跨域问题,需要设置后端Response的Header.走了不少坑,在这总结一下以备以后使用 2.使用场景

随机推荐