Home > Java > javaTutorial > body text

How to solve the problem of empty mapper automatically injected into idea springBoot project

WBOY
Release: 2023-05-17 18:49:18
forward
1822 people have browsed it

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 {
    // ...
}
Copy after login

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
Copy after login

Configure in application.yml:

mybatis:
  mapper-locations: classpath:mapper/*.xml
Copy after login

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

How to solve the problem of empty mapper automatically injected into idea springBoot project

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template