SpringBoot--整合Mybatis+druid
分为两部分,首先替换默认数据源为阿里德鲁伊并添加监控,其次是SpringBoot下使用Mybatis
替换数据源为德鲁伊
首先在配置文件里配置好数据库连接的基本信息,如username password url等,重要的是把默认的type换成Druid。这样做数据源已经换成druid了,但是如果想完成对druid的定制化设置如设置initialSize,仅仅在yml里配置是不可以的,需要再实现一个配置类,并在这个配置类里实现后台监控的配置。
spring:
datasource:
username: root
password: password
url: jdbc:mysql://localhost:3306/javaweb
driver-class-name: com.mysql.jdbc.Driver
# initialization-mode: always
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
- 标注@Configuration表示这是一个注解,使用@Bean向容器里添加Bean,Bean的类型是DataSource
- 添加@ConfigurationProperties(prefix="spring.dataSource"),这样在yml里关于druid的配置才能生效
- 以向容器中添加Servlet和Filter的形式为druid添加后台监控,并添加相应的配置
@Configuration
public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
} //配置druid监控
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
HashMap<String, String> map = new HashMap<>();
map.put("loginUsername","admin");
map.put("loginPassword","123456");
bean.setInitParameters(map); return bean;
} @Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
bean.setFilter(new WebStatFilter());
HashMap<String, String> map = new HashMap<>();
map.put("exclusions","*.js");
bean.setInitParameters(map);
bean.setUrlPatterns(Arrays.asList("/*")); return bean;
} }
整合Mybatis
编写接口和接口实现类xml文件和普通使用Mybatis没有区别,关键在于让SpringBoot接口的实现类的位置,和Mybatis配置文件的位置。需要在yml里告诉SpringBoot具体的位置,其中config-location对应配置文件的位置,mapper-locations对应接口实现类的位置。为了使项目整洁,两个配置文件我都放在了resources/mybatis下。
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
SpringBoot--整合Mybatis+druid的更多相关文章
- springboot整合mybatis,druid,mybatis-generator插件完整版
一 springboot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SpringBoot整合MyBatis,HiKari、Druid连接池的使用
SpringBoot整合MyBatis 1.创建项目时勾选mybatis.数据库驱动. mysql驱动默认是8.x的版本,如果要使用5.x的版本,创建后到pom.xml中改. 也可以手动添加依赖 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- springboot整合mybatis出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...
- 【SpringBoot系列1】SpringBoot整合MyBatis
前言: 一直看网上说SpringBoot是解锁你的配置烦恼,一种超级快速开发的框架.一直挺想学的,正好最近也有时间,就学了下 这个是SpringBoot整合MyBatis的一个教程,用了阿里的drui ...
- SpringBoot整合Mybatis之进门篇
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
随机推荐
- I Count Two Three(打表+排序+二分查找)
I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...
- Balanced Ternary String(贪心+思维)
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...
- 使用ADO.NET
Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Centos分区/超过2T的磁盘
centos分区大于2TB 用parted分区工具分区 fdisk -l 查看要分的区(我这里是/dev/vdb) parted /dev/vdb #进入/dev/vdb进行分区 mktabl ...
- NIO Channel SocketChannel ServerSocketChannel
ServerSocketChannel: ServerSocketChannel是一个基于通道的socket监听器.它同我们所熟悉的java.net.ServerSocket执行相同的基本任务,不过它 ...
- crontab定时任务接入
# 查看 $ crontab -l # 创建 $ crontab -e # 每分钟输出一次当前时间 * * * * * echo `date` >> /demo.log # 查看定时 $ ...
- 【转】ANDROIDROM制作(一)——ROM结构介绍、精简和内置、一般刷机过程
作为对Rom制作的一个总结,本节主要介绍以下内容: 1.Rom介绍 2.Rom文件结构 3.app的精简与内置 4.Recovery简介 5.radio包简介 6.一般刷机过程.刷机过程中 ...
- Deep Reinforcement Learning with Iterative Shift for Visual Tracking
Deep Reinforcement Learning with Iterative Shift for Visual Tracking 2019-07-30 14:55:31 Paper: http ...
- Java与.net 关于URL Encode 的区别
在c#中,HttpUtility.UrlEncode("www+mzwu+com")编码结果为www%2bmzwu%2bcom,在和Java开发的平台做对接的时候,对方用用url编 ...
- np.meshgrid