Spring Boot 集成 Mybatis
原文: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的更多相关文章
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 详解Spring Boot集成MyBatis的开发流程
MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- spring boot集成mybatis(1)
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot集成mybatis(2) - 使用pagehelper实现分页
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot集成mybatis(3) - mybatis generator 配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot集成MyBatis 通用Mapper 使用总结
spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...
- 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 ...
- spring boot 集成 Mybatis,JPA
相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...
- spring boot集成mybatis分页插件
mybatis的分页插件能省事,本章记录的是 spring boot整合mybatis分页插件. 1.引入依赖 <!-- 分页插件pagehelper --> <dependency ...
随机推荐
- MFC基础程序设计VS2015 最新03
视频教程地址观看:http://pan.baidu.com/s/1mhKQ6kK 一.数字(浮点数或整数)转为文字:a)如果转为窄字符字符串,sprintf函数很好用,浮点数都没问题:b)如果转为宽字 ...
- VFP 实验参考答案
1. 使用for循环计算1到1000之间能既能被7整除也能被11整除的所有的数的和.(结果为6006)源程序代码 sum=0 i=1 for i=1 to 1000 if((i%7=0) and ...
- Java虚拟机原理
1.编译机制 分析和输入到符号表: 词法分析:将代码转化为token序列 语法分析:由token序列生成抽象语法树 输入到符号表:将类中出现的符号输入到类的符号表 注解处理: 处理用户自定义注解,之后 ...
- jenkins+ant+jmeter html报告文件作为附件发送(ant-jmeter支持javamail)
前言:由于ant-jmeter目前的版本不支持javamail,也就是说发送邮件时只能借助jenkins自带的发送邮件插件来发送报告. 但是jenkins发送邮件支持发送邮件内容(且有价值.有营养的内 ...
- 在host-only模式下ssh不插网线
visualbox在host-only模式下,宿主机可以在没有网络的条件下ssh虚拟机. 设置方法: 1.在visualbox中,选择全局设置(preference)--网络(network)-- h ...
- 微信小程序 获取OpenId
微信小程序 官方API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/ 首先 以下代码是 页面加载请求用户 是否同意授权 同意之后 用code 访问 微信 ...
- 【T-SQL】系列文章全文目录(2017-06-02更新)
本系列[T-SQL]主要是针对T-SQL的总结. T-SQL基础 [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...
- 发布高性能迷你React框架anu
anu, 读作[安努],原意为苏美尔的主神. anu是我继avalon之后又一个新框架(github仓库为https://github.com/RubyLouvre/anu, 欢迎加星与试用) 此框架 ...
- android 定时器(Handler Timer Thread AlarmManager CountDownTimer)
Android实现定时任务一般会使用以上(Handler Timer Thread AlarmManager CountDownTimer)五种方式.当然还有很多组合使用(比如Handler+Thre ...
- SQL Server使用导入导出向导导入超过4000个字符的字段的数据
在使用SQL Server导入导出向导导入数据的时候,我们经常会碰到某个单元格的数据超长而被截断报错的情况.本文针对这种场景给出相应的解决方案. 环境描述:SQL Server 2012,文件源: ...