In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is a detailed introduction to "Jspxcms supports multiple data sources". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "Jspxcms supports multiple data sources" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.
The first step is to determine what multiple data sources are.
If multiple data sources means that the tables in the system are placed in different databases (for example, column table cms_node is placed in database A, article table cms_info is placed in database B), this situation is not supported.
If the tables in the system are put into a database, but you also want to read some data from other databases through secondary development, this situation is OK.
The frameworks used in Jspxcms systems are spring-boot, spring-data-jpa. Essentially, support for multiple data sources is only relevant to these frameworks and not to the system itself. The official spring-boot documentation describes how to configure multiple data sources https://docs.spring.io/spring-boot/docs/1.5.20.RELEASE/reference/htmlsingle/#howto-two-datasources, and there are plenty of tutorials online.
Modify database connection configuration
The configuration file src/main/resources/application.properties.
Change the spring.datasource prefix of the default database configuration to app.datasource.first, and create a second data source app.datasource.second.
#spring.datasource.url=jdbc:mysql://localhost/jspxcms? characterEncoding=utf8#spring.datasource.username=root#spring.datasource.password=password#spring.datasource.driver-class-name=com.mysql.jdbc.Driverapp.datasource.first.url=jdbc:mysql://localhost/jspxcms? characterEncoding=utf8app.datasource.first.username=rootapp.datasource.first.password=passwordapp.datasource.first.driver-class-name=com.mysql.jdbc.Driverapp.datasource.second.url=jdbc:mysql://localhost/second_database? characterEncoding=utf8app.datasource.second.username=rootapp.datasource.second.password=passwordapp.datasource.second.driver-class-name=com.mysql.jdbc.Driver Add data source configuration code
Add the data source configuration code com.jspxcms.core.Application to the Java configuration file. The second data source uses JdbcTemplate to access data.
@Bean @Primary @ConfigurationProperties("app.datasource.first") public DataSourceProperties dataSourceProperties() { return new DataSourceProperties(); } @Bean @Primary @ConfigurationProperties("app.datasource.first") public DataSource dataSource() { return dataSourceProperties().initializeDataSourceBuilder().build(); } @Bean @ConfigurationProperties("app.datasource.second") public DataSourceProperties secondDataSourceProperties() { return new DataSourceProperties(); } @Bean @ConfigurationProperties("app.datasource.second") public DataSource secondDataSource() { return secondDataSourceProperties().initializeDataSourceBuilder().build(); } @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(secondDataSource()); } Examples of use
At this point, the configuration of multiple data sources is complete. Examples of usage are as follows:
@Controllerpublic class MyController { @GetMapping("/second_data_source") public String index(HttpServletRequest request, org.springframework.ui.Model modelMap) { List data = jdbcTemplate.queryForList("select * from my_table"); for (Map d : data) { System.out.println(d.get("my_field")); } } @Autowired private JdbcTemplate jdbcTemplate;} Read here, this article "Jspxcms support multiple data sources" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.