原文:https://github.com/x113773/testall/issues/9

方式一:mybatis-spring-boot-starter
---
这种方式比较简单,具体步骤如下:
1. 首先添加依赖
```
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
```
2. application.properties添加如下配置
```
spring.datasource.url=jdbc:mysql://localhost:3306/testall?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123qwe
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapper-locations=classpath:/mybatis/*.xml
mybatis.type-aliases-package=com.ansel.testall.mybatis.model
```
3. 自动扫描mapper接口(或者也可以在每个mapper接口上添加@Mapper注解)
在Application.java上添加注解@MapperScan:
```
@SpringBootApplication
@MapperScan("com.ansel.testall.mybatis.mapper")
public class Application extends SpringBootServletInitializer {
...
```
然后就可以使用mybatis-generator生成mapper接口,mapper xml,model了,关于事务详见[这里](url)。

---
方式一解释:
其实mybatis-spring-boot-starter替我们做了大部分配置:(摘自[官方文档](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/))

> As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at least one mapper interface.
> MyBatis-Spring-Boot-Starter will:
> - Autodetect an existing DataSource.
> - Will create and register an instance of a SqlSessionFactory passing that DataSource as an input using the SqlSessionFactoryBean.
> - Will create and register an instance of a SqlSessionTemplate got out of the SqlSessionFactory.
> - Autoscan your mappers, link them to the SqlSessionTemplate and register them to Spring context so they can be injected into your beans.

大概翻译一下:
也许你早就知道,为了在spring上使用mybatis你至少需要一个SqlSessionFactory和一个mapper接口。
MyBatis-Spring-Boot-Starter会:
- 自动检测一个现有的DataSource(数据源)
- 通过把DataSource传送给SqlSessionFactoryBean,创建并注册一个SqlSessionFactory实例
- 使用 SqlSessionFactory 作为构造方法的参数来创建一个SqlSessionTemplate 实例
- 自动扫描mapper接口,与SqlSessionTemplate 连接,并它们注册到Spring上下文使它们可以注入到其他beans中。

如果使用方式二的话,就需要显示做出部分配置。

方式二:mybatis-spring
---
这种方式与传统的spring集成mybatis基本一致(下面展示完整的java配置版,部分xml版)

1. 还是首先添加依赖
```
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
```

2. application.properties添加如下配置
```
spring.datasource.url=jdbc:mysql://localhost:3306/testall?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123qwe
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```

3. 添加一个Mybatis的配置类MyBatisConfig.java,里面做的显示配置,基本与方式一中的自动配置一致:
```
package com.ansel.testall.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class MyBatisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new org.apache.tomcat.jdbc.pool.DataSource();
}

@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("com.ansel.testall.mybatis.model");

return sqlSessionFactoryBean.getObject();
}

@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}

/**
* 因为添加了spring-boot-starter-
* jdbc依赖,会触发DataSourceTransactionManagerAutoConfiguration这个自动化配置类
* ,自动构造事务管理器,所以若只有一个数据源可以不必进行下面的配置
*
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}

```

4. 添加一个MyBatisMapperScannerConfig.java类,把mapper扫描单独放在这里配置(或者使用方法一第3步中的注解方式也可以)

```
package com.ansel.testall.mybatis;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
// 由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解,而这个注解只能放在类上,所以...
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//因为只有一个sqlSessionFactory,所以下面这个可以不用设置
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.ansel.testall.mybatis.mapper");
return mapperScannerConfigurer;
}

}

```
---
部分xml版配置:
```
<!-- mapper自动扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ansel.testall.mybatis.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
```
```
<!-- 另外一种mapper自动扫描 -->
<mybatis:scan base-package="com.ansel.testall.mybatis.mapper" />
```

Spring Boot 集成 Mybatis的更多相关文章

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

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

  2. 详解Spring Boot集成MyBatis的开发流程

    MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...

  3. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  4. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. spring boot集成mybatis(2) - 使用pagehelper实现分页

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  6. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  8. spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete

    前言 spring boot集成mybatis时只生成两个sql, 搞了一个早上,终于找到原因了 找了很多办法都没有解决, 最后注意到生成sql的时候打印了一句话: Cannot obtain pri ...

  9. spring boot 集成 Mybatis,JPA

    相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...

  10. spring boot集成mybatis分页插件

    mybatis的分页插件能省事,本章记录的是 spring boot整合mybatis分页插件. 1.引入依赖 <!-- 分页插件pagehelper --> <dependency ...

随机推荐

  1. Linux(ubuntu)下jdk&tomcat的安装

    1.下载相应版本的jdk及tomcat:sudo wget ${url} 2.解压: tar zxvf jdk-7u79-linux-x64.tar.gz​ tar zxvf apache-tomca ...

  2. 《JavaScript高级程序设计》里对 call() 和 apply() 的解释 (116页)

    每个函数都包含两个非继承而来的方法:apply()和call().这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值. apply(): 方法接受两个参数:一个是在其 ...

  3. 一、 开篇(ASP.NET MVC5 系列)

    这个教程将教你一些用VS2013创建ASP.NET MVC 5 Web应用程序基础知识.为了避免开发工具的不一致而带来的一些小麻烦,建议你使用和我一样的开发工具VS2013英文版. 开发工具:Visu ...

  4. APP为什么需要着陆页

    小编APP运营团队一直认为,虽然大多数的用户会在App Store或安卓应用商店中搜寻我们的应用,但也有许多用户会通过传统的PC端搜索来寻找答案.而且在APP营销中,为了更好的将用户转换为下载或购买, ...

  5. Java学习之J2EE

    什么是J2EE  本文摘抄于其他博文. 什么是J2EE 一.准备篇 1 什么是J2EE?它和普通的Java有什么不同?答:J2EE全称为Java2 Platform Enterprise Editio ...

  6. javaSE_06Java中的数组(array)-练习

    (1),数组的两种遍历方式,顺序查找,二分查找,求最大最小数,冒泡排序,选择排序. public class Test1{ public static void main(String[] args) ...

  7. boost.property_tree读取中文乱码问题正确的解决方式

    开发项目的时候在使用boost,在宽字符下遇到中文乱码问题 上网上看大家都是先转成utf8在进行解析的,例如: http://blog.csdn.net/hu_jiangan/article/deta ...

  8. 基于angular实现模拟微信小程序swiper组件

    这段时间的主业是完成一个家政类小程序,终于是过审核发布了.不得不说微信的这个小程序生态还是颇有想法的,抛开他现有的一些问题不说,其提供的组件系统乍一看还是蛮酷的.比如其提供的一个叫swiper的视图组 ...

  9. undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

    参考https://github.com/wkh237/react-native-fetch-blob/issues/51 自己做了一下总结: 这个报错位置在react-native-fetch-bl ...

  10. TCP慢启动,拥塞控制,ECN 笔记

    TCP慢启动,拥塞控制,ECN 笔记 1,TCP慢启动 TCP在连接过程的三次握手完成后,开始传数据,并不是一开始向网络通道中发送大量的数据包,这样很容易导致网络中路由器缓存空间耗尽,从而发生拥塞:而 ...