使用Mybatis-Plus时的SqlSessionFactory问题及处理

 更新时间:2021年12月16日 11:49:59   作者:canyun9798  
这篇文章主要介绍了使用Mybatis-Plus时的SqlSessionFactory问题及处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用Mybatis-Plus的SqlSessionFactory问题

前些日子工作中出现一个问题,项目中使用了MybatisPlus,然后出现了一个问题,Druid的其他的配置都可以正常使用,但是配置的SqlSessionFactory这个bean不能被加载,我在这个bean中加载的mybatis-config.xml文件也不能被加载,因为代码里使用了拦截器进行数据库的自动分页,找到问题后在这里mark一下。

其实这里不能加载的原因是因为MybatisPlus中自定义了MybatisSqlSessionFactoryBean这个类,而这个类是实现了接口FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent>,而在mybatis中有一个类也实现了这些接口,SqlSessionFactoryBean,所以在mybatisplus的配置文件中配置SqlSessionFactoryBean时需要换成mybatisplus中自定义的这个类MyBatisSqlSessionFactoryBean,并在类中加载对应的mybatis-config.xml文件。

贴一下这两个类的源码,看一眼就明白了

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.mybatis.spring;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
    private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);
    private Resource configLocation;
    private Configuration configuration;
    private Resource[] mapperLocations;
    private DataSource dataSource;
    private TransactionFactory transactionFactory;
    private Properties configurationProperties;
    private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    private SqlSessionFactory sqlSessionFactory;
    private String environment = SqlSessionFactoryBean.class.getSimpleName();
    private boolean failFast;
    private Interceptor[] plugins;
    private TypeHandler<?>[] typeHandlers;
    private String typeHandlersPackage;
    private Class<?>[] typeAliases;
    private String typeAliasesPackage;
    private Class<?> typeAliasesSuperType;
    private DatabaseIdProvider databaseIdProvider;
    private Class<? extends VFS> vfs;
    private Cache cache;
    private ObjectFactory objectFactory;
    private ObjectWrapperFactory objectWrapperFactory;
    public SqlSessionFactoryBean() {
    }
     。。。。。。
}

还有MybatisSqlSessionFactoryBean的

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.baomidou.mybatisplus.spring;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLConfigBuilder;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.IEnum;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.handlers.EnumTypeHandler;
import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.toolkit.PackageHelper;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import javax.sql.DataSource;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
    private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);
    private Resource configLocation;
    private Configuration configuration;
    private Resource[] mapperLocations;
    private DataSource dataSource;
    private TransactionFactory transactionFactory;
    private Properties configurationProperties;
    private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    private SqlSessionFactory sqlSessionFactory;
    private String environment = MybatisSqlSessionFactoryBean.class.getSimpleName();
    private boolean failFast;
    private Interceptor[] plugins;
    private TypeHandler<?>[] typeHandlers;
    private String typeHandlersPackage;
    private Class<?>[] typeAliases;
    private String typeAliasesPackage;
    private String typeEnumsPackage;
    private Class<?> typeAliasesSuperType;
    private DatabaseIdProvider databaseIdProvider;
    private Class<? extends VFS> vfs;
    private Cache cache;
    private ObjectFactory objectFactory;
    private ObjectWrapperFactory objectWrapperFactory;
    private GlobalConfiguration globalConfig = GlobalConfigUtils.defaults();
    public MybatisSqlSessionFactoryBean() {
    }
     。。。。。。
}

springboot+mybatis-plus报错Property'sqlSessionFactory'or'sqlSessionTemplate'are required

报错信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘TBaseAuthController': Unsatisfied dependency expressed through field ‘tBaseAuthService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘TBaseAuthServiceImpl': Unsatisfied dependency expressed through field ‘baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘TBaseAuthMapper' defined in file [D:\浏览器下载\myframe\yss-server\target\classes\com\yss\cn\modules\mapper\TBaseAuthMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory' or ‘sqlSessionTemplate' are required

Caused by: java.lang.IllegalArgumentException: Property ‘sqlSessionFactory' or ‘sqlSessionTemplate' are required

解决方案:

添加jar包:

<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.3.2</version>
</dependency>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringCloud解决feign调用token丢失问题解决办法

    SpringCloud解决feign调用token丢失问题解决办法

    在feign调用中可能会遇到如下问题:同步调用中,token丢失,这种可以通过创建一个拦截器,将token做透传来解决,异步调用中,token丢失,这种就无法直接透传了,因为子线程并没有token,这种需要先将token从父线程传递到子线程,再进行透传
    2024-05-05
  • java生成验证码步骤归纳总结

    java生成验证码步骤归纳总结

    这篇文章主要为大家详细介绍了java生成验证码的步骤总结,需要的朋友可以参考下
    2017-04-04
  • 详述IntelliJ IDEA 中自动生成 serialVersionUID 的方法(图文)

    详述IntelliJ IDEA 中自动生成 serialVersionUID 的方法(图文)

    本篇文章主要介绍了详述IntelliJ IDEA 中自动生成 serialVersionUID 的方法(图文),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-11-11
  • Java实现经典游戏超级玛丽的示例代码

    Java实现经典游戏超级玛丽的示例代码

    在你的童年记忆里,是否有一个蹦跳、顶蘑菇的小人?本文将用java语言实现经典游戏《超级玛丽》,文中采用了swing技术进行了界面化处理,需要的可以参考一下
    2022-02-02
  • java 中数组初始化实例详解

    java 中数组初始化实例详解

    这篇文章主要介绍了 本文主要讲数组的初始化方法、可变参数列表以及可变参数列表对函数重载的影响的相关资料,需要的朋友可以参考下
    2017-05-05
  • 在Java中实现线程安全的单例模式的常见方式

    在Java中实现线程安全的单例模式的常见方式

    单例模式是一种常用的软件设计模式,它确保一个类只有一个实例,并提供一个全局访问点,在多线程环境下,确保单例模式的线程安全性是非常重要的,因为多个线程可能会同时尝试创建实例,导致实例不唯一的问题,本文介绍了在Java中实现线程安全的单例模式有几种常见的方式
    2024-09-09
  • IntelliJ IDEA之高效代码插件RainBow Brackets详解

    IntelliJ IDEA之高效代码插件RainBow Brackets详解

    这篇文章主要介绍了IntelliJ IDEA之高效代码插件RainBow Brackets详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • mybatis接收以逗号分隔的字符串批量查询方式

    mybatis接收以逗号分隔的字符串批量查询方式

    这篇文章主要介绍了mybatis接收以逗号分隔的字符串批量查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • java四种引用及在LeakCanery中应用详解

    java四种引用及在LeakCanery中应用详解

    这篇文章主要介绍了java四种引用及在LeakCanery中应用,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • java分页拦截类实现sql自动分页

    java分页拦截类实现sql自动分页

    这篇文章主要为大家详细介绍了java分页拦截类可以实现sql自动分页,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11

最新评论