Try not to use the unit test provided by jUnit
As a request, try to use the test class provided by SpringBoot for testing, which can automatically scan components and use bean objects in the container
And if If there is an injected object in a component, then this component must be taken out from the SpringBoot container and then the functions of the injected object can be used! ! !
There was an error today and it took me a long time to solve it. Finally, I found out that it was a very low-level and basic error!
This is the mapper interface. Using @mapper is equivalent to registering the proxy object of the interface into the bean, but it cannot be found in the context (actually it is normal)
Because the @Mapper annotation is Mybatis provided, and the @Autowried annotation is provided by Spring. IDEA can understand the context of Spring, but it is not related to Mybatis. And we can see from the @Autowried source code that by default, @Autowried requires that dependent objects must exist, so IDEA can only give a red warning at this time.
package com.bit.mapper; import com.bit.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface UserMapper { User selectById(@Param("userid") Integer id); }
This is the xml file corresponding to the mapper interface, and there is no problem.
<?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.bit.mapper.UserMapper"> <select id="selectById" resultType="com.bit.pojo.User"> select * from users where id = #{userid} </select> </mapper>
Add the xml file in the java directory to the resource resource, and nest it in the build tag. There is also no problem.
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
Then we wrote the service layer, wrote a UserService interface, and created an implementation class of the UserServiceImpl interface
In this implementation class, when injecting UserMapper, it kept saying that it could not be injected. I always thought that there was Problem (but in the end I found that there was no problem)
I finished writing the service implementation class, and there was no problem
package com.bit.service; import com.bit.mapper.UserMapper; import com.bit.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public User queryById(Integer id) { System.out.println("进入了service"); return userMapper.selectById(id); } }
Then I went to test it directly, I tested it Woolen cloth?
Instantiate UserService, create a new object, and then call the method directly to see if UserMapper can be called to query the database. Then I kept getting null pointer exception errors
@SpringBootTest class BitApplicationTests { @Test void contextLoads() { UserService userService = new UserServiceImpl(); userService.queryById(13); System.out.println(userService); System.out.println(userService.queryById(15)); System.out.println(userService.queryById(13)); } }
I once thought that the mapper interface was not injected into UserServcie, which caused the method of calling UserServcie to be called UserMapper The method is empty, I thought it was a problem with the Mapper interface. I searched various ways how to solve it. After a few hours, I found the answer in other people's blogs
Our UserMapper is injected into UserServiceImpl, We cannot use UserServcieIml directly. If we use its function in other classes, we must inject this class into the current class and get the UserService from the container before we can call it correctly without a null pointer exception. I always Not found, this is a very low-level error.
Correct approach: First assemble it into the current object, and then get the bean from the container for use
@SpringBootTest class BitApplicationTests { @Autowired private UserService userService; @Test void contextLoads() { System.out.println(userService.queryById(15)); System.out.println(userService.queryById(13)); } }
The above is the detailed content of What are the problems that may be encountered when SpringBoot integrates MyBatis?. For more information, please follow other related articles on the PHP Chinese website!