首页
/ MyBatis-Plus项目中"Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"错误分析与解决方案

MyBatis-Plus项目中"Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"错误分析与解决方案

2025-05-13 00:19:33作者:邓越浪Henry

问题背景

在使用MyBatis-Plus框架开发项目时,开发者可能会遇到"Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"的错误提示。这个错误通常发生在Spring Boot应用启动阶段,表明MyBatis-Plus无法正确初始化Mapper接口所需的SQL会话工厂或模板。

错误原因分析

该错误的根本原因是MyBatis-Plus无法自动配置SQL会话相关的关键组件。在Spring Boot集成MyBatis-Plus的环境中,这通常由以下几种情况导致:

  1. 配置缺失:未正确配置MyBatis-Plus的相关属性,特别是mapper-locations路径
  2. 版本冲突:项目中存在MyBatis和MyBatis-Plus版本不兼容的情况
  3. 自动配置失效:某些自定义配置覆盖了Spring Boot的自动配置
  4. 多数据源场景:在多数据源配置中未正确处理SQL会话工厂的创建

解决方案

方案一:基础配置检查

首先确保在application.yml或application.properties中配置了MyBatis-Plus的基本属性:

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*.xml
  type-aliases-package: com.example.model
  configuration:
    map-underscore-to-camel-case: true

特别注意配置项前缀必须是mybatis-plus而非mybatis,这是新手常见的配置错误。

方案二:手动配置SQL会话工厂

当自动配置失效时,可以手动创建必要的Bean:

@Configuration
public class MyBatisConfig {
    
    @Bean
    public DataSource dataSource() {
        // 配置数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        
        // 添加分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        factoryBean.setPlugins(interceptor);
        
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

方案三:多数据源场景处理

在多数据源环境下,需要为每个数据源单独配置SQL会话工厂,并使用@Primary注解标记主数据源:

@Configuration
public class MultiDataSourceConfig {

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

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

    @Bean
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
        return createSqlSessionFactory(dataSource);
    }

    @Bean
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
        return createSqlSessionFactory(dataSource);
    }

    private SqlSessionFactory createSqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        // 其他配置...
        return factoryBean.getObject();
    }
}

最佳实践建议

  1. 版本一致性:确保MyBatis、MyBatis-Spring和MyBatis-Plus版本兼容
  2. 配置检查:定期检查配置项,特别是从MyBatis迁移到MyBatis-Plus的项目
  3. 日志监控:开启DEBUG级别日志,监控MyBatis-Plus的初始化过程
  4. 单元测试:为数据访问层编写集成测试,及早发现问题
  5. 依赖管理:使用Maven或Gradle的依赖管理功能,避免版本冲突

总结

"Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"错误是MyBatis-Plus项目中常见的配置问题。通过正确配置、手动创建必要Bean或处理多数据源场景,可以有效解决这个问题。理解MyBatis-Plus的自动配置原理,掌握SQL会话工厂的创建过程,是避免此类问题的关键。

登录后查看全文
热门项目推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
260
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
854
505
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
254
295
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
331
1.08 K
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
397
370
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
kernelkernel
deepin linux kernel
C
21
5