异常
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类。
解决
我这里使用的是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);
}