一,使用spring boot脚手架搭建spring boot框架生成maven项目

如下图所示:

设置自定义的坐标,即左侧的Group和Artifact,右侧可以搜索添加一些依赖,搜索不到的可以在pom文件中手动添加,本文需要的依赖如下:

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.3.1</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <scope>runtime</scope>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>druid</artifactId>
  16. <version>1.0.25</version>
  17. </dependency>
  18. <!-- 分页插件 -->
  19.  
  20. <dependency>
  21. <groupId>com.github.pagehelper</groupId>
  22. <artifactId>pagehelper</artifactId>
  23. <version>4.1.6</version>
  24. </dependency>

引入所需依赖jar包后,就可以开始集成mybatis和druid了。

二,集成druid

本文采用properties文件的形式进行配置,根据自己习惯,亦可选用yml文件进行相关配置。

1.在application.properties写入以下配置:

  1. #主数据库的配置
  2. #spring.datasource.name = test 多数据源时可配
  3. #spring.datasource.type = com.alibaba.druid.pool.DruidDatasource
  4. spring.datasource.driverClassName = com.mysql.jdbc.Driver
  5. spring.datasource.url = jdbc:mysql://IP:port/数据库名称?useUnicode=true&amp;characterEncoding=utf8&amp;allowMultiQueries=true&amp;autoReconnect=true
  6. spring.datasource.username = ***
  7. spring.datasource.password = ***
  8.  
  9. #连接池的补充设置
  10. #初始化、最小、最大
  11. spring.datasource.initialSize = 1
  12. spring.datasource.minIdle = 1
  13. spring.datasource.maxActive = 20
  14. #获取连接等待超时的时间、毫秒(1m)
  15. spring.datasource.maxWait = 60000
  16. #检测关闭空闲连接的间隔时间、毫秒(1m),当空闲连接大于(minEvictableIdleTimeMillis),则关闭物理连接
  17. spring.datasource.timeBetweenEvictionRunsMillis = 60000
  18. #一个连接在池中最小的生存时间、毫秒(5m)
  19. spring.datasource.minEvictableIdleTimeMillis = 300000
  20. #监控统计拦截的filters,去掉后监控界面sql无法统计,'wall用于防火墙','log4j'用于日志
  21. spring.datasource.druid.sys.filters = stat,wall,log4j
  22. #用于检测连接是否有效的语句
  23. spring.datasource.validationQuery=SELECT 'x'
  24. #检测连接的超时时间、秒
  25. spring.datasource.validationQueryTimeout = 3
  26. #申请连接时,空闲时间大于(timeBetweenEvictionRunsMillis),则检测连接的有效性
  27. spring.datasource.testWhileIdle = true
  28. #申请连接时,检测连接的有效性(性能损耗)
  29. spring.datasource.testOnBorrow = false
  30. #归还连接时,检测连接的有效性(性能损耗)
  31. spring.datasource.testOnReturn = false

有关druid的配置信息,可移步Druid查看,本文不再赘述。

2.编写DruidConfig,如下:

  1. import java.util.Properties;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.Primary;
  9. import org.springframework.core.env.Environment;
  10.  
  11. import com.alibaba.druid.pool.DruidDataSourceFactory;
  12.  
  13. @Configuration
  14. public class DruidConfig {
  15. //配置文件
  16. @Autowired
  17. private Environment env;
  18. @Bean
  19. //默认为主数据源
  20. @Primary
  21. public DataSource getDataSource() throws Exception{
         //此处不推荐使用实例化一个DruidDataSource的方式,进行数据源的配置,采用DruidDataSourceFactory的方式创建DataSource实例,原理分析可查看设计模式之工厂模式。
  22. Properties properties = new Properties();
  23. properties.put("driverClassName", env.getProperty("spring.datasource.driverClassName"));
  24. properties.put("url", env.getProperty("spring.datasource.url"));
  25. properties.put("username", env.getProperty("spring.datasource.username"));
  26. properties.put("password", env.getProperty("spring.datasource.password"));
  27. properties.put("initialSize", env.getProperty("spring.datasource.initialSize"));
  28. properties.put("minIdle", env.getProperty("spring.datasource.minIdle"));
  29. properties.put("maxActive", env.getProperty("spring.datasource.maxActive"));
  30. properties.put("maxWait", env.getProperty("spring.datasource.maxWait"));
  31. properties.put("timeBetweenEvictionRunsMillis", env.getProperty("spring.datasource.timeBetweenEvictionRunsMillis"));
  32. properties.put("minEvictableIdleTimeMillis", env.getProperty("spring.datasource.minEvictableIdleTimeMillis"));
  33. properties.put("validationQuery", env.getProperty("spring.datasource.validationQuery"));
  34. properties.put("filters", env.getProperty("spring.datasource.druid.sys.filters"));
  35. properties.put("validationQueryTimeout", env.getProperty("spring.datasource.validationQueryTimeout"));
  36. properties.put("testWhileIdle", env.getProperty("spring.datasource.testWhileIdle"));
  37. properties.put("testOnBorrow", env.getProperty("spring.datasource.testOnBorrow"));
  38. properties.put("testOnReturn", env.getProperty("spring.datasource.testOnReturn"));
  39. return DruidDataSourceFactory.createDataSource(properties);
  40. }
  41. }

druid集成完毕。

三,集成mybatis

1.在application.properties中写入以下配置

  1. # mybatis_config
  2. # mapper.xml的文件地址
  3. mybatis.mapperLocations=classpath:mybatis/mapper/*.xml
  4. mybatis.typeAliasesPackage=****

2.编写MybatisConfig,如下:

  1. import java.util.Properties;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.apache.ibatis.plugin.Interceptor;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.mybatis.spring.SqlSessionFactoryBean;
  8. import org.mybatis.spring.SqlSessionTemplate;
  9. import org.mybatis.spring.annotation.MapperScan;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.transaction.PlatformTransactionManager;
  15. import org.springframework.transaction.annotation.EnableTransactionManagement;
  16. import org.springframework.transaction.annotation.TransactionManagementConfigurer;
  17. import org.springframework.core.env.Environment;
  18. import org.springframework.core.io.Resource;
  19. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  20. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  21.  
  22. import com.github.pagehelper.PageHelper;
  23.  
  24. @Configuration
  25. @AutoConfigureAfter({DruidConfig.class})
  26. //扫描dao层,basePackages 为dao层所在路径,支持通配符*,多个以,分隔
  27. @MapperScan(basePackages = "")
  28. @EnableTransactionManagement
  29. public class MyBatisConfig implements TransactionManagementConfigurer{
  30. @Autowired
  31. //配置文件
  32. private Environment env;
  33. @Autowired
  34. //默认为配置文件中的数据源
  35. DataSource dataSource;
  36.  
  37. //根据数据源创建sqlSessionFactory
  38. @Bean
  39. public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception{
  40. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  41. //指定数据源
  42. factoryBean.setDataSource(dataSource);
  43. //指定封装类所在包
  44. factoryBean.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));
  45. //指定mapper.xml文件所在
  46. Resource[] resource = new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations"));
  47. factoryBean.setMapperLocations(resource);
  48.  
  49. //添加分页插件
  50. PageHelper pageHelper = new PageHelper();
  51. Properties properties = new Properties();
  52. properties.setProperty("reasonable", "true");
  53. properties.setProperty("supportMethodsArguments", "true");
  54. properties.setProperty("returnPageInfo", "check");
  55. properties.setProperty("params", "count=countSql");
  56. pageHelper.setProperties(properties);
  57. factoryBean.setPlugins(new Interceptor[]{pageHelper});
  58. return factoryBean;
  59. }
  60.  
  61. @Bean
  62. public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
  63. return new SqlSessionTemplate(sqlSessionFactory);
  64. }
  65.  
  66. @Bean
  67. @Override
  68. public PlatformTransactionManager annotationDrivenTransactionManager() {
  69. return new DataSourceTransactionManager(dataSource);
  70. }
  71.  
  72. }

至此,集成完毕。

spring boot +mysql + mybatis + druid的整理(一)——单数据源的更多相关文章

  1. 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?

    Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...

  2. Spring Boot系列(三):Spring Boot整合Mybatis源码解析

    一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...

  3. Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...

  4. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  5. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  6. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  7. Spring boot之MyBatis

    文章目录1. 环境依赖2. 数据源2.1. 方案一 使用 Spring Boot 默认配置2.2. 方案二 手动创建3. 脚本初始化4. MyBatis整合4.1. 方案一 通过注解的方式4.1.1. ...

  8. spring boot集成mybatis(1)

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

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

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

随机推荐

  1. mysql优化专题」90%程序员都会忽略的增删改优化(2)

    补充知识点:操作数据语句优化的认识 通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作). ...

  2. idea 配置热部署

    1. 2.

  3. JAVA9模块化详解(二)——模块的使用

    JAVA9模块化详解(二)--模块的使用 二.模块的使用 各自的模块可以在模块工件中定义,要么就是在编译期或者运行期嵌入的环境中.为了提供可靠的配置和强健的封装性,在分块的模块系统中利用他们,必须确定 ...

  4. VR、AR、MR定义区别

    近日, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR.MR ...

  5. SQL Server AlwaysOn添加监听器失败

    标签:MSSQL/ 一.错误描述 1.群集服务未能使群集服务或应用程序“Alwayson22”完全联机或脱机.一个或多个资源可能处于失败状态.这可能会影响群集服务或应用程序的可用性 2.群集服务中的群 ...

  6. HTML干货

    什么也不想说 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...

  7. Spring拦截器总结

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 Spring过滤器WebFilter可以配置中文过滤 拦截器实现步骤 ...

  8. xcode编译报错unknown error -1=ffffffffffffffff Command /bin/sh failed with exit code 1

    升级完xcode9.1之后,编译项目出现如下错误: CI今日构建时报出如下错误: /Users/xxx/Library/Developer/Xcode/DerivedData/Snowball-ebl ...

  9. Docker(三):Docker仓库配置

    1.仓库介绍 仓库(repository)用来集中管理Docker镜像,支持镜像分发和更新. 目前世界上最大最知名的公共仓库是Docker官方的Docker Hub,国内比较知名的有:Docker P ...

  10. ES6 函数的扩展2

    8.2 rest参数 ES6引入rest参数(形式为"-变量名"),用于获取函数的多余参数,这样就不需要使用arguments对象了. arguments对象并没有数组的方法,re ...