SpringBoot integrates mybatis error reporting document records

Posted by stweaker on Wed, 13 Oct 2021 22:55:01 +0200

SpringBoot integrates mybatis error reporting document records

The following is the error information that I have delayed sorting out for many hours. I hope you can refer to it. This paper mainly gives it to myself in the form of notes as a reference.

1. Organization structure of the project

Set the User class in pojo, create the Dao layer interface @ Autowired injection, create an instance, and test on the UserDaoTest class.

1.1 add dependent pom files

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

Here, my spring boot package uses version 2.0.0, and mybatis spring boot starter uses version 1.3.2.

1.2 documents under application.properties

# mybatis alias scan
mybatis.type-aliases-package=com.xaf.pojo
# mapper.xml file location. If there is no mapping file, please comment it out
mybatis.mapper-locations=classpath:mappers/*.xml

Because it is to integrate mybatis and create maven project. The resources here are placed in the mappers package under rsources. Here, keep in mind that the path is consistent with the actual path of the project.

1.3 enter the Application startup class

@SpringBootApplication
@MapperScan("com.xaf.dao")
public class Application{
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
  1. The Application startup class should be placed in the parent package of dao layer or service layer. Because the Application startup can only scan the classes in the package and its sub packages, otherwise the whole startup will have problems.
  2. Remember to start MapperScan scanning. This annotation is to scan the mapper.java file or the Java class with @ mapper annotation.

For example, here:

@Mapper
public interface UserDao {
    public List<User> findAll();
}

1.4 mapper mapping file

<?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.xaf.dao.UserDao">
    <select id="findAll" resultType="User">
        select * from tb_user
    </select>
</mapper>

1.5 Test class

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {

    @Autowired()
    private UserDao userDao;

    @Test
    public void testFindAll(){
        List<User> list = userDao.findAll();
    }
}

After configuring the startup class and creating the interface, test the instance. Call the testFindAll method in the UserDao interface.

I don't seem to think there is any error. The next step is to analyze and report errors.

2. Error message

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xaf.dao.UserDao.findAll

	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49)
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
	at com.sun.proxy.$Proxy78.findAll(Unknown Source)
	at com.xaf.dao.UserDaoTest.testFindAll(UserDaoTest.java:21)
	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:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

The feedback of overall error reporting is:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xaf.dao.UserDao.findAll

If there are no problems above, but an error is still reported, the error message is caused by IDEA's inability to find the mapper's mapping file. Here you can view the verification conjecture under the target package:

Sure enough, the UserDao.xml I originally put under the mappers under resources was not packaged into the classes file when compiling.

After my personal modification, I can view the project structure here:

It's probably like this. If a small partner appears and places xml in src\main\java, you can set it as follows:

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/ *.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

Of course, if there are other files, you can also add the < include > < / include > tag.

After compilation, it looks like this:

The project is running successfully!

3. Summary

Error reported by integrating mybatis:

  1. Add dependent packages
  2. Set @ MapperScan(""... ") under the startup class or add @ Mapper under the scan class
  3. The path to the startup class
  4. mapper sets the address of the namespace of the mapping file
  5. maven configuration problem

The above summarized questions are shared with you here. Thank you for reading.

Topics: Java Spring Spring Boot