spring.datasource.schema配置详解(spring的datasource配置有哪些方式)

网友投稿 569 2022-07-27


目录1.现将sql文件放在resources下的sql文件夹下2.新建数据库mybatis3.配置yml

用springboot2.0执行sql脚本:

1.现将sql文件放在resources下的sql文件夹下

2.新建数据库mybatis

3.配置yml

spring:

datasource:

# 数据源基本配置

username: root

password: 123

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

type: com.alibaba.druid.pool.DruidDataSource

# 数据源其他配置

initialSize: 5

minIdle: 5

maxActive: 20

http:// maxWait: 60000

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: SELECT 1 FROM DUAL

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

# 配置监控统计拦截的filters,去掉后监控界面sql无http://法统计,'wall'用于防火墙

filters: stat,wall,log4j

maxPoolPreparedStatementPerConhttp://nectionSize: 20

useGlobalDataSourceStat: true

connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

schema:

- classpath:sql/department.sql

- classpath:sql/employee.sql

initialization-mode: ALWAYS

注意:

配置类:

package com.example.springbooy06datamybatis.config;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.support.http.StatViewServlet;

import com.alibaba.druid.support.http.WebStatFilter;

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 javax.sql.DataSource;

import java.util.Arrays;

import java.util.HashMap;

import java.util.Map;

@Configuration

public class DruidConfig {

@ConfigurationProperties(prefix = "spring.datasource")

@Bean

public DataSource druid(){

return new DruidDataSource();

}

//配置Druid的监控

//1、配置一个管理后台的Servlet

@Bean

public ServletRegistrationBean statViewServlet(){

ServletRegistrahttp://tionBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

Map initParams = new HashMap<>();

initParams.put("loginUsername","admin");

initParams.put("loginPassword","123456");

initParams.put("allow","");//默认就是允许所有访问

initParams.put("deny","192.168.15.21");

bean.setInitParameters(initParams);

return bean;

}

//2、配置一个web监控的filter

@Bean

public FilterRegistrationBean webStatFilter(){

FilterRegistrationBean bean = new FilterRegistrationBean();

bean.setFilter(new WebStatFilter());

Map initParams = new HashMap<>();

initParams.put("exclusions","*.js,*.css,/druid/*");

bean.setInitParameters(initParams);

bean.setUrlPatterns(Arrays.asList("/*"));

return bean;

}

}

执行成功!:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中如何快速构建项目脚手架的实现(搭建脚手架流程)
下一篇:springcloud中RabbitMQ死信队列与延迟交换机实现方法(rabbitmq宕机了怎么保证消息还能接收)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~