Spring Boot 集成MyBatis

Spring Boot 系列

  1. Spring Boot 入门

  2. Spring Boot 属性配置和使用

  3. Spring Boot 集成MyBatis

  4. Spring Boot 静态资源处理

  5. Spring Boot - 配置排序依赖技巧

  6. Spring Boot - DevTools 介绍

Spring Boot 集成druid

druid有很多个配置选项,使用Spring Boot 的配置文件可以方便的配置druid

application.yml配置文件中写上:

  1. spring:

  2. datasource:

  3. name: test

  4. url: jdbc:mysql://192.168.16.137:3306/test

  5. username: root

  6. password:

  7. # 使用druid数据源

  8. type: com.alibaba.druid.pool.DruidDataSource

  9. driver-class-name: com.mysql.jdbc.Driver

  10. filters: stat

  11. maxActive: 20

  12. initialSize: 1

  13. maxWait: 60000

  14. minIdle: 1

  15. timeBetweenEvictionRunsMillis: 60000

  16. minEvictableIdleTimeMillis: 300000

  17. validationQuery: select 'x'

  18. testWhileIdle: true

  19. testOnBorrow: false

  20. testOnReturn: false

  21. poolPreparedStatements: true

  22. maxOpenPreparedStatements: 20

这里通过type: com.alibaba.druid.pool.DruidDataSource配置即可!

Spring Boot 集成MyBatis

Spring Boot 集成MyBatis有两种方式,一种简单的方式就是使用MyBatis官方提供的:

mybatis-spring-boot-starter

另外一种方式就是仍然用类似mybatis-spring的配置方式,这种方式需要自己写一些代码,但是可以很方便的控制MyBatis的各项配置。

一、mybatis-spring-boot-starter方式

pom.xml中添加依赖:

  1. <dependency>

  2. <groupId>org.mybatis.spring.boot</groupId>

  3. <artifactId>mybatis-spring-boot-starter</artifactId>

  4. <version>1.0.0</version>

  5. </dependency>

mybatis-spring-boot-starter依赖树如下: 

其中mybatis使用的3.3.0版本,可以通过: 
<mybatis.version>3.3.0</mybatis.version>属性修改默认版本。 
mybatis-spring使用版本1.2.3,可以通过: 
<mybatis-spring.version>1.2.3</mybatis-spring.version>修改默认版本。

application.yml中增加配置:

  1. mybatis:

  2. mapperLocations: classpath:mapper/*.xml

  3. typeAliasesPackage: tk.mapper.model

除了上面常见的两项配置,还有:

  • mybatis.config:mybatis-config.xml配置文件的路径
  • mybatis.typeHandlersPackage:扫描typeHandlers的包
  • mybatis.checkConfigLocation:检查配置文件是否存在
  • mybatis.executorType:设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE

二、mybatis-spring方式

这种方式和平常的用法比较接近。需要添加mybatis依赖和mybatis-spring依赖。

然后创建一个MyBatisConfig配置类:

  1. /**

  2. * MyBatis基础配置

  3. *

  4. * @author liuzh

  5. * @since 2015-12-19 10:11

  6. */

  7. @Configuration

  8. @EnableTransactionManagement

  9. public class MyBatisConfig implements TransactionManagementConfigurer {

  10. @Autowired

  11. DataSource dataSource;

  12. @Bean(name = "sqlSessionFactory")

  13. public SqlSessionFactory sqlSessionFactoryBean() {

  14. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

  15. bean.setDataSource(dataSource);

  16. bean.setTypeAliasesPackage("tk.mybatis.springboot.model");

  17. //分页插件

  18. PageHelper pageHelper = new PageHelper();

  19. Properties properties = new Properties();

  20. properties.setProperty("reasonable", "true");

  21. properties.setProperty("supportMethodsArguments", "true");

  22. properties.setProperty("returnPageInfo", "check");

  23. properties.setProperty("params", "count=countSql");

  24. pageHelper.setProperties(properties);

  25. //添加插件

  26. bean.setPlugins(new Interceptor[]{pageHelper});

  27. //添加XML目录

  28. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

  29. try {

  30. bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));

  31. return bean.getObject();

  32. } catch (Exception e) {

  33. e.printStackTrace();

  34. throw new RuntimeException(e);

  35. }

  36. }

  37. @Bean

  38. public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {

  39. return new SqlSessionTemplate(sqlSessionFactory);

  40. }

  41. @Bean

  42. @Override

  43. public PlatformTransactionManager annotationDrivenTransactionManager() {

  44. return new DataSourceTransactionManager(dataSource);

  45. }

  46. }

上面代码创建了一个SqlSessionFactory和一个SqlSessionTemplate,为了支持注解事务,增加了@EnableTransactionManagement注解,并且反回了一个PlatformTransactionManagerBean。

另外应该注意到这个配置中没有MapperScannerConfigurer,如果我们想要扫描MyBatis的Mapper接口,我们就需要配置这个类,这个配置我们需要单独放到一个类中。

  1. /**

  2. * MyBatis扫描接口

  3. *

  4. * @author liuzh

  5. * @since 2015-12-19 14:46

  6. */

  7. @Configuration

  8. //TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解

  9. @AutoConfigureAfter(MyBatisConfig.class)

  10. public class MyBatisMapperScannerConfig {

  11. @Bean

  12. public MapperScannerConfigurer mapperScannerConfigurer() {

  13. MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();

  14. mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");

  15. mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper");

  16. return mapperScannerConfigurer;

  17. }

  18. }

这个配置一定要注意@AutoConfigureAfter(MyBatisConfig.class),必须有这个配置,否则会有异常。原因就是这个类执行的比较早,由于sqlSessionFactory还不存在,后续执行出错。

做好上面配置以后就可以使用MyBatis了。

关于分页插件和通用Mapper集成

分页插件作为插件的例子在上面代码中有。

通用Mapper配置实际就是配置MapperScannerConfigurer的时候使用tk.mybatis.spring.mapper.MapperScannerConfigurer即可,配置属性使用Properties

Spring Boot集成MyBatis的基础项目

我上传到github一个采用第二种方式的集成项目,并且集成了分页插件和通用Mapper,项目包含了简单的配置和操作,仅作为参考。

项目地址:https://github.com/abel533/MyBatis-Spring-Boot

分页插件和通用Mapper的相关信息可以通过上面地址找到。

原文链接:http://blog.csdn.net/isea533/article/details/50359390

Spring Boot 集成MyBatis相关推荐

  1. Spring Boot 集成 Mybatis 实现双数据源

    转载自   Spring Boot 集成 Mybatis 实现双数据源 这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源 ...

  2. spring boot 集成Mybatis时 Invalid bound statement (not found)

    spring boot 集成Mybatis时,运行提示 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  3. Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper

    1. 概述 本文的内容包括如下内容: Spring Boot集成mybatis Spring Boot集成pagehelper分页插件,定义分页的相关类 实现工具类:model转dto,实现数据层和传 ...

  4. spring boot集成mybatis+事务控制

    一下代码为DEMO演示,采用注解的方式完成Spring boot和Mybatis的集成,并进行事物的控制 数据源的配置: 1 spring.datasource.url=jdbc:mysql://lo ...

  5. Spring Boot 集成 MyBatis 与 c3p0

    *对应的目录结构 一.添加依赖 <!-- 添加对 mybatis 的依赖 --><dependency><groupId>org.mybatis.spring.bo ...

  6. mybatis映射longtext类型数据_全网首例全栈实践(五)Spring Boot 集成Mybatis

    一.概述 我们的Spring Boot后续项目使用的都是MySQL.Spring Boot连接MySQL的方式包括JDBC,Spring JPA,Hibeirnate,Mybatis等,本文主要带大家 ...

  7. MBG真香 Spring Boot集成Mybatis Generator插件

    Mybatis中文官网对mybatis-generator的介绍:http://www.mybatis.cn/archives/885.html Mybatis官网对mybatis-generator ...

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

    文章共 509字,阅读大约需要 2分钟 ! 概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给 ...

  9. spring boot集成mybatis

    1.在pom中添加依赖: #mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> &l ...

最新文章

  1. ubuntu12.04 启动mysql_Ubuntu 12.04 MySQL改utf-8 启动不了
  2. 顶级项目管理工具 Top 10
  3. 长沙网络推广浅析如何增加网站的蜘蛛爬取频次?
  4. c++ websocket客户端_阿里面经WebSocket实时通信
  5. 关于推荐和机器学习的几个网站
  6. [LeetCode] Invert Binary Tree - 二叉树翻转系列问题
  7. [导入]关于OllyDbg 2.0的消息..
  8. [react] React中怎么操作虚拟DOM的Class属性
  9. switch java 语法_Java_基础语法之switch语句
  10. 2021年内衣品牌营销传播方案-婧麒+美柚.pdf(附下载链接)
  11. 计算机考研哈理工好吗,哈尔滨理工大学考研难吗?一般要什么水平才可以进入?...
  12. 实现微信小程序版本管理
  13. win10如何深度清理c盘【系统天地】
  14. 高手过招 放“码”出击 | 2022 Google 全球编程比赛集结倒计时!
  15. TP6使用session
  16. python用四个圆画成花_秘籍:学画牡丹技法要领,不轻易外传...
  17. android 带刻度的滑动条_Android实现滚动刻度尺效果
  18. 水箱建模最小二乘法_大气VOCs在线监测系统评估工作指南(二)
  19. Shell中判断字符串是否为数字的6种方法
  20. 显卡的指标有哪些方面_纯干货!显卡购买重要参数:老司机勿入

热门文章

  1. Linux 多线程应用中如何编写安全的信号处理函数
  2. Ubuntu12.04 搭建TFTP服务
  3. 嵌入式Linux系统编程学习之二十六多线程概述
  4. volatile的作用及原理
  5. 等待队列中为什么需要互斥锁?一个线程在等待时被唤醒后会做什么?安全队列的代码实现
  6. java 如何结束线程_java中,如何安全的结束一个正在运行的线程?
  7. 云端服务器怎么维护,云端服务器怎么维护
  8. python中tf.abs_python – Tensorflow:替换tf.nn.rnn_cell._linear(输入,大小,0,范围)
  9. ios 按钮图片拉伸_#UIButton#背景图片的拉伸
  10. 手机版计算机音乐,计算机音乐手机版