使用Mybatis-Plus時的SqlSessionFactory問題及處理
使用Mybatis-Plus的SqlSessionFactory問題
前些日子工作中出現(xiàn)一個問題,項目中使用了MybatisPlus,然后出現(xiàn)了一個問題,Druid的其他的配置都可以正常使用,但是配置的SqlSessionFactory這個bean不能被加載,我在這個bean中加載的mybatis-config.xml文件也不能被加載,因為代碼里使用了攔截器進行數(shù)據(jù)庫的自動分頁,找到問題后在這里mark一下。
其實這里不能加載的原因是因為MybatisPlus中自定義了MybatisSqlSessionFactoryBean這個類,而這個類是實現(xiàn)了接口FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent>,而在mybatis中有一個類也實現(xiàn)了這些接口,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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 解析Mybatis SqlSessionFactory初始化原理
- mybatis初始化SqlSessionFactory失敗的幾個原因分析
- MyBatis源碼解析——獲取SqlSessionFactory方式
- Mybatis SqlSessionFactory與SqlSession詳細講解
- 詳解Mybatis核心類SqlSessionFactory的構建
- Mybatis中自定義實例化SqlSessionFactoryBean問題
- MyBatis-plus報錯Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required的解決方法
- 使用Mybatis時SqlSessionFactory對象總是報空指針
相關文章
Java從數(shù)據(jù)庫中讀取Blob對象圖片并顯示的方法
這篇文章主要介紹了Java從數(shù)據(jù)庫中讀取Blob對象圖片并顯示的方法,實例分析了Java讀取數(shù)據(jù)庫中Blob對象圖片的技巧與操作方法,需要的朋友可以參考下2015-02-02SpringBoot快速整合SpringSecurity的詳細步驟(新手都會!)
日 Spring Security 是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型,他可以實現(xiàn)強大的Web安全控制,下面這篇文章主要給大家介紹了關于SpringBoot快速整合SpringSecurity的詳細步驟,需要的朋友可以參考下2023-03-03Java那點事——StringBuffer與StringBuilder原理與區(qū)別
本文給大家分享StringBuffer與StringBuilder的區(qū)別,它們的應用場景是什么?非常不錯,面試考官經(jīng)??嫉囊粋€問題,有需要的朋友跟著腳本之家小編一起學習吧2016-06-06SpringBoot+jpa配置如何根據(jù)實體類自動創(chuàng)建表
這篇文章主要介紹了SpringBoot+jpa配置如何根據(jù)實體類自動創(chuàng)建表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11使用Mybatis的Batch?Insert?Support?實現(xiàn)批量插入
這篇文章主要介紹了使用Mybatis的Batch?Insert?Support?實現(xiàn)批量插入。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07