spring boot-13.数据访问
1.spring boot 的自动配置提供的方便快捷的数据库操作服务,只需要进行少量配置即可连接数据库。spring boot 在org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration 配置了多种数据源
(1)org.apache.tomcat.jdbc.pool.DataSource
(2)com.zaxxer.hikari.HikariDataSource
(3)org.apache.commons.dbcp.BasicDataSource
(4)org.apache.commons.dbcp2.BasicDataSource
默认使用的是org.apache.tomcat.jdbc.pool.DataSource,除了这几种数据源之外我们也可以添加其它的数据源。这几个数据源之间通过全局配置文件的spring.datasource.type配置进行切换。
2.我们使用默认的方式来访问数据库
(1)在全局配置文件中配置数据库相关信息,就可以完成数据源的简单配置
spring.datasource.url=jdbc:mysql://localhost:3306/stock
spring.datasource.username=root
spring.datasource.password=1q2w3e
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
(2)接下来使用JdbcTemplate 来访问数据库
@Controller
public class EmpController {
@Autowired
JdbcTemplate jdbcTemplate; @GetMapping("/getEmp")
@ResponseBody
public List<Map<String, Object>> getEmp() {
List<Map<String, Object>> list =jdbcTemplate.queryForList("select * from stock_code");
return list;
}
}
(3)输出结果

3.在实际项目开发中往往不会使用默认的方式连接数据库,我们需要的是一些高性能的连接池,下面我们就来将默认的方式切换为使用阿里的druid连接池工具。
(1)修改配置文件中的数据源类型,并添加druid的其他配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/stock
username: root
password: 1q2w3e
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
(2)将druid数据源添加到容器中,并将配置属性值注入(关于配置属性注入可以参考第5篇的相关介绍)和配置druid的应用监控统计功能。
package com.springboot.config; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import javax.sql.DataSource; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter; @Configuration
public class DruidConfig { @ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druid() {
return new DruidDataSource();
} //配置druid监控管理管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<String,String>(); initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","192.168.1.251"); bean.setInitParameters(initParams);
return bean;
} //配置一个druid统计监控范围的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean;
}
}
(3)运行结果

durid监控管理后台

spring boot-13.数据访问的更多相关文章
- Spring Boot的数据访问:CrudRepository接口的使用
示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...
- (8)Spring Boot 与数据访问
文章目录 简介 整合基本的JDBC与数据源 整合 druid 数据源 整合 mybatis 简介 对于数据访问层,无论是 SQL 还是 NOSQL ,Spring Boot 默认都采用整合 Sprin ...
- Spring Boot框架 - 数据访问 - 整合Mybatis
一.新建Spring Boot项目 注意:创建的时候勾选Mybatis依赖,pom文件如下 <dependency> <groupId>org.mybatis.spring.b ...
- Spring Boot实现数据访问计数器
1.数据访问计数器 在Spring Boot项目中,有时需要数据访问计数器.大致有下列三种情形: 1)纯计数:如登录的密码错误计数,超过门限N次,则表示计数器满,此时可进行下一步处理,如锁定该账户 ...
- Spring Boot的数据访问 之Spring Boot + jpa的demo
1. 快速地创建一个项目,pom中选择如下 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- Spring Boot框架 - 数据访问 - JDBC&自动配置
一.新建Spring Boot 工程 特殊勾选数据库相关两个依赖 Mysql Driver — 数据库驱动 Spring Data JDBC 二.配置文件application.properties ...
- Spring MVC或Spring Boot配置默认访问页面不生效?
相信在开发项目过程中,设置默认访问页面应该都用过.但是有时候设置了却不起作用.你知道是什么原因吗?今天就来说说我遇到的问题. 首先说说配置默认访问页面有哪几种方式. 1.tomcat配置默认访问页面 ...
- Spring boot未授权访问造成的数据库外联
一.spring boot 日常测试或攻防演练中像shiro,fastjson等漏洞已经越来越少了,但是随着spring boot框架的广泛使用,spring boot带来的安全问题也越来越多,本文仅 ...
- Spring Boot与数据
SpringBoot 着眼于JavaEE! 不仅仅局限于 Mybatis .JDBC. Spring Data JPA Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数 ...
- Spring Boot 跨域访问
如何在 Spring Boot 中配置跨域访问呢? Spring Boot 提供了对 CORS 的支持,您可以实现WebMvcConfigurer 接口,重写addCorsMappings 方法来添加 ...
随机推荐
- k8s的一键分发秘钥 需要yum install expect
#下面的密码你改改就行了 我的机器也用的123456 ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa for i in k8s-1 k8s ...
- Angular 英雄示例教程
英雄指南教程(Tour of Heroes)涵盖了 Angular 的基本知识. 在本教程中,你将构建一个应用,来帮助人事代理机构来管理一群英雄. 这个入门级 app 包含很多数据驱动的应用所需的特性 ...
- 51 Nod 1134 最长递增子序列(经典问题回顾)
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元 ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串
A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...
- ASP.NET通过反射生成sql语句
最近对接一个接口,需要通过xml序列化成实体后添加额外信息后批量插入数据库,需要手动拼sql.因为涉及多张表,拼凑很麻烦而且容易出错,所以写了两个工具方法来生成sql,先写到博客里面,以便以后不时之需 ...
- 试用saucelabs进行浏览器兼容性测试
Hi,all 跟大家分享下saucelabs,一个云测试平台,支持PC和手机(自带的)浏览器的兼容性测试,并且支持selenium/appium的自动化测试,不过是收费的,价格还挺贵,但是人工的测试是 ...
- opencv_将图像上的4个点按逆时针排序
1:代码如下: #include "stdafx.h" #include "cxcore.h" #include "cvcam.h" #in ...
- 20175212童皓桢 实验四 Android程序设计
20175212童皓桢 实验四 Android程序设计 实验内容 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>并完成相关 ...
- ORCAD导网表:遇"No_connect" property
问题: Orcad Capture中将No Connect标识放置到了原本应该放置连线的管脚上,不知道怎么删除. 虽然添加一根Wire可以掩盖该管脚上已经添加的No Connect标识,但是到处网表的 ...
- Raspbian 编译安装 PHP 7.2
原文地址:Raspbian 编译安装 PHP 7.2 0x00 配置 开发板: Raspberry Pi 3B 系统: Raspbian 2019-04-08 stretch 0x01 下载源码 20 ...