In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
In real projects, we often have different deployment environments, such as dev database, system test database and production database, so how to deploy the same spring boot web app to different database environments?
Spring boot provides the function of a profile, which enables the same application to switch to different deployment environments by configuring multiple profile files. The concept of profile is not covered in detail here. If you are interested, go to the official website to inquire. Here is how to configure spring boot to enable spring JDBC tempalte to switch between different jdbc data sources through code.
1 introduce necessary dependencies in pom.xml
Org.springframework.boot spring-boot-starter-jdbc org.springframework spring-jdbc com.oracle ojdbc6 11.2.0.3
Note: I am using oracle jdbc here.
2 create different spring boot configuration files, these three file subtables represent dev,system,production
Application.propertiesapplication-sys.propertiesapplication-prod.properties
Note that application- {profile} corresponds to different environments. You can switch different profiles through java-jar-Dspring.profile.active=sys
3 configure different jdbc information to the corresponding configuration file
In the application.properties file, spring.datasource.url=jdbc:oracle:thin:@dev01.example.com:1521:lausonedspring.datasource.username=devUserspring.datasource.password=XXXXXspring.datasource.driver-class-name=oracle.jdbc.OracleDriver in application-sys.propertiesspring.datasource.url=jdbc:oracle:thin:@sys01.example.com:1521:lausonedspring.datasource.username=sysUserspring.datasource.password=XXXXXspring.datasource.driver-class-name=oracle.jdbc.OracleDriver in application-prod.propertiesspring.datasource.url=jdbc:oracle:thin:@prodv01.example.com:1521:lausonedspring.datasource. Username=prodUserspring.datasource.password=XXXXXspring.datasource.driver-class-name=oracle.jdbc.OracleDriver
4 configure spring boot
@ Configurationpublic class DataBaseConfig {@ Bean (name= "asuDBsource") @ ConfigurationProperties (prefix= "spring.datasource") public DataSource primaryDataSource () {return DataSourceBuilder.create () .build ();} @ Bean (name= "asuJDBC") @ Autowired public JdbcTemplate blcJdbcTemplate (@ Qualifier ("asuDBsource") DataSource source) {return new JdbcTemplate (source) }}
Here I created a DataBaseConfig.java file to configure jdbc to connect to the database and return the data source, and configure the data source to return jdbcTemplate. I use traditional jdbc to connect to the database and do not use hibernate.
5. Call JDBCTemplate. 5 through dependency injection (DI).
@ Repositorypublic class AsuDaoImpl implements IAsuDao {@ Autowired @ Qualifier (value= "asuJDBC") private JdbcTemplate jdbcTemplate;}
Here I create a dao interface, and dao Impl to inject this jdbcTemplate.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.