使用MybatisPlus报错“MybatisPlusException: com.demo.bean.User Not Found TableInfoCache.“
  TEZNKK3IfmPf 2023年11月13日 24 0

异常

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: com.demo.bean.User Not Found TableInfoCache.

	at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
	at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
	at com.baomidou.mybatisplus.core.toolkit.Assert.notNull(Assert.java:72)
	at com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils.currentSessionFactory(GlobalConfigUtils.java:54)
	at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.sqlSession(SqlHelper.java:85)
	at com.baomidou.mybatisplus.extension.activerecord.Model.sqlSession(Model.java:248)
	at com.baomidou.mybatisplus.extension.activerecord.Model.selectById(Model.java:161)
	at com.baomidou.mybatisplus.extension.activerecord.Model.selectById(Model.java:174)
	at com.demo.test.TestMybatisPlus.testAR1(TestMybatisPlus.java:373)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

错误代码

    @Test
    public void testAR1() {
        // 实例化对象
        User user = new User();
        user.setId(4L);// 设置主键id
        // 调用AR的查询方法selectById()
        User resultUser = user.selectById();
        System.out.println(resultUser);
    }

原因

没有将实体类对应的Mapper类注入进来,解决办法就是手动注入实体类对应的Mapper类。

参考博客:mybatis-plus报Not Found TableInfoCache异常

解决

我这里使用的是spring集成mybatis,所以是手动加载spring的核心配置文件applicationContext.xml然后获取UserMapper。

    @Test
    public void testAR1() {
        ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");// 重要的是这行,加载对于spring和mybatis的配置
        UserMapper userMapper = (UserMapper) app.getBean("userMapper");// 即使不用UserMapper也会执行成功
        // 实例化对象
        User user = new User();
        user.setId(4L);// 设置主键id
        // 调用AR的查询方法selectById()
        User resultUser = user.selectById();
        System.out.println(resultUser);
    }

使用MybatisPlus报错“MybatisPlusException: com.demo.bean.User Not Found TableInfoCache.“

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   46   0   0 JSpspring
TEZNKK3IfmPf