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 introduce JdbcTemplate and Multi-data Source configuration in SpringBoot2

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

Share

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

This article will explain in detail how to introduce JdbcTemplate and multi-data source configuration in SpringBoot2. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Introduction of JdbcTemplate object 1 and JdbcTemplate

A case of configuring a data source and accessing a database through JdbcTemplate under the Spring Boot2.0 framework.

The operation of SpringBoot to the database is deeply encapsulated in jdbc. Using the injection function of spring, DataSource can be registered in JdbcTemplate.

2, JdbcTemplate core method 1) execute method: can be used to execute any SQL statement; 2) update method batchUpdate method: update method is used to execute new, modified, deleted and other statements; batchUpdate method is used to execute batch processing related statements; 3) query method and queryFor method: used to execute query related statements; 4) call method: used to execute stored procedures, function related statements. 2. Usage in SpringBoot2 1. Import Jar package mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-starter-jdbc2, Configuration data source information spring: application: # Application name name: node06-boot-jdbc datasource: # data Source 1: data_one Library primary: # 2.0 versions must be configured like this: jdbc-url: jdbc:mysql://localhost:3306/data_one # url: jdbc:mysql://localhost:3306/data_one username: root password: 123 driver-class -name: com.mysql.jdbc.Driver # data source 2: data_two library secondary: # 2.0 versions must be configured as follows: jdbc-url: jdbc:mysql://localhost:3306/data_two # url: jdbc:mysql://localhost:3306/data_two username: root password: 123 driver-class-name: com.mysql.jdbc.Driver3, Data source code configuration

1) configuration of data source 1

The @ Primary annotation indicates that the data source is the default master database.

/ * data source one configuration * / @ Configurationpublic class DataOneConfig {@ Primary / / main database @ Bean (name = "primaryDataSource") @ Qualifier ("primaryDataSource") @ ConfigurationProperties (prefix = "spring.datasource.primary") public DataSource primaryDataSource () {return DataSourceBuilder.create () .build () } @ Bean (name = "primaryJdbcTemplate") public JdbcTemplate primaryJdbcTemplate (@ Qualifier ("primaryDataSource") DataSource dataSource) {return new JdbcTemplate (dataSource);}}

2) data source second configuration

/ * data source second configuration * / @ Configurationpublic class DataTwoConfig {@ Bean (name = "secondaryDataSource") @ Qualifier ("secondaryDataSource") @ ConfigurationProperties (prefix= "spring.datasource.secondary") public DataSource secondaryDataSource () {return DataSourceBuilder.create (). Build ();} @ Bean (name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate (@ Qualifier ("secondaryDataSource") DataSource dataSource) {return new JdbcTemplate (dataSource) 4. Write a simple test class @ RestControllerpublic class JdbcController {private static final Logger LOG = LoggerFactory.getLogger (JdbcController.class); / / data source one @ Autowired @ Qualifier ("primaryJdbcTemplate") private JdbcTemplate primaryJdbcTemplate; / / data source two @ Autowired @ Qualifier ("secondaryJdbcTemplate") private JdbcTemplate secondaryJdbcTemplate / * multiple data source query * / @ RequestMapping ("/ queryData") public String queryData () {String sql = "SELECT COUNT (1) FROM d_phone"; Integer countOne= primaryJdbcTemplate.queryForObject (sql,Integer.class); Integer countTwo= secondaryJdbcTemplate.queryForObject (sql,Integer.class); LOG.info ("countOne==" + countOne+ "; countTwo==" + countTwo); return "SUCCESS" }} this is the end of the article on "how to introduce JdbcTemplate and multi-data source configuration in SpringBoot2". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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