Basic use of mybatis


Welcome to the community markdown editor!

Mybatis is a semi ORM persistence layer framework. Its advantage is that it can write native SQL statements in mapper. The effect is obvious. Developers can easily control the execution of SQL. They can write logical SQL through dynamic SQL tags. The writing of SQL is flexible and the management readability of SQL is high;

Mybatis supports annotation to write SQL. You only need to paste the annotation @ mapper on the mapper interface and the corresponding crud annotation on the method to write SQL inside the annotation;

@Select(“SELECT * FROM t_order_info WHERE order_no=#{orderNo}”)

1、 Configuration

XML mode:

In particular, the path of the mapper file and the path of the mapper interface should be consistent. The same is true in spring or springboot. If the mapper location is different, it can be configured in the configuration file

The configuration file is as follows:

<? xml version="1.0" encoding="UTF-8"?>


The above is the configuration of integrating mybatis in the spring project. If the configuration is spring boot, the configuration is as follows

#Modify the port to 80

It’s better to use the boot package instead of the native dependency to use springboot


Annotation method:

Configuration file – the configuration file is in YML format



    url: jdbc:mysql://localhost:3306/wolfcode_goods?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

    driverClassName: com.mysql.jdbc.Driver


    username: root

    password: root

    filters: stat

    maxActive: 1000

    initialSize: 100

    maxWait: 60000

    minIdle: 500

    timeBetweenEvictionRunsMillis: 60000

    minEvictableIdleTimeMillis: 300000

    testOnBorrow: false

    testOnReturn: false

    testWhileIdle: true

    poolPreparedStatements: true

    maxOpenPreparedStatements: 20

    validationQuery: select 'x'



    default-fetch-size: 100

    default-statement-timeout: 3000

    map-underscore-to-camel-case: true

There are two kinds of annotations: if it is simple SQL, use the basic annotation @ select @ insert@update @delete

One is to add more providers after @ selectprovider;

Here is a brief introduction to the differences between the two, and I’ll show you some examples

Take the query as an example to query the commodity data whose gooid is equal to the parameter we passed
If we need to query the condition is not so simple, we need dynamic splicing, then the above method will be very troublesome, but the following method will be much simpler

In this way, we can see at a glance that when the SQL needs to be written as dynamic SQL, we can use the annotation method of adding provider;

For the first time, I hope you can find some mistakes and correct them. Thank you

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Using shell to simulate TCP protocol

Background of the problem The company has a set of message push system (hereinafter referred to as GCM), which takes over the client part due to personnel changes. After looking at the document, there are only a few pages of simple instructions in the communication protocol part. The code is too many and messy to […]