使用Mybatis-Plus時(shí)的SqlSessionFactory問題及處理
使用Mybatis-Plus的SqlSessionFactory問題
前些日子工作中出現(xiàn)一個(gè)問題,項(xiàng)目中使用了MybatisPlus,然后出現(xiàn)了一個(gè)問題,Druid的其他的配置都可以正常使用,但是配置的SqlSessionFactory這個(gè)bean不能被加載,我在這個(gè)bean中加載的mybatis-config.xml文件也不能被加載,因?yàn)榇a里使用了攔截器進(jìn)行數(shù)據(jù)庫的自動分頁,找到問題后在這里mark一下。
其實(shí)這里不能加載的原因是因?yàn)镸ybatisPlus中自定義了MybatisSqlSessionFactoryBean這個(gè)類,而這個(gè)類是實(shí)現(xiàn)了接口FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent>,而在mybatis中有一個(gè)類也實(shí)現(xiàn)了這些接口,SqlSessionFactoryBean,所以在mybatisplus的配置文件中配置SqlSessionFactoryBean時(shí)需要換成mybatisplus中自定義的這個(gè)類MyBatisSqlSessionFactoryBean,并在類中加載對應(yīng)的mybatis-config.xml文件。
貼一下這兩個(gè)類的源碼,看一眼就明白了
//
// 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報(bào)錯(cuò)Property'sqlSessionFactory'or'sqlSessionTemplate'are required
報(bào)錯(cuò)信息:
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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 解析Mybatis SqlSessionFactory初始化原理
- mybatis初始化SqlSessionFactory失敗的幾個(gè)原因分析
- MyBatis源碼解析——獲取SqlSessionFactory方式
- Mybatis SqlSessionFactory與SqlSession詳細(xì)講解
- 詳解Mybatis核心類SqlSessionFactory的構(gòu)建
- Mybatis中自定義實(shí)例化SqlSessionFactoryBean問題
- MyBatis-plus報(bào)錯(cuò)Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required的解決方法
- 使用Mybatis時(shí)SqlSessionFactory對象總是報(bào)空指針
相關(guān)文章
SpringCloud解決feign調(diào)用token丟失問題解決辦法
在feign調(diào)用中可能會遇到如下問題:同步調(diào)用中,token丟失,這種可以通過創(chuàng)建一個(gè)攔截器,將token做透傳來解決,異步調(diào)用中,token丟失,這種就無法直接透傳了,因?yàn)樽泳€程并沒有token,這種需要先將token從父線程傳遞到子線程,再進(jìn)行透傳2024-05-05
詳述IntelliJ IDEA 中自動生成 serialVersionUID 的方法(圖文)
本篇文章主要介紹了詳述IntelliJ IDEA 中自動生成 serialVersionUID 的方法(圖文),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-11-11
Java實(shí)現(xiàn)經(jīng)典游戲超級瑪麗的示例代碼
在你的童年記憶里,是否有一個(gè)蹦跳、頂蘑菇的小人?本文將用java語言實(shí)現(xiàn)經(jīng)典游戲《超級瑪麗》,文中采用了swing技術(shù)進(jìn)行了界面化處理,需要的可以參考一下2022-02-02
在Java中實(shí)現(xiàn)線程安全的單例模式的常見方式
單例模式是一種常用的軟件設(shè)計(jì)模式,它確保一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問點(diǎn),在多線程環(huán)境下,確保單例模式的線程安全性是非常重要的,因?yàn)槎鄠€(gè)線程可能會同時(shí)嘗試創(chuàng)建實(shí)例,導(dǎo)致實(shí)例不唯一的問題,本文介紹了在Java中實(shí)現(xiàn)線程安全的單例模式有幾種常見的方式2024-09-09
IntelliJ IDEA之高效代碼插件RainBow Brackets詳解
這篇文章主要介紹了IntelliJ IDEA之高效代碼插件RainBow Brackets詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
java四種引用及在LeakCanery中應(yīng)用詳解
這篇文章主要介紹了java四種引用及在LeakCanery中應(yīng)用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

