When spring boot and mybatis are integrated, spring boot annotations cannot scan to annotations under packages other than the application class when the interface is scanned, such as:
The app is the application class, the Productmapper class:
@Mapper Public Interfaceproductmapper {@Insert (Insert into products (Pname,type,price) VALUES (#{pname},#{type},#{price}) Public intAdd (product product); @Delete ("Delete from products where id=#{arg1}") Public intDeletebyid (intID); @Update ("Update products set Pname=#{pname},type=#{type},price=#{price} where Id=#{id}") Public intUpdate (product product); @Select ("SELECT * FROM Products where ID=#[ARG1}") PublicProduct GetById (intID); @Select ("SELECT * from Productsorder by id DESC") PublicList<product>querybylists ();}
When the app is running, the backend will report that Productmapper is not found in this class:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying Bean of type ' com.self.spring.mapper.ProductMapper ' availableat Org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean (Defaultlistablebeanfactory.java : 353) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean ( defaultlistablebeanfactory.java:340) at Org.springframework.context.support.AbstractApplicationContext.getBean ( abstractapplicationcontext.java:1090) at Com.self.spring.springboot.App.main (app.java:17)
causes the above error: The Bean assembly default rules for Springboot projects are scanned from top to bottom based on the package location where the application class resides! "Application class" refers to the Springboot project entry class. The location of this class is critical:
If the package containing the application class is: io.github.gefangshuai.app
, only the package io.github.gefangshuai.app
and all its child packages will be scanned, and if the service or DAO is in a package that is not io.github.gefangshuai.app
under its child package, it will not be scanned!
Workaround
The first type: The Application class is placed under the parent package, and all annotated classes are placed under the same or under its child package
Second: Specify the URL of the package to be scanned for annotations, the solution to the problem above can be annotated on the application class to scan the class under the Mapper interface package.
@SpringBootApplication @mapperscan (basepackages= "Com.self.spring.springboot.mapper") Public class App { publicstaticvoid main (string[] args) { = Springapplication.run (App.class, args); System.out.println (Context.getbean (productmapper. class )); Context.close (); }}
Spring Boot Auto Scan