Spring Boot P 全能编程开发指南

一、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 配置文件的加载顺序如下:

  1. 命令行参数。
  2. 来自 SPRING_APPLICATION_JSON 的属性。
  3. JNDI 属性 from java:comp/env。
  4. Java 系统属性:
    • systemProperties
    • systemEnvironment
  5. OS 环境变量。
  6. ~/config/application.properties。
  7. ~/config/application.yml。
  8. classpath:/config/application.properties。
  9. classpath:/config/ application.yml。
  10. classpath:/application.properties。
  11. classpath:/application.yml。

七、SpringBoot 配置加载的方法有哪些

1、在 SpringBoot 开发过程中。常用的 SpringBoot 配置加载方法如下:

  1. 使用 @Value 注解为属性自动注入值
  2. 通过 @ConfigurationProperties 注解将配置文件内容自动注入 Bean
  3. 通过 Environment 注入值
  4. 通过 @PropertySource 注解注入值

八、Spring Boot P 配置多数据源

1、在 Spring Boot P 开发中,我们常用的多数据源配置方法有如下几种:

  1. 编写配置类,将不同数据源的连接信息封装成参数,注入到数据库链接池中
  2. 使用 Atomikos 分布式事务管理器
  3. 使用 HikariCp 数据库连接池
  4. 使用 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/n/150426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-08 14:53
下一篇 2024-11-08 14:53

相关推荐

  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • 运维Python和GO应用实践指南

    本文将从多个角度详细阐述运维Python和GO的实际应用,包括监控、管理、自动化、部署、持续集成等方面。 一、监控 运维中的监控是保证系统稳定性的重要手段。Python和GO都有强…

    编程 2025-04-29
  • Python wordcloud入门指南

    如何在Python中使用wordcloud库生成文字云? 一、安装和导入wordcloud库 在使用wordcloud前,需要保证库已经安装并导入: !pip install wo…

    编程 2025-04-29
  • Python应用程序的全面指南

    Python是一种功能强大而简单易学的编程语言,适用于多种应用场景。本篇文章将从多个方面介绍Python如何应用于开发应用程序。 一、Web应用程序 目前,基于Python的Web…

    编程 2025-04-29
  • Python中的while true:全能编程开发必知

    对于全能编程开发工程师而言,掌握Python语言是必不可少的技能之一。而在Python中,while true是一种十分重要的语句结构,本文将从多个方面对Python中的while…

    编程 2025-04-29
  • Python字符转列表指南

    Python是一个极为流行的脚本语言,在数据处理、数据分析、人工智能等领域广泛应用。在很多场景下需要将字符串转换为列表,以便于操作和处理,本篇文章将从多个方面对Python字符转列…

    编程 2025-04-29
  • Python小波分解入门指南

    本文将介绍Python小波分解的概念、基本原理和实现方法,帮助初学者掌握相关技能。 一、小波变换概述 小波分解是一种广泛应用于数字信号处理和图像处理的方法,可以将信号分解成多个具有…

    编程 2025-04-29
  • Spring Boot 集成 Jacoco

    本文将从以下几个方面介绍如何在 Spring Boot 中集成 Jacoco:1、Jacoco 概述;2、Spring Boot 集成 Jacoco 的配置;3、生成 Jacoco…

    编程 2025-04-29
  • Python初学者指南:第一个Python程序安装步骤

    在本篇指南中,我们将通过以下方式来详细讲解第一个Python程序安装步骤: Python的安装和环境配置 在命令行中编写和运行第一个Python程序 使用IDE编写和运行第一个Py…

    编程 2025-04-29
  • Python起笔落笔全能开发指南

    Python起笔落笔是指在编写Python代码时的编写习惯。一个好的起笔落笔习惯可以提高代码的可读性、可维护性和可扩展性,本文将从多个方面进行详细阐述。 一、变量命名 变量命名是起…

    编程 2025-04-29

发表回复

登录后才能评论