What is the purpose of Mapper?

When I first used MyBatis, I wrote it completely and then used MyBatis code generator (referred to as MBG). In the process of using MBG, I found a very troublesome problem. If the database field changes frequently, the code needs to be generated repeatedly. And because MBG overwrites the generated code and generates XML appending, each rebuild requires a lot of alignment changes. In addition to this problem, there is another problem, just basic methods, such as add, delete, change and check, have already generated a lot of XML content, without adding a handwritten method, the code may have hundreds of lines, content, look cumbersome.

Because many people are using MBG, there are many common single-table methods defined in MBG. In order to solve the problems mentioned above, and also to avoid too much project reconstruction in the MBG compatible method, on the basis of MBG combined with part of JPA annotations produced a universal Mapper. Universal Mapper makes it easy to get basic single-table methods and to extend common methods. Using a universal Mapper can greatly improve your productivity.

Quick start

To give you a quick overview of the benefits of universal Mapper, here’s a brief overview of the process.

Start by adding the generic Mapper dependency to the Maven project’s POM.xml:

Next, start using it concretely.

1. Configure the entity class

Basic code such as entity classes can be generated directly through MBG and special code generator. To avoid excessive information, it is written and configured manually.

There are the following categories:

In the above class, we added the primary key@Id, marks this field as the database primary key. UseGeneratedKeys = true MyBatis useGeneratedKeys = true MyBatis useGeneratedKeys = true

After the above simple configuration, it is equivalent to the

relational mapping in MyBatis, special attention, this mapping relationship is only valid for general Mapper, handwritten method, need to deal with the mapping relationship. To recommend a Java communication group: 874811168 into the group can get free information

2. Create a Mapper interface

CountryMapper Interface from the entity class, create CountryMapper interface as follows:

It inherited the tk.mybatis.mapper.com mon. Mapper interfaces, the interface specifies the generic type on the Country. When you inherit the Mapper interface, you already have a number of methods for Country, as follows:

These methods are consistent with most of the methods generated by MBG, as well as some common methods that are not MBG.

3. Configure Mapper

In order for the above methods to be used directly, you also need to configure the generic Mapper so that the above methods are automatically generated at startup so that all of them can be used at run time.

Depending on the development environment, different configuration methods are required, complete content can be integrated with the general Mapper, we here take the most common Spring and MyBatis integration as an example.

If the MyBatis interface mode is used in an integrated Spring environment, you need to configure MapperScannerConfigurer. If you run the general Mapper, you only need to modify the following configurations:

Note the difference between the official package name and the tk package name here:

1.tk.mybatis.spring.mapper.MapperScannerConfigurer

2.org.mybatis.spring.mapper.MapperScannerConfigurer

3. I recommend a framework communication group: 874811168 you can get free information by joining the group. Only the first part is changed from ORG to TK.

At this point, the simplest configuration of the universal Mapper is complete, and the complete configuration can be seen here integrating with Spring.

4. Easy to use

Here is a simple test case. In practice, you can inject CountryMapper.

Full address of the test: Spring Integration test case

General Mapper only provides the basic number of methods, encountered no method, you can write according to the normal usage of MyBatis, and there is no difference between the normal usage.

5. Learn more

The above simple introduction has can be applied to the real project, but there are a lot of detail here are not involved, if you are ready to join the general Mapper, you can first project, introduced by the above content, and then through a more detailed documentation to understand more details, there are a lot of you may want to but MBG there is no way, there is here, And if you want to implement your own generic methods, you can easily extend them.

Reference: https://blog.csdn.net/isea533/article/details/83045335