Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways of writing Mapper

Shulou Source: shulou.com Published: 2022-06-01 03:30:07 10月03日 Update

This article is to share with you about the way Mapper is written. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What are the ways to write Mapper?

First: interface implementation class inherits SqlSessionDaoSupport: to use this method, you need to write mapper interface, mapper interface implementation class, mapper.xml file.

(1) configure the location of mapper.xml in sqlMapConfig.xml

(2) define mapper interface

(3) implement class integration SqlSessionDaoSupport

In the mapper method, you can add, delete, modify and query data by this.getSqlSession ().

(4) spring configuration

The second is to use org.mybatis.spring.mapper.MapperFactoryBean:

(1) configure the location of mapper.xml in sqlMapConfig.xml. If the names of mapper.xml and mappre interfaces are the same and in the same directory, you do not need to configure them here.

(2) define the mapper API:

(3) namespace in mapper.xml is the address of mapper interface

(4) the method name in the mapper interface is consistent with the id of the statement defined in mapper.xml.

(5) defined in Spring

Third: use mapper scanners:

(1) mapper.xml file writing:

Namespace in mapper.xml is the address of the mapper interface

The method name in the mapper interface is consistent with the id of the statement defined in mapper.xml

If you keep the names of the mapper.xml and mapper interfaces the same, you do not need to configure them in sqlMapConfig.xml.

(2) define the mapper API:

Note that the file name of mapper.xml is the same as the interface name of mapper and is placed in the same directory

(3) configure mapper scanner:

(4) obtain the implementation object of mapper from the spring container after using the scanner.

Thank you for reading! This is the end of this article on "what are the ways of writing 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, you can share it for more people to see!

Tags: Interface configuration consistency method method name scanner file no location content address more directory article good practical same container object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology NVidia Xiaomi MariaDB