mybatis springboot(长文解析)

更新时间:

💡一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

截止目前, 星球 内专栏累计输出 90w+ 字,讲解图 3441+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 3100+ 小伙伴加入学习 ,欢迎点击围观

在现代 Java 开发领域,mybatis springboot 的组合已成为主流技术栈之一。它结合了 Spring Boot 的快速开发能力与 MyBatis 的灵活性,为开发者提供了一站式解决方案。对于编程初学者而言,理解这一组合能快速掌握企业级应用的开发模式;对于中级开发者,深入其底层原理和最佳实践则能显著提升代码质量与系统性能。本文将通过循序渐进的方式,从环境搭建到高级特性,结合实际案例展开讲解,帮助读者构建扎实的知识体系。


一、环境搭建与基础配置

1.1 项目初始化

在 Spring Boot 中集成 MyBatis 需要先添加相关依赖。通过 Maven 的 pom.xml 文件引入以下核心依赖:

<dependencies>
    <!-- Spring Boot 核心 starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <!-- MyBatis-Spring-Boot 启动器 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    
    <!-- 数据库驱动,以 MySQL 为例 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
</dependencies>

1.2 数据库连接配置

application.properties 文件中配置数据库信息:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
  • mapper-locations 指定了 XML 映射文件的路径。
  • type-aliases-package 允许通过类名直接引用实体类,无需全限定名。

二、核心概念解析

2.1 MyBatis 的核心组件

2.1.1 SqlSession

可以将其想象为数据库的“翻译官”。它负责执行 SQL 语句、提交事务,并返回结果。在 Spring Boot 中,SqlSession 由框架自动管理,开发者无需直接操作。

2.1.2 Mapper 接口与 XML 映射

Mapper 接口定义了数据库操作的方法,而 XML 文件则具体描述 SQL 语句。例如,定义一个 UserMapper 接口:

public interface UserMapper {
    User selectById(Long id);
    int insert(User user);
}

对应的 XML 文件 UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
    <select id="selectById" resultType="User">
        SELECT * FROM user WHERE id = #{id}
    </select>
    
    <insert id="insert">
        INSERT INTO user (name, age) VALUES (#{name}, #{age})
    </insert>
</mapper>

2.2 Spring Boot 与 MyBatis 的集成原理

Spring Boot 通过 @MapperScan 注解自动扫描 Mapper 接口,并将其注册为 Spring Bean。例如在启动类中添加:

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

这一过程类似于“快递分拣系统”——框架自动识别并分配 Mapper 接口到对应的服务层,减少手动配置的复杂度。


三、CRUD 操作实战

3.1 创建(Create)

以用户注册为例,实体类 User

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
}

在 Mapper 接口中定义插入方法:

int insert(User user);

执行后,MyBatis 会根据 XML 中的 INSERT 语句将数据写入数据库。

3.2 查询(Read)

3.2.1 单条查询

User selectById(Long id);

对应的 SQL:

<select id="selectById" resultType="User">
    SELECT * FROM user WHERE id = #{id}
</select>

3.2.2 多条查询

List<User> selectAll();

对应的 SQL:

<select id="selectAll" resultType="User">
    SELECT * FROM user
</select>

3.3 更新(Update)

int update(User user);

对应的 SQL:

<update id="update">
    UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}
</update>

3.4 删除(Delete)

int deleteById(Long id);

对应的 SQL:

<delete id="deleteById">
    DELETE FROM user WHERE id = #{id}
</delete>

四、高级特性与优化

4.1 动态 SQL

MyBatis 提供了 <if><choose> 等标签,实现 SQL 的动态拼接。例如模糊查询:

<select id="selectByName" resultType="User">
    SELECT * FROM user 
    WHERE 1=1
    <if test="name != null">
        AND name LIKE CONCAT('%', #{name}, '%')
    </if>
</select>

这类似于“智能拼图”,根据输入参数动态组合 SQL 语句。

4.2 分页查询

可通过 RowBounds 或直接在 SQL 中使用 LIMIT

List<User> selectByPage(int offset, int limit);

对应的 SQL:

<select id="selectByPage" resultType="User">
    SELECT * FROM user LIMIT #{offset}, #{limit}
</select>

4.3 关联查询与结果映射

4.3.1 一对一关联

假设 User 关联 Address,可定义 resultMap

<resultMap id="userResultMap" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="address" javaType="Address">
        <result property="street" column="street"/>
    </association>
</resultMap>

4.3.2 多对一关联

通过 <resultMap> 实现逆向关联,例如 Address 关联 User

<resultMap id="addressResultMap" type="Address">
    <id property="id" column="id"/>
    <result property="street" column="street"/>
    <association property="user" javaType="User">
        <result property="id" column="user_id"/>
    </association>
</resultMap>

4.4 缓存机制

4.4.1 一级缓存(本地缓存)

默认开启,作用域为 SqlSession 生命周期内,避免重复查询同一数据。

4.4.2 二级缓存(跨 SqlSession)

通过 <cache> 标签启用:

<cache/>

需注意:实体类需实现 Serializable 接口。


五、最佳实践与注意事项

5.1 命名规范

  • 表名与实体类名:遵循 snake_case(如 user_info)与 CamelCase(如 UserInfo)的映射关系。
  • 方法命名:采用 selectByXxxupdateXxx 等前缀,提升代码可读性。

5.2 事务管理

通过 @Transactional 注解控制事务边界:

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    
    @Transactional
    public void transferMoney(Long fromId, Long toId, Integer amount) {
        // 事务内的操作
        userMapper.updateBalance(fromId, -amount);
        userMapper.updateBalance(toId, amount);
    }
}

5.3 性能优化

  • 延迟加载:在关联查询中使用 lazyLoadingEnabled="true"
  • 批量操作:通过 @Options 注解配置批量插入:
    @Insert("INSERT INTO user (name, age) VALUES (#{name}, #{age})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void batchInsert(@Param("users") List<User> users);
    

5.4 异常处理

捕获 PersistenceException 并封装业务异常:

try {
    userMapper.insert(user);
} catch (PersistenceException e) {
    throw new CustomException("插入用户失败", e);
}

六、总结

通过本文的讲解,读者应能掌握 mybatis springboot 的核心功能与实践技巧。从基础的 CRUD 到动态 SQL、关联查询,再到事务与缓存优化,每一环节都体现了 MyBatis 的灵活性与 Spring Boot 的简洁性。对于初学者,建议从简单案例入手,逐步深入高级特性;中级开发者则可结合实际项目,探索分页插件、代码生成器等扩展工具,进一步提升开发效率。

在后续学习中,可关注 MyBatis-Plus 这类增强框架,它能进一步简化开发流程,但本文的核心原理仍为其底层逻辑的基础。掌握这一组合,将为构建高可用、高性能的 Java 应用奠定坚实基础。

最新发布