springboot3 configuration

1 多数据库配置

github: https://github.com/baomidou/dynamic-datasource
使用@DS()注解来切换数据库
详情介绍:https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611

注意:@DS 可以注解在方法上或上,同时存在就近原则 方法上注解 优先于 类上注解。

1.1 pom配置

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
    <version>${version}</version>
</dependency>

1.2 .propertis or .yml 配置

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # 内置加密,使用请查看详细文档
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
       #......省略
       #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2

1.3 代码中的额外配置@DS

使用多数据库时,需要在Mapper或Service的类或方法上,用注解@DS(“xxx”)指定所使用的数据库。

2 多模块配置

springboot多模块开发。其中pom文件和properties文件有如下关系:
(1)pom: 父子模块存在继承关系,子模块默认继承父模块所有依赖版本插件配置全局变量,除非子模块指定版本。(就和C++中父子类的继承一样。可以重载父类方法一样。子模块也可以重新指定依赖版本。)
(2)properties: 启动类不会主动关联从模块的properties,如果需要使能从模块的配置,需要在启动类的

除了上述两个文件外,还需要在主模块指定 包名 和 mapper。

2.1 pom

‌(1)基础继承‌
子模块通过标签声明父模块后,默认继承父POM中的:
< properties>定义的全局变量
< dependencyManagement>管理的依赖版本
< pluginManagement>定义的插件配置
‌(2)非自动继承内容‌
父模块中直接声明的< dependencies>和< build>配置‌不会自动继承‌到子模块,需子模块显式引用

(3)依赖版本覆盖‌
子模块声明相同依赖时:
若子模块指定版本号,则优先使用子模块版本17
未指定版本时继承父模块< dependencyManagement>中的版本25

‌(4)属性覆盖‌
子模块可重新定义< properties>中的值覆盖父模块配置

2.1.x 补充关于包名的

分模块编写时,我们一般会存在如下关系的pom.xml文件:
在这里插入图片描述

1 父pom

一般而言,父pom会定义如下:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/>
</parent>

<groupId>com.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<modules>
	<module>mydemo-master</module>
	<module>mydemo-display</module>
</modules>
...

注意: 如下部分时父pom自定义的(可以根据自己的需求更改。)

<groupId>com.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
2 子pom1
<parent>
<groupId>com.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>

<groupId>com.mydemo</groupId>
<artifactId>mydemo-master</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

注意:子pom1引入的 < parent> 就是父pom中自定义的那部分,需要完全一样。

<groupId>com.mydemo</groupId>
<artifactId>mydemo-master</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

而这一部分就是子pom自定义的,但是注意,groupId一定要与父pom完全一致。

3 子pom2
<parent>
<groupId>com.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>

<groupId>com.mydemo</groupId>
<artifactId>mydemo-display</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

与子pom2书写规则完全一致。

2.2 properties

(1)配置隔离性‌
Spring Boot默认不会自动加载父模块的application.yml或application.properties文件,每个子模块的配置文件是独立生效的。

‌(2)优先级规则‌
子模块的resources目录下的配置文件优先级高于父模块,且同目录下.properties会覆盖.yml的配置

(3)父模块通过指定名称引入子模块的配置:
多个子模块定义application.properties时,打包后仅主模块配置生效
将数据库连接等公共配置放在主模块的application.properties中,其他子模块的配置文件需要命名为application-{profile}.properties。在主模块中通过如下配置激活子模块的配置:(注意:profile可以是任意名称)

# 激活dev环境
spring.profiles.active=profile

2.3 启动类

无论有多少个模块,启动类只能有一个。
如果其他子模块没有正常启动,需要添加注解:

@springBootApplication(sanBasePackages={"com.mydemo")
public class WebApplication{
	public static void main(String[] args){
		springApplication.run(WebApplication.class, args);
	}
}

注意,这里"com.mydemo"是子模块的包路径。也就是我们在父子pom中,指定的 < groupId >
如果子模块的包路径和启动模块的包路径完全一致,则不添加该注解也可正常启动。

2.4 MapperScan

多模块开发时,还需要在启动类前加上注解@MapperScan

@MppaerScan("com.mydemo.example.mapper")
public class WebApplication{
	public static void main(String[] args){
		springApplication.run(WebApplication.class, args);
	}
}

这里,com.mydemo.example.mapper下,存放着我的Mapper文件(有@Mapper注解的类。@MapperScan会扫描该包及其子包)

3 Mybatisplus配置

Mybatisplus兼容Mybatis,因此,依赖引入是仅引用Mybatisplus即可。

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.12</version>
</dependency>

3.1 注解的语句用法(参数传递)

可以直接使用@Select、@Results等注解,在方法上,而无需配置xml文件。
这其中,需要注意参数的传递。
(1)#{param} 占位符
这类会视为变量,预编译阶段会认为是 ? 。

(2)${param} 字符串
该类参数在预编译阶段会被字节按照字符串替换。

mybatis注解的配置
https://blog.csdn.net/qq_45297578/article/details/121677970

3.2 sql查询结果与自定义类的映射

通常来说,我们会准备一个类来接受sql查询返回的结果。
有3种方法可以实现数据映射:
(0)类属性名与sql表的列名自动匹配(完全一致)
(1)在类定义中加上@Table的注解指定
(2)在mapper方法上加上@Results注解

@Results({
    @Result(property = "userName", column = "user_name"),
    @Result(property = "homeTown", column = "home_town")
})
@Select("SELECT * FROM user")
List<User> selectUsers();

这里,property指定的是类User的属性,column指定的是sql查询语句返回结果的列名。

tu

%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3BentryX%3D0%3BentryY%3D0.5%3BentryDx%3D0%3BentryDy%3D0%3B%22%20edge%3D%221%22%20source%3D%223%22%20target%3D%228%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%223%22%20value%3D%22mydemo-master%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1500%22%20y%3D%22-280%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%224%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3BentryX%3D0%3BentryY%3D0.5%3BentryDx%3D0%3BentryDy%3D0%3B%22%20edge%3D%221%22%20source%3D%225%22%20target%3D%227%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%225%22%20value%3D%22mydemo-display%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1500%22%20y%3D%22-120%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%226%22%20value%3D%22pom.xml%26lt%3Bdiv%26gt%3B(%E7%88%B6pom)%26lt%3B%2Fdiv%26gt%3B%22%20style%3D%22rounded%3D1%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1500%22%20y%3D%2240%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%227%22%20value%3D%22pom.xml%26lt%3Bdiv%26gt%3B(%E5%AD%90pom2)%26lt%3B%2Fdiv%26gt%3B%22%20style%3D%22rounded%3D1%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1400%22%20y%3D%22-40%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%228%22%20value%3D%22pom.xml%26lt%3Bdiv%26gt%3B(%E5%AD%90pom1)%26lt%3B%2Fdiv%26gt%3B%22%20style%3D%22rounded%3D1%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1400%22%20y%3D%22-210%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%229%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3BentryX%3D0%3BentryY%3D0.5%3BentryDx%3D0%3BentryDy%3D0%3BexitX%3D0.5%3BexitY%3D1%3BexitDx%3D0%3BexitDy%3D0%3B%22%20edge%3D%221%22%20source%3D%2212%22%20target%3D%223%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2210%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3BexitX%3D0.5%3BexitY%3D1%3BexitDx%3D0%3BexitDy%3D0%3BentryX%3D0%3BentryY%3D0.5%3BentryDx%3D0%3BentryDy%3D0%3B%22%20edge%3D%221%22%20source%3D%2212%22%20target%3D%225%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2211%22%20style%3D%22edgeStyle%3DorthogonalEdgeStyle%3Brounded%3D0%3BorthogonalLoop%3D1%3BjettySize%3Dauto%3Bhtml%3D1%3BexitX%3D0.5%3BexitY%3D1%3BexitDx%3D0%3BexitDy%3D0%3BentryX%3D0%3BentryY%3D0.5%3BentryDx%3D0%3BentryDy%3D0%3B%22%20edge%3D%221%22%20source%3D%2212%22%20target%3D%226%22%20parent%3D%221%22%3E%3CmxGeometry%20relative%3D%221%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3CmxCell%20id%3D%2212%22%20value%3D%22MyProject%22%20style%3D%22rounded%3D0%3BwhiteSpace%3Dwrap%3Bhtml%3D1%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%22-1640%22%20y%3D%22-360%22%20width%3D%22120%22%20height%3D%2260%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值