解决java.lang.IllegalArgumentException:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are requir
文章目錄
- 1. 復現問題
- 2. 分析問題
- 2.1 檢查是否引入了mybatis的jar包
- 2.2 檢查啟動類的配置
- 2.3 檢查xml文件對應java類的配置是否有誤
- 2.4 檢查application.yml文件的mybatis配置是否有誤
- 2.5 檢查pon.xml文件
- 3. 解決問題
1. 復現問題
今天在測試如下代碼時:
@SpringBootTest class LowCodeApplicationTests {@Autowired private Userservice userservice;@Testvoid contextLoads() {List<User> users = userservice.queryByParam("測試");for (User user : users) {System.out.println(user);}} }報錯如下錯誤:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:\project\target\classes\com\cloud\lowcode\mapper\UserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are requiredat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:461)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:527)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:497)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:650)at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:228)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:318)... 83 more Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are requiredat org.springframework.util.Assert.notNull(Assert.java:201)at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122)at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73)at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)... 96 more即錯誤信息為:java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required。
2. 分析問題
將該錯誤信息翻譯成中文為java.lang.IllegalArgumentException:屬性“sqlSessionFactory”或“sqlSessionTemplate”是必需的。
2.1 檢查是否引入了mybatis的jar包
<properties><java.version>1.8</java.version><mybatisStarter.version>2.2.2</mybatisStarter.version><baomidouStarter.version>1.0.5</baomidouStarter.version><baomidouMybatisPlus.version>2.1.9</baomidouMybatisPlus.version> </properties><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatisStarter.version}</version> </dependency> <dependency><groupId>com.baomidou</groupId><artifactId>mybatisplus-spring-boot-starter</artifactId><version>${baomidouStarter.version}</version> </dependency> <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>${baomidouMybatisPlus.version}</version> </dependency>2.2 檢查啟動類的配置
檢查啟動類是否加上MapperScan,如下已加上該注解:
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) @MapperScan("com.**.mapper") public class LowCodeApplication {public static void main(String[] args) {SpringApplication.run(LowCodeApplication.class, args);}}2.3 檢查xml文件對應java類的配置是否有誤
如下是userMapper.xml配置信息:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.cloud.lowcode.mapper.UserMapper"><select id="queryByParam" parameterType="string" resultType="com.cloud.lowcode.entity.po.User">SELECT * FROM `user`<where><if test="username != null || username.length > 0">name = #{username}</if></where></select> </mapper>再看對應的UserMapper.java類,如下所示:
package com.cloud.lowcode.mapper;import com.baomidou.mybatisplus.mapper.BaseMapper; import com.cloud.lowcode.entity.po.User; import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** @author 念兮為美* @datetime 2022/11/28 13:51* @desc 用戶實體類*/ @Mapper public interface UserMapper extends BaseMapper<User> {List<User> queryByParam(String username); }userMapper.xml和userMapper.java對應如下:
-
userMapper.xml和UserMapper.java的名字一樣(不包括后綴名)
-
userMapper.xml中的namespace為com.cloud.lowcode.mapper.UserMapper對應UserMapper.java中的包名+類名,沒問題
-
userMapper.xml中的id為queryByParam對應UserMapper.java中的方法名,沒問題
-
userMapper.xml中的參數類型parameterType為string對應UserMapper.java中的queryByParam字符串類型,沒問題
-
經檢查resultType也沒問題
2.4 檢查application.yml文件的mybatis配置是否有誤
如下是application.yml配置的mybatis信息:
#mybatis配置 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath*:/mapper/*Mapper.xmltype-aliases-package: com.cloud.lowcode.entity.po該配置信息沒有問題。
2.5 檢查pon.xml文件
<build><resources><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/java</directory><filtering>true</filtering></resource></resources> </build>pom.xml中已配置了resource信息,也沒有問題。
以上四種情況,都沒有問題,那么,問題出現在哪里呢?
3. 解決問題
嘗試刪除啟動類中的exclude={DataSourceAutoConfiguration.class},即修改成如下代碼:
@SpringBootApplication @MapperScan("com.**.mapper") public class LowCodeApplication {public static void main(String[] args) {SpringApplication.run(LowCodeApplication.class, args);}}重新啟動測試類,便能成功運行,如下代碼所示:
總結
以上是生活随笔為你收集整理的解决java.lang.IllegalArgumentException:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are requir的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: as的用法(基础)
- 下一篇: HTML里面em和i区别,html中em