Springboot基础篇(5):自定义 MyBatis Starter

在 Spring Boot 生态中,Starter 是一种非常方便的模块化方式,它可以帮助我们快速集成第三方库或自定义功能。本文将带你一步步实现一个自定义的 MyBatis Starter,并将其发布到 Maven 仓库中,供其他项目使用。

1 什么是 MyBatis Starter?

MyBatis 是一个优秀的持久层框架,而 MyBatis Starter 是 Spring Boot 提供的一个官方 Starter,用于快速集成 MyBatis。通过自定义 Starter,我们可以封装一些通用的 MyBatis 配置、插件或工具类,简化项目的配置和开发。

2 实现目标

我们将实现一个自定义的 MyBatis Starter,包含以下功能:

  1. 自动配置 MyBatis 数据源。
  2. 自动扫描 Mapper 接口。

3 项目结构

4 实现步骤

在实现自定义 MyBatis Starter 的过程中,我们需要完成以下三个主要步骤:

  • 创建 autoconfigure 模块:实现自动配置逻辑。
  • 创建 starter 模块:依赖 autoconfigure 模块,并提供默认配置。
  • 使用自定义 Starter:在其他 Spring Boot 项目中引入并使用自定义 Starter。

4.1 创建autoconfigure

  1. 创建maven项目
    在 pom.xml 中定义项目依赖:
xml 复制代码
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>3.4.3</version>
    </dependency>
    <!--jdbc-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
      <version>3.4.3</version>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.14</version>
    </dependency>
    <!--MyBatis与Spring框架集成的桥梁-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>3.0.3</version>
    </dependency>
  1. 实现自动配置类
java 复制代码
@AutoConfiguration
public class MybatisAutoConfig {
    @Bean
    // 自动配置 MyBatis 数据源。
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        // 创建一个SqlSessionFactoryBean对象
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 返回SqlSessionFactoryBean对象
        return sqlSessionFactoryBean;
    }
    @Bean
    // 自动扫描 Mapper 接口。
    public MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory) {
        // 创建一个MapperScannerConfigurer对象
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        // 获取自动配置的包列表
        List<String> basePackages = AutoConfigurationPackages.get(beanFactory);
        // 设置MapperScannerConfigurer的基础包
        mapperScannerConfigurer.setBasePackage(basePackages.get(0));
        // 设置MapperScannerConfigurer的注解类
        mapperScannerConfigurer.setAnnotationClass(Mapper.class);
        // 返回MapperScannerConfigurer对象
        return mapperScannerConfigurer;
    }
}
  1. 注册自动配置
  2. 打包:使用以下命令将项目打包并发布到 Maven 仓库:
bash 复制代码
mvn clean install

4.2 创建starter

  1. 创建Maven项目
  2. 定义项目依赖
yaml 复制代码
  <dependency>
      <groupId>com.wfs</groupId>
      <artifactId>wmybatis-spring-boot-autoconfigure</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>3.4.3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
      <version>3.4.3</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.14</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>3.0.3</version>
    </dependency>
  1. 打包至maven仓库

4.3 使用自定义 Starter

  1. 在其他 Spring Boot 项目中引入自定义 Starter(该项目详细描述情请看:Springboot基础篇(2):SpringBoot整合Mybatis):
  1. 效果图
相关推荐
Asthenia04127 分钟前
Linux目录含义/常用Shell命令:内存/硬盘/关键词搜索/文件名搜索
后端
import_random31 分钟前
[容器]基础知识
后端
码猿技术专栏34 分钟前
Spring Boot + CRaC 启动速度提升了10倍!
java·spring boot·后端
雷渊42 分钟前
深入分析mysql给表加字段涉及到的锁
java·后端·面试
Apifox43 分钟前
无需技术能力!通过 Apifox 与你的产品/运营同事一起高效搭建精美产品文档
前端·后端·产品
axuebin1 小时前
淘天集团自营技术团队春招开始啦~
前端·后端·面试
字节王德发1 小时前
如何在Spring Boot中配置和使用MyBatis-Plus
spring boot·后端·mybatis
草药味儿の岁月1 小时前
Spring Boot启动流程及源码实现深度解析
java·spring boot·后端
Asthenia04121 小时前
线程组和线程优先级:从骨子里挖细节
后端
掘金一周1 小时前
老板花一万大洋让我写的艺术工作室官网?! HeroSection 再度来袭!(Three.js) | 3.13
android·前端·后端