Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate @ DS annotations with springboot to realize data source switching

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how springboot integrates @ DS annotations to achieve data source switching". In daily operations, I believe many people have doubts about how springboot integrates @ DS annotations to achieve data source switching. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to integrate @ DS annotations to achieve data source switching". Next, please follow the editor to study!

Enable @ DS to realize data source switching add core jar package com.baomidou dynamic-datasource-spring-boot-starter 3.0.0 yml configuration spring: datasource: # configure hikari connection pool hikari: minimum-idle: 4 maximum-pool-size: 16 connection-timeout: 10000 idle-timeout: 30000 connection-init-sql: set Names utf8mb4 # dynamic data Source configuration dynamic: # Master data Source Primary: business datasource: # data source 1 business: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_business?useUnicode=true&characterEncoding=utf-8 username: * password: * # data source 2 user: driver-class-name is enabled by default : com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_user?useUnicode=true&characterEncoding=utf-8 username: * password: * # data source 3 order: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_order?useUnicode=true&characterEncoding=utf-8 Username: * password: * "Core"-use @ DS annotation

What is the core of using @ DS annotations?

1. Note addition is not valid on dao.mapper

two。 Comments added to the interface Service class are not valid

3. The annotation added to the interface Service method is invalid

So how should this annotation be used?

You can add @ DS annotations to the implementation class or to the methods of the implementation class

When annotations are added to a class, it means that all methods in this class use this data source

When annotations are added to a method, it means that the data source used on this method takes precedence over all other configurations

@ Service@DS ("slave") public class UserServiceImpl implements UserService {@ Autowired private JdbcTemplate jdbcTemplate; public List selectAll () {return jdbcTemplate.queryForList ("select * from user");} @ Override @ DS ("slave_1") public List selectByCondition () {return jdbcTemplate.queryForList ("select * from user where age > 10");} at this point, the study on "how springboot integrates @ DS annotations to achieve data source switching" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report