Springboot---配置jdbcTemplate的Datasource

spingboot使用jdbcTemplate可以便捷的访问数据库,配置访问方式如下

1.在配置文件中,写入数据库连接信息

application.properties配置如下

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

 2.创建Spring配置类,定义DataSource用来读取application.properties中的配置

@Configuration
public class DataSourceConfig {

    @Bean(name = "primaryDataSource")//命名这个datasource,用来区分不同的bean,比如多个数据库源
    @Qualifier("primaryDataSource")//@Autowired默认是根据类型进行注入的,因此如果有多个类型一样的Bean候选者,Qualifier则需要限定其中一个候选者,否则将抛出异常,@Qualifier限定描述符除了能根据名字进行注入,更能进行更细粒度的控制如何选择候选者
    @ConfigurationProperties(prefix="spring.datasource.primary")//读取前缀是什么的配置
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

3.在使用的时候注入datasource

@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
        @Qualifier("primaryDataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将一个druid-springboot-starter的springboot项目转换成使用shardingjdbc的springboot项目的步骤如下: 1. 在pom.xml文件中增加sharding-jdbc-spring-boot-starter和mysql-connector-java的依赖。 ```xml <dependency> <groupId>io.shardingjdbc</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${shardingjdbc.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> ``` 2. 在application.yml或application.properties中配置sharding-jdbc的数据源和分库分表规则。 ```yaml spring: shardingsphere: datasource: names: ds0, ds1 # 数据源名称 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root sharding: tables: t_order: actual-data-nodes: ds$->{0..1}.t_order_$->{0..1} table-strategy: inline: sharding-column: order_id algorithm-expression: t_order_$->{order_id % 2} key-generator: type: SNOWFLAKE column: order_id binding-tables: t_order default-database-strategy: inline: sharding-column: user_id algorithm-expression: ds$->{user_id % 2} default-table-strategy: none: ``` 3. 在代码中使用sharding-jdbc的数据源访问数据库。 ```java @Autowired private JdbcTemplate jdbcTemplate; public void query() { String sql = "select * from t_order where user_id = ?"; List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, 10); System.out.println(resultList); } ``` 通过以上步骤,就可以将一个druid-springboot-starter的springboot项目转换成使用shardingjdbc的springboot项目了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值