Problems encountered with SpringBoot Autowire configuration

I recently learned to use SpringBoot, and I have some problems when using Autowire Autowire, record it.


SpringBootTest is used here to test the dao layer. When using the Autowire annotation to make the dao auto-assemble, I encountered this problem. The dao layer keeps reporting errors. After checking on the Internet, it is said that Idea sometimes reports errors when using the Autowire annotation, but Does not affect program operation.
write picture description here

In fact, it did report an error when running it, saying that the bean in the dao layer does not exist. At first, I thought that the configuration of the Test class was wrong. Baidu and other people's codes are all configured in this way. The class configuration pointed to by the SpringBootTest annotation Not wrong either.
write picture description here


1. I checked the Internet and said that the SpringBootApplication class must be in the outermost layer of the package, such as in the com.app package, then Autowire scans all classes and packages under com.app and finds itself This SpringBootApplication is indeed not in the outermost layer, so quickly move to the outermost layer.
write picture description here
After moving the test class, it still reports that the bean does not exist.


2. After comparing other people's code by Baidu, it is found that there is an error in the Dao layer. The original configuration is as follows:
write picture description here
It is found that the configuration of others is:
write picture description here

The annotation for the bean is @Mapper. After changing the operation, it is found that it is ok, and the dao layer can be automatically assembled.
The ssm framework used before, with the @Repository annotation, dao can be automatically assembled, but SpringBoot can't.


3. When using the mybatis self-increasing primary key, I found that the primary key acquisition has always been empty. I checked the configuration in the @Options of the dao layer. There is indeed nothing wrong. The fields and columns are correct. Finally, I found that the primary key of the database table forgot to set auto_increment. . After checking the configuration for a long time, I didn't pay much attention to this thing before, so I wrote it down to avoid stepping on the pit next time.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325578853&siteId=291194637