springboot数据访问层 数据源切换

数据源切换druid

1.加坐标

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druid-spring-boot-starterartifactId>
    <version>1.2.8version>
dependency>
2.做配置
2.1 配置类中注入datasource
@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource bruidSource(){
        return new DruidDataSource();
    }
2.2 yml文件中设置druid数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT
    username: root 
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource # 切换druid数据源

yaml文件加密

1.添加坐标


    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    2.1.0

2.启动类添加注解
@SpringBootApplication
@EnableEncryptableProperties #加密注解
public class SpringBootDatasourceDruidApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDatasourceDruidApplication.class, args);
    }
}
3.通过测试类生成密码
@Test
void show2() {
    //1.获取加密对象
    StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
    //2.配置加密的算法
    EnvironmentPBEConfig config = new EnvironmentPBEConfig();
    //加密算法,这个算法是默认的
    config.setAlgorithm("PBEWithMD5AndDES");
    //加密的密钥
    config.setPassword("apesource");
    standardPBEStringEncryptor.setConfig(config);

    //自己的密码
    String name = "root";
    String passwd = "123456";
    
    //生成密文
    String encryptname = standardPBEStringEncryptor.encrypt(name);
    String encryptpwd = standardPBEStringEncryptor.encrypt(passwd);

    System.out.println(encryptname);
    System.out.println(encryptpwd);
}
    //生成的密文
//    uw12GfYMqt4JN6tvxxV85A==
//    zoYTVRd3WBzt+bx6TYEGbQ==
4.配置yaml文件
# 1.修改账号密码为密文
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT
    username: ENC(uw12GfYMqt4JN6tvxxV85A==)  # ENC()函数解密
    password: ENC(zoYTVRd3WBzt+bx6TYEGbQ==)
    type: com.alibaba.druid.pool.DruidDataSource # 切换druid数据源

# 2.添加密钥(解密的密钥)
jasypt:
  encryptor:
    password: apesource

druid监控平台

1.添加坐标

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druid-spring-boot-starterartifactId>
    <version>1.2.8version>
dependency>
2.编写yaml配置
spring:
  datasource:
    filters: stat,wall #添加拦截器
3.编写配置类
3.1注入数据源
@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource bruidSource(){
        return new DruidDataSource();
    }
3.2配置servlet制作监控页面
@Bean
public ServletRegistrationBean myServletRegistrationBean(){
    //1.创建RegistrationBean对象
    ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<StatViewServlet>();
    //2.创建Servlet对象
    StatViewServlet statViewServlet = new StatViewServlet();
    //3.添加访问路径
    registrationBean.setServlet(statViewServlet);
    registrationBean.setUrlMappings(Arrays.asList("/druid/*"));
    //4.参数绑定
    Map<String,String> maps = new HashMap<String,String>();
    maps.put(StatViewServlet.PARAM_NAME_USERNAME,"admin");
    maps.put(StatViewServlet.PARAM_NAME_PASSWORD,"123");
    maps.put(StatViewServlet.PARAM_NAME_ALLOW,"");//白名单
    maps.put(StatViewServlet.PARAM_NAME_DENY,"192.168.0.12");//黑名单
    registrationBean.setInitParameters(maps);
    return registrationBean;
}

3.3配置filter编写过滤规则
@Bean
public FilterRegistrationBean myFilterRegistrationBean(){
    //1.创建RegistrationBean对象
    FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
    //2.创建Filter对象
    WebStatFilter webStatFilter = new WebStatFilter();
    //3.添加拦截规则
    filterRegistrationBean.setFilter(webStatFilter);
    filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
    //4.设置白名单
    Map<String, String> initPrams = new HashMap<>();
    //添加不需要忽略的格式信息
    initPrams.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*");
    filterRegistrationBean.setInitParameters(initPrams);

    return filterRegistrationBean;
}
3.4 输出结果

springboot数据访问层 数据源切换_第1张图片```

你可能感兴趣的:(spring,boot,后端,java)