yzdlkxkl 2023-03-01 14:02 采纳率: 88.9%
浏览 24
已结题

mybatis的延迟加载/懒加载报错

懒加载全局变量报持久化异常
学生接口


```java
    /**
     * 分步查询学生信息
     * @param sid
     * @return
     */
    Student selectByIdStep(@Param("sid") Integer sid);

学生接口映射文件

    <resultMap id="studentResultMapStep" type="Student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname" />
        <association property="clazz" select="com.fei.mybatis.mapper.ClazzMapper.selectByIdStep" column="cid"/>
    </resultMap>
    <select id="selectByIdStep" resultMap="studentResultMapStep">
        select sid,sname,cid from t_student
        <where>sid = #{sid}</where>
    </select>


单元测试

    @Test
    public void selectByIdStep(){
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByIdStep(5);
        System.out.println(student.getSname());
        //懒加载
        System.out.println(student.getClazz().getCname());
        sqlSession.close();
    }

mybatis-config.xml文件,不打开懒加载全局配置,就可以分步查询没有出错,但开启了全局配置就会报错
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <settings>
        
        <setting name="lazyLoadingEnabled " value="true"/>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        
        <package name="com.fei.mybatis.bean"/>
    </typeAliases>
    <environments default="db1">
        <environment id="db1">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.fei.mybatis.mapper"/>
    </mappers>
</configuration>


开启报的持久化异常
D:\Java\jdk1.8.0_333\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\java\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar=63319:D:\java\JetBrains\IntelliJ IDEA 2021.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar;D:\java\JetBrains\IntelliJ IDEA 2021.3\plugins\junit\lib\junit5-rt.jar;D:\java\JetBrains\IntelliJ IDEA 2021.3\plugins\junit\lib\junit-rt.jar;D:\java\jdk1.8.0_333\jre\lib\charsets.jar;D:\java\jdk1.8.0_333\jre\lib\deploy.jar;D:\java\jdk1.8.0_333\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8.0_333\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8.0_333\jre\lib\ext\dnsns.jar;D:\java\jdk1.8.0_333\jre\lib\ext\jaccess.jar;D:\java\jdk1.8.0_333\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8.0_333\jre\lib\ext\localedata.jar;D:\java\jdk1.8.0_333\jre\lib\ext\nashorn.jar;D:\java\jdk1.8.0_333\jre\lib\ext\sunec.jar;D:\java\jdk1.8.0_333\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8.0_333\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8.0_333\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8.0_333\jre\lib\ext\zipfs.jar;D:\java\jdk1.8.0_333\jre\lib\javaws.jar;D:\java\jdk1.8.0_333\jre\lib\jce.jar;D:\java\jdk1.8.0_333\jre\lib\jfr.jar;D:\java\jdk1.8.0_333\jre\lib\jfxswt.jar;D:\java\jdk1.8.0_333\jre\lib\jsse.jar;D:\java\jdk1.8.0_333\jre\lib\management-agent.jar;D:\java\jdk1.8.0_333\jre\lib\plugin.jar;D:\java\jdk1.8.0_333\jre\lib\resources.jar;D:\java\jdk1.8.0_333\jre\lib\rt.jar;D:\java\IdeaProjects\mybatis\mybatis-005-advanced-mapping\target\test-classes;D:\java\IdeaProjects\mybatis\mybatis-005-advanced-mapping\target\classes;D:\java\MavenRepository\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;D:\java\MavenRepository\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;D:\java\MavenRepository\org\mybatis\mybatis\3.5.10\mybatis-3.5.10.jar;D:\java\MavenRepository\junit\junit\4.11\junit-4.11.jar;D:\java\MavenRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\java\MavenRepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\java\MavenRepository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\java\MavenRepository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.fei.mybatis.test.StudentMapperTest,selectByIdStep

java.lang.ExceptionInInitializerError
    at com.fei.mybatis.test.StudentMapperTest.selectByIdStep(StudentMapperTest.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: The setting lazyLoadingEnabled  is not known.  Make sure you spelled it correctly (case sensitive).
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66)
    at com.fei.mybatis.utils.SqlSessionUtils.<clinit>(SqlSessionUtils.java:23)
    ... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: The setting lazyLoadingEnabled  is not known.  Make sure you spelled it correctly (case sensitive).
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    ... 27 more
Caused by: org.apache.ibatis.builder.BuilderException: The setting lazyLoadingEnabled  is not known.  Make sure you spelled it correctly (case sensitive).
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.settingsAsProperties(XMLConfigBuilder.java:135)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:107)
    ... 29 more


Process finished with exit code -1


```

  • 写回答

2条回答 默认 最新

  • Web Security Loop 2023-03-01 14:11
    关注

    不要开启全局的懒加载配置,改为在需要进行延迟加载的查询中使用fetchType属性,将值设置为lazy,可实现延迟加载,同时避免持久化异常。

    • 在需要进行延迟加载的关联查询中,通过设置fetchType属性值为lazy。
    <association property="clazz" select="com.fei.mybatis.mapper.ClazzMapper.selectByIdStep" column="cid" fetchType="lazy"/>
    
    
    
    • 通过访问关联属性的方法来触发延迟加载。
    Student student = mapper.selectByIdStep(5);
    System.out.println(student.getSname());
    System.out.println(student.getClazz().getCname());
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 创建了问题 3月1日

悬赏问题

  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化