一、整合jdbc

1、创建一个springInitializr项目

勾选 web————springweb、SQL————JDBC API,MYSQL Diver

2、连接数据库

3、创建yml

4、在

Springboot04DataApplication同级下创建controller包,再创建JDBCController类

5、启动

Springboot04DataApplication

二:整合Druid数据源
1、在pom.xml中加入相关依赖
<!--Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>

2、yml配置
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMills: 60000
minEvictableIdleTimeMidds: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true

#配置监控统计拦截好的filters,stat:监控统计、Log4j:日志记录、wall:防御sql注入
#如果允许时保报错 java.lang.ClassNotFountException: org.apache.log4j.Priority
#则导入log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.solwSqlMillis=500

3、在Springboot04DataApplication同级下创建config包创建DuridConfig类

@Configuration
public class DruidConfig {

@ConfigurationProperties(prefix ="spring.datasource")
@Bean
public DataSource DruidDataSource(){
return new DruidDataSource();
}

//后台监控
//因为SpringBoot 内置了 servlet容器,所以没有web.xml,替代方法:ServletRegistrationBean
@Bean
public ServletRegistrationBean StatViewServlet(){
ServletRegistrationBean<StatViewServlet> bean= new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

//后台需要有人登陆,账号密码配置
HashMap<String,String> initParameters=new HashMap<>();

//增加配置
initParameters.put("loginUsername","admin"); //登陆key 是固定的 loginUsername loginPassword
initParameters.put("loginPassword","123456");

//允许谁可以访问
initParameters.put("allow","");

//禁止谁能访问
//initParameters.put("","")

bean.setInitParameters(initParameters); //设置初始化参数
return bean;
}

//filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean=new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
//可以过滤那些请求呢?
Map<String,String> initParameters=new HashMap<>();

//这些东西不进行统计
initParameters.put("exclusions","*.js,**.css,/druid/*");

bean.setInitParameters(initParameters);
return bean;
}
}

4、运行Springboot04DataApplication

SpringBoot整合jdbc及整合Druid数据源的更多相关文章

  1. 【log4j】springboot项目启动 ,使用的druid数据源,log4j报错 log4j:WARN Please initialize the log4j system properly.

    springboot项目启动 ,使用的druid数据源,log4j报错 -- :: --- [ restartedMain] o.hibernate.annotations.common.Versio ...

  2. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  3. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  4. SpringBoot 整合jdbc和mybatis

    摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...

  5. Springboot学习07-数据源Druid

    Springboot学习07-数据源Druid 关键字 Druid 前言 学习笔记 正文 1-Druid是什么 Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器 ...

  6. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  7. 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕

    1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  8. springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置

    一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...

  9. Java之SpringBoot自定义配置与整合Druid

    Java之SpringBoot自定义配置与整合Druid SpringBoot配置文件 优先级 前面SpringBoot基础有提到,关于SpringBoot配置文件可以是properties或者是ya ...

随机推荐

  1. .Net 生成带注释的Nuget包

    使用.NET Core时,我们的类库都要打包成nuget包上传到nuget服务器,以供自己或他人使用 .Net sdk提供了一个 .Net pack 命令可以生成nuget包.比如下面的命令 dotn ...

  2. 怎样在Qt中建立使用动态链接库

    参考网址: https://blog.csdn.net/q496713258/article/details/6990837 qt 的学习网址: http://c.biancheng.net/view ...

  3. "排序二叉树"之探幽

    /*怎么理解排序二叉树呢?在二叉树的基本定义上增加两个基本条件: (1)所有左子树的节点数值都小于此节点的数值: (2)所有右节点的数值都大于此节点的数值. */ 1 /*************** ...

  4. 初识apache DBCP连接池

    连接案例: 首先:我们使用的是mysql数据库,所以要有一个mysql和java的JDBCjar包: 然后是DBCP中的两个jar包,DBCP使用的话,需要两个包: dbcp.jar和pool.jar ...

  5. Flink Streaming状态处理(Working with State)

    参考来源: https://www.jianshu.com/p/6ed0ef5e2b74 https://blog.csdn.net/Fenggms/article/details/102855159 ...

  6. unity优化 — 纹理(优化)通道分离

    unity针对Android平台还提供了通道分离的方式:将图片(sprite)压缩成ETC1,提取Alpha生成一张通道图.unity将通道图保存的格式为a8格式,目的为了让混合起来的Alpha效果很 ...

  7. Go测试--main测试

    目录 简介 示例 简介 子测试的一个方便之处在于可以让多个测试共享Setup和Tear-down.但这种程度的共享有时并不满足需求,有时希望在整个测试程序做一些全局的setup和Tear-down,这 ...

  8. go语言文件系统

    检测文件是否存在 //存在返回 true,不存在返回 false func fileIfExist(filename string) bool { _, err := os.Stat(filename ...

  9. HTB Hack The Box -- Oopsiec

    信息收集 开放了22ssh,80端口,其中ssh有弱口令爆破端口 先打开网页,然后进行目录爆破,在这期间先看一下网页的大概信息 没爆到什么有用的东西,但是有uploads文件夹说明是不是说明有文件上传 ...

  10. 第06课:GDB 常用命令详解(中)

    本课的核心内容: info 和 thread 命令 next.step.util.finish.return 和 jump 命令 info 和 thread 命令 在前面使用 info break 命 ...