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 the Spring Dao layer @ Repository and @ Mapper

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

Share

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

This article will explain in detail how to use the Spring Dao layer @ Repository and @ Mapper. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Spring Dao layer @ Repository and @ Mapper

When you use annotations to develop the Dao layer, you often confuse the two annotations. I don't know how to add them. Make a note here.

1. @ Mapper

@ Mapper is the annotation of Mybatis, which has nothing to do with Spring, and @ Repository is the annotation of Spring, which declares a Bean. (important)

There are two ways to use Mybatis: XML files or annotations. If you use XML files, we need to specify the location of XML in the configuration file. Here, we only study the development of annotations.

In the Spring program, Mybatis needs to find the corresponding mapper and dynamically generate the proxy class during compilation to realize the database query function, so we need to add @ Mapper annotation to the interface.

@ Mapperpublic interface UserDao {...}

However, just using the @ Mapper annotation, we will find that in the dependency injection in other variables, IDEA will prompt an error, but does not affect the run (test ~). Because we did not explicitly mark that this was a Bean,IDEA thought that the instance injection would not be found at run time, it prompted us to make an error. As shown in the picture below, there will be red wavy lines.

Although this error message does not affect the operation, it looks very uncomfortable, so we can add the declaration of bean to the corresponding interface, as follows:

@ Repository / / can also use @ Component, the effect is the same, only to declare as bean@Mapperpublic interface UserDao {@ Insert ("insert into user (account, password, user_name)" + "values (# {user.account}, # {user.password}, # {user.name})") int insertUser (@ Param ("user") User user) throws RuntimeException;} 2, @ Repository

As mentioned above, @ Repository is used to declare the bean of the dao layer, and if we are really going to use @ Repository for development, it is code-based development, which is simply handwritten JDBC.

Like @ Service and @ Controller, we add @ Repository to the corresponding implementation class, as follows:

@ Repositorypublic class UserDaoImpl implements UserDao {@ Override public int insertUser () {JdbcTemplate template = new JdbcTemplate ();.} 3, other scanning methods

Annotation-based development also has other tools to help Mybatis find mapper: the @ MapperScan annotation, which can be added to the startup class to automatically scan all interfaces under the package path.

@ SpringBootApplication@MapperScan ("com.scut.thunderlearn.dao") public class UserEurekaClientApplication {public static void main (String [] args) {SpringApplication.run (UserEurekaClientApplication.class, args);}}

With this method, there is no need to add any annotations to the interface.

4. Summary

@ Mapper: there must be one, or Mybatis won't find mapper.

@ Repository: optional, and the error message of dependency injection can be eliminated.

@ MapperScan: can replace @ Mapper.

The difference between @ Mapper and @ Repository 1. Similarities

Both @ Mapper and @ Repository act on the dao layer interface to generate the proxy object bean, which is managed by the spring container.

For mybatis, you don't have to write mapper.xml files.

2. Differences

@ Mapper does not need to configure the scan address and can be used alone. If you have multiple mapper files, you can add @ MapperScan ("the package where the mapper file is located") in the project startup class, so that you do not need to annotate every mapper file with @ Mapper.

@ Repository cannot be used alone, otherwise the following error will be reported

Field userMapper in com.liu.service.UserServiceImpl required a bean of type 'com.liu.mapper.UserMapper' that could not be found.

Bean could not be found because files annotated with @ Repository were not scanned when the project was started, so using @ Repository requires configuring the scan address

But in idea, using @ Repository eliminates errors when injecting mapper objects into the business tier, as shown in the following figure

This is the end of the article on "how to use Spring Dao layer @ Repository and @ Mapper". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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: 229

*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