MyBatis-Plus 自定義sql語(yǔ)句的實(shí)現(xiàn)
一、引言
Good Good Study,Day Day Up,童鞋點(diǎn)個(gè)關(guān)注,不迷路,么么噠~~~
MP自帶的條件構(gòu)造器雖然很強(qiáng)大,有時(shí)候也避免不了寫(xiě)稍微復(fù)雜一點(diǎn)業(yè)務(wù)的sql,那么那么今天說(shuō)說(shuō)MP怎么自定義sql語(yǔ)句吧。
二、配置
自定義的sql當(dāng)然是寫(xiě)在XML文件中的啦,那么首先來(lái)定義xml文件的位置,在yml配置文件如下
mybatis-plus: # 如果是放在src/main/java目錄下 classpath:/com/*/*/mapper/*Mapper.xml # 如果是放在resource目錄 classpath:/mapper/**.xml mapper-locations: classpath:/mapper/**.xml
三、具體實(shí)現(xiàn)
使用注解實(shí)現(xiàn):
在我們Mapper接口中定義自定義方法即可。
/**
* @Auther: IT賤男
* @Date: 2019/6/10 14:40
* @Description: User對(duì)象持久層
*/
public interface UserMapper extends BaseMapper<User> {
/**
*
* 如果自定義的方法還希望能夠使用MP提供的Wrapper條件構(gòu)造器,則需要如下寫(xiě)法
*
* @param userWrapper
* @return
*/
@Select("SELECT * FROM user ${ew.customSqlSegment}")
List<User> selectByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper);
/**
* 和Mybatis使用方法一致
* @param name
* @return
*/
@Select("SELECT * FROM user where name = #{name}")
List<User> selectByName(@Param("name") String name);
}
使用xml文件實(shí)現(xiàn):
使用xml一定要指定xml文件所在位置
/**
* @Auther: IT賤男
* @Date: 2019/6/10 14:40
* @Description: User對(duì)象持久層
*/
public interface UserMapper extends BaseMapper<User> {
/**
*
* 如果自定義的方法還希望能夠使用MP提供的Wrapper條件構(gòu)造器,則需要如下寫(xiě)法
*
* @param userWrapper
* @return
*/
List<User> selectByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper);
/**
* 和Mybatis使用方法一致
* @param name
* @return
*/
List<User> selectByName(@Param("name") String name);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="selectByName" resultType="com.example.demo.model.User">
SELECT * FROM user where name = #{name}
</select>
<select id="selectByMyWrapper" resultType="com.example.demo.model.User">
SELECT * FROM user ${ew.customSqlSegment}
</select>
</mapper>
測(cè)試測(cè)試:
/**
* 自定義sql查詢語(yǔ)句
*/
@Test
public void selectByMySelect() {
List<User> users = userMapper.selectByName("王天風(fēng)");
users.forEach(System.out::println);
}
/**
* 自定義sql使用Wrapper
*/
@Test
public void selectByMyWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper();
wrapper.like("name", "雨").lt("age", 40);
List<User> users = userMapper.selectByMyWrapper(wrapper);
users.forEach(System.out::println);
}
四、回答評(píng)論區(qū)提問(wèn)
提問(wèn)一:能否提供自定義Update語(yǔ)句與Wrapper的整合使用呢?
/**
* <p>
* 用戶 Mapper 接口
* </p>
*
* @author IT賤男
* @since 2019-06-14
*/
public interface UserMapper extends BaseMapper<User> {
/**
* 自定Wrapper修改
*
* @param userWrapper 條件構(gòu)造器
* @param user 修改的對(duì)象參數(shù)
* @return
*/
int updateByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper, @Param("user") User user);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<update id="updateByMyWrapper">
UPDATE user SET email = #{user.email} ${ew.customSqlSegment}
</update>
</mapper>
@Test
public void updateByMyWrapper() {
// 條件構(gòu)造器
QueryWrapper<User> wrapper = new QueryWrapper();
wrapper.eq("name", "admin");
// 修改后的對(duì)象
User user = new User();
user.setEmail("Test@email.com");
userMapper.updateByMyWrapper(wrapper, user);
}
到此這篇關(guān)于MyBatis-Plus 自定義sql語(yǔ)句的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 自定義sql語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus自帶QueryWrapper自定義sql實(shí)現(xiàn)復(fù)雜查詢實(shí)例詳解
- MyBatisPlus自定義SQL的實(shí)現(xiàn)
- mybatis-plus使用xml自定義sql語(yǔ)句方式
- MyBatis-plus如何執(zhí)行自定義SQL
- Mybatis-Plus實(shí)現(xiàn)自定義SQL具體方法
- MyBatis-Plus自定義SQL的詳細(xì)過(guò)程記錄
- MybatisPlus使用queryWrapper如何實(shí)現(xiàn)復(fù)雜查詢
- MyBatis-Plus自定義SQL和復(fù)雜查詢的實(shí)現(xiàn)
相關(guān)文章
Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析三之后端反向調(diào)用前端
Dwr是為人所熟知的前端框架,其異步推送功能是為人所津津樂(lè)道的,下來(lái)主要研究一下它的這個(gè)功能是怎么應(yīng)用的;2016-04-04
如何基于Autowired對(duì)構(gòu)造函數(shù)進(jìn)行注釋
這篇文章主要介紹了如何基于Autowired對(duì)構(gòu)造函數(shù)進(jìn)行注釋,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
分享Spring?Cloud?OpenFeign?的五個(gè)優(yōu)化技巧
這篇文章主要分享的是Spring?Cloud?OpenFeign?的五個(gè)優(yōu)化技巧,OpenFeign?是?Spring?官方推出的一種聲明式服務(wù)調(diào)用和負(fù)載均衡組件,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-05-05
SpringBoot整合Guava Cache實(shí)現(xiàn)全局緩存的示例代碼
這篇文章主要介紹了SpringBoot整合Guava Cache實(shí)現(xiàn)全局緩存,Guava Cache是Google Guava庫(kù)中的一個(gè)模塊,提供了基于內(nèi)存的本地緩存實(shí)現(xiàn),文中介紹了SpringBoot整合使用Guava Cache的具體步驟,需要的朋友可以參考下2024-03-03
java實(shí)現(xiàn)查找文本內(nèi)容替換功能示例
文本替換幾乎是所有文本編輯器都支持的功能,但是要限制在編輯其中才可以執(zhí)行該功能。本實(shí)例實(shí)現(xiàn)了制定文本文件的內(nèi)容替換,并且不需要再編輯其中打開(kāi)文本文件2014-02-02
關(guān)于weblogic部署Java項(xiàng)目的包沖突問(wèn)題的解決
這篇文章主要介紹了關(guān)于weblogic部署Java項(xiàng)目的包沖突問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

