springboot用yml配置jdbc数据源

 

要让那些黄色配置生效,要自己写一个配置类。

 

https://blog.csdn.net/justlpf/article/details/80728529详细解释

 

代码:

application.yml:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/lianxiTwo
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initiaSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECE 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    filters: stat,wall,log4j
    maxPoolPrepareStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

controller:

package com.lianxitwo.springbootjdbctwo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class Getjdbc {

    @Autowired
    JdbcTemplate jdbcTemplate;

   @ResponseBody
   @GetMapping("query")
    public Map<String, Object> map(){
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from classOne");
         return list.get(0);
    }


}

 

配置文件:

package com.lianxitwo.springbootjdbctwo.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.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

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

    @Bean
    public ServletRegistrationBean statViewServlet(){
       ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> map = new HashMap<>();
        map.put("loginUsername","admin");
        map.put("loginPassword","123456");
        map.put("allow","");
        map.put("deny","14.153.78.101");
          bean.setInitParameters(map);
          return bean;
    }

    @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;
    }


}
好的,下面是 SpringBoot 集成 sharding-jdbc-spring-boot-starter 并使用 yml 配置数据源的方式 进行 分表配置的步骤: 1. 首先,在 pom.xml 文件中添加 sharding-jdbc-spring-boot-starter 依赖,如下所示: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${shardingsphere.version}</version> </dependency> ``` 2. 在 application.yml 配置文件中添加数据源配置,如下所示: ```yml spring: datasource: # 主数据源 master: url: jdbc:mysql://localhost:3306/db_master?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8 username: root password: root # 从数据源 slave: url: jdbc:mysql://localhost:3306/db_slave?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8 username: root password: root # sharding-jdbc 配置 sharding: jdbc: #配置主从数据源名称 data-sources: # 配置数据源 master: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: ${spring.datasource.master.url} username: ${spring.datasource.master.username} password: ${spring.datasource.master.password} hikari: minimum-idle: 5 maximum-pool-size: 20 auto-commit: true idle-timeout: 30000 pool-name: master # 配置数据源 slave: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: ${spring.datasource.slave.url} username: ${spring.datasource.slave.username} password: ${spring.datasource.slave.password} hikari: minimum-idle: 5 maximum-pool-size: 20 auto-commit: true idle-timeout: 30000 pool-name: slave # 配置表规则 sharding: tables: # 配置分表规则 user: actual-data-nodes: master.user_${0..2} table-strategy: inline: sharding-column: id algorithm-expression: user_${id % 3} key-generator: type: SNOWFLAKE column: id props: worker-id: 123 # 配置读写分离规则 master-slave-rules: - name: ms master-data-source-name: master slave-data-source-names: slave ``` 3. 在代码中使用数据源,如下所示: ```java @Service public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; @Override public void addUser(User user) { String sql = "insert into user(id, name, age) values(?, ?, ?)"; Object[] params = new Object[]{user.getId(), user.getName(), user.getAge()}; jdbcTemplate.update(sql, params); } @Override public List<User> getUsers() { String sql = "select * from user"; List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); return userList; } } ``` 希望这些步骤能够帮助你集成 sharding-jdbc-spring-boot-starter 并使用 yml 配置数据源的方式进行分表配置。如果你有更多的问题,欢迎随时向我提问。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值