Detailed explanation of spring boot mybatis

This article focuses on the annotated example code of spring boot mybatis. For details, please refer to this article:

pom. xml

Basic SQL operations

Spring boot has automatically implemented all configurations of mybatis. You can write directly to the Dao interface

@Mapper: declare a Dao interface of mybatis, which will be scanned by spring boot

@Select: declare a query method, and use @ insert for corresponding additions, deletions and modifications@ Delete@Update

List < user >: returns a collection. If only one result is returned, the return value is user. If it is an addition, deletion or modification method, the return value is int

User param: pass the parameter, #{id} which is the ID value of the param object

Scan mapper

@MapperScan("hello.dao")

By adding the above annotation to the configuration class, you can scan all interfaces in the Dao package instead of writing the @ mapper annotation in each Dao, but this will improve the coupling degree. While @ mapper can be integrated with Dao, echoing @ controller and @ service, and the overall structure is more elegant

Hump naming

Add the following configuration in properties. After the query is executed, the NN of the database can be_ NN format field, which is automatically converted into hump naming parameter in Java result set object

Result mapping @ results

If the result set is not a Java object but a map, the column names in the map will be the same as the NN in the database_ Like NN, there is no automatic hump conversion. You can use @ result to indicate the result mapping, and the same applies to Java objects

Using list < Map > does not need to maintain POJO. It is suitable for scenarios where database fields are uncertain or often change. However, the readability and maintainability of the program are not as good as list < user >

Reusable @ results

Assign the ID user when declaring

In other methods, the result mapping with ID user is reused

@ResultMap("user")

Print SQL log to console

In application Add the following configuration to properties

logging. level. Your bag name Mybatis interface package = debug

When executing SQL, the SQL log will be printed on the console

The first line: = = > on the left is the interface and method for executing SQL, and on the right is the execution statement

Line 2: pass parameter 1, string type

The third row: a row of data is found

paging

First, in POM The paging plug-in at the beginning of the article is introduced into XML, and the paging logic is written in the service layer.

How it works: PageHelper Startpage will intercept the next SQL, that is, mybatisdao SQL for findall(). According to the syntax of the current database, the SQL is transformed into a high-performance paging SQL. At the same time, the total row number of the table will be queried. See the SQL log for details.

pageHelper. Startpage and mybatisdao Findall() should be closely followed, and there should be no other logic in the middle, otherwise a bug may appear.

Page < user > page: equivalent to a list set. After the findall() method completes the query, it will assign values to the relevant parameters of the page object

Return ID

Suppose that the ID primary key of the database table is automatically increased. Now add a piece of data. To get the ID of the data automatically increased, the method is as follows

Dao layer

Usegeneratedkeys = true: get the primary key generated by the database

Keyproperty = "Id": store the primary key value into the ID property of the user param object

Service layer

Console

stored procedure

This is a MySQL stored procedure. Pass in an ID value. Query the name value according to this ID and use it as the outgoing parameter

Dao layer

Mode = in: pass in the parameter, that is, user id

Mode = out: outgoing parameter, i.e. user name

StatementType. Callable: indicates that this is a stored procedure

Service layer

results of enforcement

summary

The above is the full note solution of spring boot mybatis introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>