侧边栏壁纸
博主昵称
朱治龙

心中有信仰,脚下有力量。

  • 累计撰写 149 篇文章
  • 累计收到 9 条评论

Mybatis-Plus 自定义SQL

朱治龙
2023-10-18 / 0 评论 / 52 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年10月19日,已超过534天没有更新,若内容或图片失效,请留言反馈。

MybatisPlus自定义SQL.jpg

有的时候使用条件构造器自定义SQL满足不了我们的需求,我们既想使用 Wrapper,又想使用SQL,MP 对这种方式也提供了支持,MP 版本号应≥3.0.7。下面是该方案的实现记录:

实现方案一:Mapper接口中使用@Select注解

1、Mapper 示例代码如下:

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.paratera.protect.entity.Staff;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 朱治龙
 * @date 2023-10-17 11:46:00
 */
public interface StaffMapper extends BaseMapper<Staff> {

    @Select("select * from staff ${ew.customSqlSegment}")
    List<Staff> selectAll(@Param(Constants.WRAPPER)Wrapper<Staff> wrapper);
}

2、调用示例代码:

    @Test
    void testSelfSQL() {
        LambdaQueryWrapper<Staff> lqw = Wrappers.lambdaQuery(Staff.class);
        lqw.eq(Staff::getName, "朱治龙").and(wq2 -> wq2.lt(Staff::getAge, 40).or().isNotNull(Staff::getEmail));
        List<Staff> staffList = staffMapper.selectAll(lqw);
        staffList.forEach(System.out::println);
    }

3、输出结果

DEBUG==>  Preparing: select * from staff WHERE (name = ? AND (age < ? OR email IS NOT NULL))
DEBUG==> Parameters: 朱治龙(String), 40(Integer)
TRACE<==    Columns: id, name, age, email, mobile, manager_id, create_time
TRACE<==        Row: 1714166763984199681, 朱治龙, 36, zhuzl@blsc.cn, 15084978453, 1088248166370832385, 2023-10-17 14:29:38
DEBUG<==      Total: 1
Staff(id=1714166763984199681, name=朱治龙, age=36, email=zhuzl@blsc.cn, mobile=15084978453, managerId=1088248166370832385, createTime=2023-10-17T14:29:38)

实现方案二:使用xml

1、配置xml文件的存放路径。再application.yml中添加 xml 文件的引用路径配置

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

2、在resources目录下添加 mapper 目录,并新建 mapper 文件,如文件名为 StaffMapper.xml,示例内容为:

<?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.paratera.protect.dao.StaffMapper">
    <select id="selectByXml" resultType="com.paratera.protect.entity.Staff">
        select * from staff ${ew.customSqlSegment}
    </select>
</mapper>

3、Mapper 接口中添加方法,示例代码如下:

package com.paratera.protect.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.paratera.protect.entity.Staff;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author 朱治龙
 * @date 2023-10-17 11:46:00
 */
public interface StaffMapper extends BaseMapper<Staff> {
    

    List<Staff> selectByXml(@Param(Constants.WRAPPER)Wrapper<Staff> wrapper);
}

4、调用示例代码:

    @Test
    void testSelfSQL2() {
        LambdaQueryWrapper<Staff> lqw = Wrappers.lambdaQuery(Staff.class);
        lqw.eq(Staff::getName, "朱治龙").and(wq2 -> wq2.lt(Staff::getAge, 50).or().isNotNull(Staff::getEmail));
        List<Staff> staffList = staffMapper.selectByXml(lqw);
        staffList.forEach(System.out::println);
    }

5、输出结果

DEBUG==>  Preparing: select * from staff WHERE (name = ? AND (age < ? OR email IS NOT NULL))
DEBUG==> Parameters: 朱治龙(String), 50(Integer)
TRACE<==    Columns: id, name, age, email, mobile, manager_id, create_time
TRACE<==        Row: 1714166763984199681, 朱治龙, 36, zhuzl@blsc.cn, 15084978453, 1088248166370832385, 2023-10-17 14:29:38
DEBUG<==      Total: 1
Staff(id=1714166763984199681, name=朱治龙, age=36, email=zhuzl@blsc.cn, mobile=15084978453, managerId=1088248166370832385, createTime=2023-10-17T14:29:38)
0

评论 (0)

OωO
  • ::(呵呵)
  • ::(哈哈)
  • ::(吐舌)
  • ::(太开心)
  • ::(笑眼)
  • ::(花心)
  • ::(小乖)
  • ::(乖)
  • ::(捂嘴笑)
  • ::(滑稽)
  • ::(你懂的)
  • ::(不高兴)
  • ::(怒)
  • ::(汗)
  • ::(黑线)
  • ::(泪)
  • ::(真棒)
  • ::(喷)
  • ::(惊哭)
  • ::(阴险)
  • ::(鄙视)
  • ::(酷)
  • ::(啊)
  • ::(狂汗)
  • ::(what)
  • ::(疑问)
  • ::(酸爽)
  • ::(呀咩爹)
  • ::(委屈)
  • ::(惊讶)
  • ::(睡觉)
  • ::(笑尿)
  • ::(挖鼻)
  • ::(吐)
  • ::(犀利)
  • ::(小红脸)
  • ::(懒得理)
  • ::(勉强)
  • ::(爱心)
  • ::(心碎)
  • ::(玫瑰)
  • ::(礼物)
  • ::(彩虹)
  • ::(太阳)
  • ::(星星月亮)
  • ::(钱币)
  • ::(茶杯)
  • ::(蛋糕)
  • ::(大拇指)
  • ::(胜利)
  • ::(haha)
  • ::(OK)
  • ::(沙发)
  • ::(手纸)
  • ::(香蕉)
  • ::(便便)
  • ::(药丸)
  • ::(红领巾)
  • ::(蜡烛)
  • ::(音乐)
  • ::(灯泡)
  • ::(开心)
  • ::(钱)
  • ::(咦)
  • ::(呼)
  • ::(冷)
  • ::(生气)
  • ::(弱)
  • ::(狗头)
泡泡
阿鲁
颜文字
取消