一、SpringBoot 配置多數據源 druid
1、SpringBoot 配置多數據源是非常常見的需求,為了高效地管理不同數據源的連接,我們可以使用 Druid 資料庫連接池。
@Configuration
public class DruidDBConfig {
private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);
@Autowired
private DruidProperties druidProperties;
@Bean(name="primaryDataSource")
@Primary
public DruidDataSource primaryDataSource() {
return initDataSource(druidProperties.getPrimary());
}
@Bean(name="secondaryDataSource")
public DruidDataSource secondaryDataSource() {
return initDataSource(druidProperties.getSecondary());
}
private DruidDataSource initDataSource(DruidProperties.DruidDataSourceProperties properties) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(properties.getUrl());
druidDataSource.setUsername(properties.getUsername());
druidDataSource.setPassword(properties.getPassword());
druidDataSource.setDriverClassName(properties.getDriverClassName());
druidDataSource.setInitialSize(properties.getInitialSize());
druidDataSource.setMinIdle(properties.getMinIdle());
druidDataSource.setMaxActive(properties.getMaxActive());
druidDataSource.setMaxWait(properties.getMaxWait());
return druidDataSource;
}
}
2、在 application.yml 中配置數據源信息
# DataSource - Primary spring.datasource.primary.url=jdbc:mysql://localhost:3306/primary_db spring.datasource.primary.username=root spring.datasource.primary.password=admin spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver # DataSource - Secondary spring.datasource.secondary.url=jdbc:mysql://localhost:3307/secondary_db spring.datasource.secondary.username=root spring.datasource.secondary.password=admin spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
二、SpringBoot 配置文件
1、SpringBoot 的配置文件 application.yml 使用 YAML 格式,簡單易讀,更容易維護。
2、YAML 文件定義方式比較靈活,支持各種數據類型,包括:標量(字元串,布爾值,數字),列表,映射等。
3、一個示例配置文件:
spring:
application:
name: MyApp
datasource:
url: jdbc:mysql://localhost:3306/my_app_db
username: root
password: admin
driver-class-name: com.mysql.jdbc.Driver
mail:
host: smtp.gmail.com
username: myapp@gmail.com
password: mydivxsammiyvocc
properties:
mail:
smtp:
auth: true
starttls:
enable: true
server:
port: 8080
三、SpringBoot 批量新增數據
1、在 SpringBoot 開發過程中,有時需要新增批量數據。使用 JDK8 Stream 語法,代碼更加簡潔高效。
@Service
public class BatchInsertService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void batchInsert(List<Student> students) {
String sql = "insert into student(name, age) values(?,?)";
int[][] args = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
preparedStatement.setString(1, students.get(i).getName());
preparedStatement.setInt(2, students.get(i).getAge());
}
@Override
public int getBatchSize() {
return students.size();
}
});
logger.info("Inserted {} students", args.length);
}
}
四、SpringBoot 配置文件密碼加密
1、在 SpringBoot 開發中,我們經常需要將資料庫連接的密碼存儲在配置文件中。但是,這可能會導緻密碼泄露。
2、SpringBoot 明文密碼加密方式,修改 application.yml 如下:
spring:
datasource:
username: myname
password: '{cipher}eb8cfcd691faff66f3218889698f1472cd43078d7673dffc84c5a754839f7429'
3、加密配置,修改 application.yml 如下:
encrypt:
key-store:
location: file:///${user.home}/.keystore
password: mysecretpassword
alias: mykey
secret: mysecretkey
五、SpringBoot 配置文件 project
1、SpringBoot 可以自動生成、引導和配置新項目。Spring Initializer 生成項目框架,並提供需要的依賴項列表。
2、http://start.spring.io
六、SpringBoot 配置文件載入順序詳解
1、SpringBoot 配置文件的載入順序如下:
- 命令行參數。
- 來自 SPRING_APPLICATION_JSON 的屬性。
- JNDI 屬性 from java:comp/env。
- Java 系統屬性:
- systemProperties
- systemEnvironment
- OS 環境變數。
- ~/config/application.properties。
- ~/config/application.yml。
- classpath:/config/application.properties。
- classpath:/config/ application.yml。
- classpath:/application.properties。
- classpath:/application.yml。
七、SpringBoot 配置載入的方法有哪些
1、在 SpringBoot 開發過程中。常用的 SpringBoot 配置載入方法如下:
- 使用 @Value 註解為屬性自動注入值
- 通過 @ConfigurationProperties 註解將配置文件內容自動注入 Bean
- 通過 Environment 注入值
- 通過 @PropertySource 註解注入值
八、Spring Boot P 配置多數據源
1、在 Spring Boot P 開發中,我們常用的多數據源配置方法有如下幾種:
- 編寫配置類,將不同數據源的連接信息封裝成參數,注入到資料庫鏈接池中
- 使用 Atomikos 分散式事務管理器
- 使用 HikariCp 資料庫連接池
- 使用 Spring Boot P 自帶多數據源配置方法
九、SpringBoot 配置文件路徑
1、SpringBoot 配置文件放在 src/main/resources 下,默認會被編譯到 classpath 路徑下。
2、在配置文件中,可以通過 ${} 獲取系統環境和變數。
spring:
profiles:
active: ${SPRING_PROFILE_ACTIVE:dev}
十、SpringBoot 配置類和配置文件選取
1、對於 SpringBoot 的配置,我們可以選擇 Java 配置類或者 YML/Properties 配置文件兩種方式。
2、通常情況下,我們選擇使用 YML/Properties 配置文件。
3、在某些特殊情況下,我們可能會選擇使用 Java 配置類,例如我們需要通過代碼來動態創建、修改配置項。
結語
以上就是 Spring Boot P 全能編程開發指南的介紹。Spring Boot P 是一個快速開發、簡化部署、輕量級的 Java Web 應用開發框架,它可以快速幫助我們構建出高效、安全、易於維護的應用程序。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/150426.html
微信掃一掃
支付寶掃一掃