代码结构:
- 导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<!--mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!--简化代码的工具包-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
</dependencies>
- 配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
- 创建实体类
package com.example.demo.inesa.entity;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data //lombok中的注解,自动生成getter&setter方法
@Component
//@TableName(value = "user")
public class User {
private Integer id;
private String name;
private Integer age;
private String email;
}
- 创建mapper
package com.example.demo.inesa.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.inesa.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper//表明这是一个Mapper,也可以在启动类上加上包扫描
public interface UserMapper extends BaseMapper<User> {
}
//继承BaseMapper可以省略xml的编写
- MyBatis Plus 提供了多种方式来进行关联表查询
5.1 使用 @TableField 注解和 selectJoin 方法,在实体类中使用 @TableField 注解标注关联字段,然后使用 selectJoin 方法进行关联查询。例如:
@TableName("user")
public class User {
@TableField(exist = false)
private List<Role> roles;
}
@TableName("role")
public class Role {
}
在查询时可以使用 selectJoin 方法:
userMapper.selectJoin(wrapper -> wrapper.eq("id", userId));
5.2使用 @TableField 注解和 @SqlParser 注解: 使用 @TableField 注解标注关联字段,并在查询方法上使用 @SqlParser 注解来指定关联查询的 SQL 片段。例如:
@TableName("user")
public class User {
@TableField(exist = false)
@SqlParser(filter = true)
private List<Role> roles;
}
在查询方法上使用 @SqlParser 注解:
@SqlParser(filter = true)
@Select("select u.*, r.* from user u left join role r on u.id = r.user_id where u.id = #{userId}")
User selectUserWithRoles(@Param("userId") Long userId);
5.3使用 XML 配置关联查询: 可以使用 MyBatis 的 XML 配置文件进行复杂的关联查询。在 XML 文件中定义关联查询的 SQL 语句,然后在 Mapper 接口中调用相应的方法。例如:
<!-- userMapper.xml -->
<select id="selectUserWithRoles" resultMap="userWithRoles">
select u.*, r.* from user u left join role r on u.id = r.user_id where u.id = #{userId}
</select>
在 Mapper 接口中调用方法:
User selectUserWithRoles(@Param("userId") Long userId);
- 配置多数据源
6.1导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot3-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.1.3</version>
</dependency>
6.2 配置文件
server:
port: 8660
spring:
datasource:
dynamic:
#设置默认的数据源或者数据源组,默认值即为master
primary: master
#严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/dante_cloud_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
slave_1:
url: jdbc:mysql://localhost:3306/dante_d?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
slave_2:
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: 123456
driver-class-name: org.postgresql.Driver
6.3 使用@DS(“**”)注解区分不同数据库
@GetMapping("/info")
@DS("slave_2")
public String getSimpleInfo() {
Info info = infoMapper.selectById(1);
System.out.println(info);
return "Hello, this is simple data from the server!";
}