14、《SpringBoot+MyBatis集成(2)——进阶配置XML与注解的灵活运用》


SpringBoot+MyBatis集成进阶配置 - XML与注解的灵活运用


前言

在Spring Boot与MyBatis的集成开发中,开发者常面临XML映射文件注解 两种SQL定义方式的选择,以及复杂场景下的动态SQL、多数据源等进阶需求。本文将从核心配置的灵活性出发,对比XML与注解的适用场景,详解动态SQL的实现技巧,并结合ResultMaptypeAliases等高级特性,最终通过多数据源配置实战演示企业级解决方案。无论你是希望优化现有项目,还是应对复杂业务逻辑,本文均能提供清晰的实践路径。


一、XML与注解:如何选择?
1. 注解的简洁与局限

通过@Select@Insert等注解直接在Mapper接口中编写SQL,适合简单、静态的SQL场景,例如:

java 复制代码
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(Long id);

优点

  • 代码简洁,无需维护XML文件。
  • 适合快速开发小型项目。

缺点**:

  • 复杂SQL可读性差(如动态条件、关联查询)。
  • 无法直接使用MyBatis的动态SQL标签。

2. XML的动态与强大

XML映射文件通过<if><foreach>等标签支持动态SQL,适合复杂查询和批量操作:

xml 复制代码
<select id="findUsers" resultType="User">
  SELECT * FROM user
  <where>
    <if test="name != null">AND name = #{name}</if>
    <if test="status != null">AND status = #{status}</if>
  </where>
</select>

优点

  • 动态SQL灵活,易于维护复杂逻辑。
  • 支持结果集映射(ResultMap)和关联查询。

缺点

  • 需额外维护XML文件,项目结构稍显复杂。

结论简单查询用注解,复杂逻辑用XML,二者可混合使用!


二、动态SQL实战:提升代码灵活性
1. 条件分支与循环
  • <if>标签:根据参数动态拼接条件。
  • <foreach>标签:实现批量插入或IN查询:
xml 复制代码
<insert id="batchInsert">
  INSERT INTO user (name, email) VALUES
  <foreach item="user" collection="list" separator=",">
    (#{user.name}, #{user.email})
  </foreach>
</insert>
2. 多表关联查询与ResultMap

通过ResultMap定义嵌套结果集,解决一对多关联查询:

xml 复制代码
<resultMap id="OrderWithItems" type="Order">
  <id property="id" column="order_id"/>
  <collection property="items" ofType="OrderItem">
    <id property="id" column="item_id"/>
    <result property="product" column="product_name"/>
  </collection>
</resultMap>

三、核心配置优化:typeAliases与mapperLocations
1. 简化实体类引用

application.yml中配置type-aliases-package,避免XML中写全类名:

yaml 复制代码
mybatis:
  type-aliases-package: com.example.entity
  mapper-locations: classpath:mapper/*.xml
2. 自动扫描Mapper接口

通过@MapperScan注解指定Mapper接口路径,避免逐个添加@Mapper

java 复制代码
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application { ... }

四、多数据源配置实战
场景需求

同时连接主库(写操作)从库(读操作),需独立配置数据源与MyBatis会话。

1. 主数据源配置
java 复制代码
@Configuration
@MapperScan(basePackages = "com.example.mapper.primary", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {

    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/primary/*.xml"));
        return bean.getObject();
    }
}
2. 从数据源配置
java 复制代码
@Configuration
@MapperScan(basePackages = "com.example.mapper.secondary", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/secondary/*.xml"));
        return bean.getObject();
    }
}
3. 配置文件(application.yml)
yaml 复制代码
spring:
  datasource:
    primary:
      url: jdbc:mysql://localhost:3306/primary_db
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
    secondary:
      url: jdbc:mysql://localhost:3306/secondary_db
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver

五、总结与最佳实践
  1. XML与注解混合使用:简单CRUD用注解,动态SQL和关联查询用XML。
  2. 动态SQL提升灵活性 :善用<if><foreach>处理复杂业务逻辑。
  3. 配置优化技巧type-aliases-package简化代码,mapperLocations规范路径。
  4. 多数据源方案 :通过独立配置DataSourceSqlSessionFactory实现读写分离。

避坑指南

  • 多数据源场景下,需明确指定@MapperScansqlSessionFactoryRef属性。
  • 避免在多数据源中重复扫描同一Mapper接口路径。

欢迎在评论区留言讨论你在MyBatis配置中遇到的难题!

相关推荐
用户908324602732 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
用户8307196840821 天前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞
spring boot·后端·rabbitmq
Java水解1 天前
Spring Boot 视图层与模板引擎
spring boot·后端
Java水解1 天前
一文搞懂 Spring Boot 默认数据库连接池 HikariCP
spring boot·后端
洋洋技术笔记1 天前
Spring Boot Web MVC配置详解
spring boot·后端
初次攀爬者2 天前
Kafka 基础介绍
spring boot·kafka·消息队列
用户8307196840822 天前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
Java水解2 天前
SpringBoot3全栈开发实战:从入门到精通的完整指南
spring boot·后端
初次攀爬者3 天前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺3 天前
搞懂@Autowired 与@Resuorce
java·spring boot·后端