In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
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)
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.