In the SpringBoot project, if MyBatis is used as the persistence layer framework, you may encounter the problem of mapper reporting a null pointer exception when using automatic injection. This is because SpringBoot cannot correctly identify the Mapper interface of MyBatis during automatic injection and requires some additional configuration.
There are two ways to solve this problem:
1. Add annotations to the Mapper interface
Add the @Mapper annotation to the Mapper interface to tell SpringBoot that this interface is a Mapper interface and needs to be proxied . An example is as follows:
@Mapper public interface UserMapper { // ... }
2. Manually configure the Mapper scan path
Manually configure the Mapper scan path in application.properties or application.yml to tell SpringBoot to scan the Mapper interfaces under which packages. The example is as follows:
Configure in application.properties:
mybatis.mapper-locations=classpath:mapper/*.xml
Configure in application.yml:
mybatis: mapper-locations: classpath:mapper/*.xml
It should be noted that mapper/*.xml here refers to the Mapper interface The path where the corresponding XML file is stored. If you use annotations to configure the SQL statement, you do not need to configure the XML file path.
After configuring in the above two ways, you can correctly inject the Mapper interface when using automatic injection.
Finally, if you have done all the above and still get an empty result, check whether you accidentally did not inject the mapper in the controller layer, for example
If it is a new object, the referenced injection object is not an object automatically injected into the spring container, so it will be reported as empty. You have to perform a full set of performances. Use @Autowird injection in the controller layer as well.
The above is the detailed content of How to solve the problem of empty mapper automatically injected into idea springBoot project. For more information, please follow other related articles on the PHP Chinese website!