Detailed explanation of spring boot integration mybatis (annotation method)

Mybatis is an excellent persistence layer framework that supports customized SQL, stored procedures and advanced mapping, avoiding almost all JDBC code, manually setting parameters and obtaining result sets. Spring boot is a Java framework that supports the rapid creation of spring applications. This article uses an example to learn how spring boot integrates mybatis without XML configuration.

Create database

The example of this article uses the MySQL database. First, create a user table and execute the following SQL statements:

Project directory structure and dependency configuration

First, create a maven project and configure POM dependencies. The dependencies used in this example are as follows:

Then create the following project directory structure, as shown in the following figure:

Code file content

0. Create configuration file - application properties

Write the following:

1. Create POJO - entity / user java

This is a POJO, which contains three attributes: ID, name and age. The code is as follows:

2. Create a data layer interface - service / userservice java

This is a mapper class with the following code:

//1 @ mapper declares userdao as a mapper interface / / 2 @ results is the result mapping list, @ result property is the property name of the user class, colomn is the field name of the database table / / 3 @ select, @ insert represent the executed real SQL respectively

3. Create a user service - service / userservice java

This is a service class bean, which provides three function functions. The code is as follows:

4. A common web controller -- controller / usercontroller java

This is a spring web controller class, which introduces the spring boot starter web dependency. The code is as follows:

5. Create startup class - Main / startapp java

This is a spring boot startup class. The code is as follows:

//1 since the startapp class is located in the self package of the basic package, you need to set scanbasepackage / / 2 set the package where the mapper interface is located

Operation results

After running the SQL statement to create the database table, run the startapp class. The startup is successful, as shown in the figure below

Test the show service, and the results are as follows:

Test the showdao service. When entering the URL, you need to package the parameters into the URL. The results are as follows: when there are no parameters, the access error is:

With parameters, the access is successful. Because there are no records in the database, the result is an empty list:

Test insert service

Test the showdao service again

epilogue

It can be seen from the example in this article that using spring boot to integrate mybatis requires almost no configuration work, which can effectively speed up the development efficiency!

Code base address: GitHub address

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>