Get the App
SLTechnology News&Howtos  ›  Database  › 

MyBatis-Plus custom sql statement

Shulou Source: shulou.com Published: 2022-06-01 13:11:01 09月18日 Update

I. introduction

Although the conditional constructor that comes with MP is very powerful, sometimes you can't avoid writing a slightly more complex business sql, so let's talk about how MP customizes sql statements today.

II. Configuration

Of course, the custom sql is written in the XML file, so first define the location of the xml file, in the yml configuration file as follows

Mybatis-plus:

# if you put it in the src/main/java directory, classpath:/com/*/*/mapper/*Mapper.xml

# if you put it in the resource directory classpath:/mapper/**.xml

Mapper-locations: classpath:/mapper/**.xml

III. Concrete realization

Use annotations to implement:

You can define a custom method in our Mapper interface.

/ * *

* @ Auther: IT bitch

* @ Date: 14:40 on 2019-6-10

* @ Description: User object persistence layer

* /

Public interface UserMapper extends BaseMapper {

/ * *

*

* if the custom method also wants to be able to use the Wrapper conditional constructor provided by MP, you need to write as follows

*

* @ param userWrapper

* @ return

* /

@ Select ("SELECT * FROM user ${ew.customSqlSegment}")

List selectByMyWrapper (@ Param (Constants.WRAPPER) Wrapper userWrapper)

/ * *

* consistent with Mybatis usage

* @ param name

* @ return

* /

@ Select ("SELECT * FROM user where name = # {name}")

List selectByName (@ Param ("name") String name)

}

Use xml files to implement:

Be sure to specify the location of the xml file when using xml

/ * *

* @ Auther: IT bitch

* @ Date: 14:40 on 2019-6-10

* @ Description: User object persistence layer

* /

Public interface UserMapper extends BaseMapper {

/ * *

*

* if the custom method also wants to be able to use the Wrapper conditional constructor provided by MP, you need to write as follows

* Wuxi Gynecology Hospital http://www.xasgfk.cn/

* @ param userWrapper

* @ return

* /

List selectByMyWrapper (@ Param (Constants.WRAPPER) Wrapper userWrapper)

/ * *

* consistent with Mybatis usage

* @ param name

* @ return

* /

List selectByName (@ Param ("name") String name)

}

SELECT * FROM user where name = # {name}

SELECT * FROM user ${ew.customSqlSegment}

Test:

/ * *

* Custom sql query statement

* /

@ Test

Public void selectByMySelect () {

List users = userMapper.selectByName ("Wang Tianfeng")

Users.forEach (System.out::println)

}

/ * *

* Custom sql using Wrapper

* /

@ Test

Public void selectByMyWrapper () {

QueryWrapper wrapper = new QueryWrapper ()

Wrapper.like ("name", "rain") lt ("age", 40)

List users = userMapper.selectByMyWrapper (wrapper)

Users.forEach (System.out::println)

}

Tags: File method condition constructor statement consistency location usage writing object directory test configuration complex powerful business hospital gynecology introduction where Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn OPPO Reno Shulou Information Shulou Tech Info