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 ...
随机推荐
- mysql union 组合查询
mysql> select * from test -> ; +----+------------+-------+-----------+ | id | name | score | s ...
- mysql 表联结,内部联结
mysql> select * from user; +------+----------+-----------+ | id | name | address | +------+------ ...
- vue on-change 如果有循环的timer 则无限自动执行
<div class="contract-class"> <Checkbox v-model="smallChecked" :on-chang ...
- Linux Shell:Map的用法
Map定义: 在使用map时,需要先声明,否则结果可能与预期不同,array可以不声明 方式1: declare -A myMap myMap[" 方式2: declare -A myMap ...
- openstack错误问题定位及调试
- Intellij idea 告警:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) 一.快捷键方式 鼠标移动到出错的的地方 ...
- typescript - 7.模块
我们可以把一些公共的功能单独抽离成一个文件作为一个模块. 模块里面的变量 函数 类等默认是私有的,如果我们要在外部访问模块里面的数据(变量.函数.类), 我们需要通过export暴露模块里面的数据(变 ...
- Android 动态更换桌面图标
每当双 11.12 来临之际,Android 手机 Launcher 中的淘宝.天猫图标就会变成双 11.12 主题的图标.实现了动态切换图标.名称 MainActivity package com. ...
- WebGL第一步
什么是WebGL? WebGL使用了GLSL ES(OpenGL ES)着色器语言,通过配合调用js相关的绘制接口来实现3D效果. 采用页面中的<canvas>元素来定义绘图区域,canv ...
- Qt 图片缩放参数计算
缩放图片 void VCImgWidget::wheelEvent(QWheelEvent *event) { ) { // 当滚轮远离使用者时 //ui->textEdit->zoomI ...