Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ Mapper and @ MapperScan in mybatis annotations

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use @ Mapper and @ MapperScan in mybatis annotations". In daily operation, I believe many people have doubts about how to use @ Mapper and @ MapperScan in mybatis annotations. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use @ Mapper and @ MapperScan in mybatis annotations"! Next, please follow the editor to study!

Catalogue

@ Mapper and @ MapperScan of mybatis annotations

Method 1: use @ Mapper annotations

Method 2: use @ MapperScan annotations

The difference and understanding between @ MapperScan and @ Mapper

Action

Scan a packet

Scan multiple packages

@ Mapper and @ MapperScan of mybatis annotations

When using the Mybatis persistence layer framework to manipulate the database, we can use the @ Mapper annotation and the @ MapperScan annotation to hand over the Mapper interface class to Sprinig for management.

Method 1: use @ Mapper annotations

Advantages: finer granularity

Disadvantages: add @ Mapper annotation directly to the Mapper interface class, and need to add @ Mapper annotation to every mapper interface class, which is more tedious

Method 2: use @ MapperScan annotations

@ MapperScan allows you to specify the package path of the Mapper interface class to be scanned

SpringBootApplication @ MapperScan ("com.erayt.mapper") public class App {public static void main (String [] args) {SpringApplication.run (App.class, args);}}

You can use * as a wildcard to match the package name in the path

SpringBootApplication @ MapperScan ("com.erayt.*.mapper") public class App {public static void main (String [] args) {SpringApplication.run (App.class, args);}}

You can also scan multiple packages using the @ MapperScan annotation

@ SpringBootApplication @ MapperScan ("com.erayt.mapperFirst", "com.erayt.mapperSecond") public class App {public static void main (String [] args) {SpringApplication.run (App.class, args);} @ MapperScan and @ Mapper

Scan the Dao layer in the project and inject dao interface classes into Spring, allowing other classes to reference

Mapper: in the dao interface class, add this comment; the trouble is that every dao interface class must add this comment

@ MapperScan: you can specify the path of the dao interface class to be scanned. You can add this annotation to the startup class instead of @ Mapper annotation (you don't have to add @ Mapper annotation to all dao interface classes in this module)

Scan a packet

@ MapperScan ("com.demo.mapper"): scan for interfaces in the specified package

@ MapperScan ("com.demo.*.mapper"): a * represents a first-level packet; for example, you can scan to com.demo.aaa.mapper, but not to com.demo.aaa.bbb.mapper.

@ MapperScan ("com.demo.**.mapper"): two * represent any packet; for example, you can scan to com.demo.aaa.mapper or com.demo.aaa.bbb.mapper.

Scan multiple packages

If the dao interface class is under a package or subpackage that the main program can scan:

@ MapperScan ({"com.kfit.demo", "com.kfit.user"})

If not, you can configure it in the following ways:

@ MapperScan ({"com.kfit.*.mapper", "org.kfit.*.mapper"}). This is the end of the study on "how to use @ Mapper and @ MapperScan in the mybatis annotations". I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report