【每周一个编程技巧 - Java笔记】玩转SSM:SpringBoot+Mybatis多条件筛选

2023-11-16

SpringBoot+Mybatis多条件筛选

  在实际的业务开发系统中,做的最多的工作就是增、删、改、查操作,而这部分增、删、改、查的操作中又有80%的都是查询操作。本文记录的主要内容是,基于SpringBoot和Mybatis来实现条件查询的功能。

1. 使用Map作为筛选条件

<select id="selectByCondition" resultMap="BaseResultMap" parameterType="Map">
    select
    <include refid="Base_Column_List"/>
    from t_user
    where 1=1
    <if test="map != null">
      <foreach collection="map" item="value" index="key">
        <if test="value != null and value != ''">
          AND ${key} = #{value}
        </if>
      </foreach>
    </if>
    <if test="offset != null and pageSize != null">
      LIMIT #{offset}, #{pageSize}
    </if>
  </select>

  Service中具体的调用逻辑如下:

public List<User> getByParam(UserListParam param) {
        int offset = 0;
        if (!ObjectUtils.isEmpty(param.getPageIndex()) && !ObjectUtils.isEmpty(param.getPageSize())) {
            offset = (param.getPageIndex() - 1) * param.getPageSize();
        }
        param.setOffset(offset);
        return userMapper.selectByParam(param);
    }

2. 使用对象作为筛选条件

<select id="selectByParam" resultMap="BaseResultMap" parameterType="com.pkh.bean.param.UserListParam" >
    select
    <include refid="Base_Column_List" />
    from t_user
    <where>
      <include refid="join_list_where" />
    </where>
    <if test="sortBy != null and sortBy != ''">
      ORDER BY ${sortBy}
      <if test="sortOrder != null and sortOrder != ''">
        ${sortOrder}
      </if>
    </if>
    <if test="offset != null and pageSize != null">
      LIMIT #{offset}, #{pageSize}
    </if>
  </select>

<sql id="Base_Column_List">
    id, user_id, user_name, `password`, deal_password, sex, `type`, phone, mobie, email, 
    we_chat, telegram, address, zip_code, create_time, update_time
  </sql>

<resultMap id="BaseResultMap" type="com.pkh.dao.po.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="deal_password" jdbcType="VARCHAR" property="dealPassword" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="mobie" jdbcType="VARCHAR" property="mobie" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="we_chat" jdbcType="VARCHAR" property="weChat" />
    <result column="telegram" jdbcType="VARCHAR" property="telegram" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="zip_code" jdbcType="VARCHAR" property="zipCode" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>

  筛选条件定义在id为join_list_where的sql标签中。

<sql id="join_list_where">
    <if test="userId != null and userId != '' ">
      AND user_id = #{userId}
    </if>
    <if test="sex != null and sex != '' ">
      AND sex = #{sex}
    </if>
    <if test="createTime != null and createTime.size() > 0">
      AND create_time <![CDATA[>= ]]> #{createTime[0]} AND create_time <![CDATA[<= ]]> #{createTime[1]}
    </if>
  </sql>

  Service中具体的调用逻辑如下:

public List<User> getByCondition(UserListParam param) {
        int offset = 0;
        if (!ObjectUtils.isEmpty(param.getPageIndex()) && !ObjectUtils.isEmpty(param.getPageSize())) {
            offset = (param.getPageIndex() - 1) * param.getPageSize();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", param.getUserId());
        map.put("sex", param.getSex());
        return userMapper.selectByCondition(map, offset, param.getPageSize());
    }

3. 视频笔记

本篇笔记也在小破站通过视频形式分享,欢迎访问: 小破站视频笔记

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【每周一个编程技巧 - Java笔记】玩转SSM:SpringBoot+Mybatis多条件筛选 的相关文章

随机推荐

  • Python自学之路第九步——用户输入和while循环

    主要用到的是input 函数 他可以接受用户的输入 这样便可以编写交互式的程序了 还介绍了while循环 这个和C中一样 包括if判断都可以尝试测试一下 很有意思 函数input pr 我们将统计您的基本信息 pr n请输入您的名字 nam
  • Mybatis - NoSuchMethodError: net.sf.jsqlparser.statement.select.SetOperationList.getSelects()Ljava/

    昨天在修改一个接口功能时 需要在原来的接口上提供分页和模糊查询 就使用了分页 PageHelper来做 但是在mybatis的xml文件中又使用了UNION来合并查询结果 导致项目启动直接报错 Handler processing fail
  • MySQL 中读写分离可能遇到的问题

    前言 MySQL 中读写分离是经常用到了的架构了 通过读写分离实现横向扩展的能力 写入和更新操作在源服务器上进行 从服务器中进行数据的读取操作 通过增大从服务器的个数 能够极大的增强数据库的读取能力 MySQL 中的高可用架构越已经呈现出越
  • ubuntu 光盘读取

    把光盘放入光驱后 要挂载光驱 将光驱设备挂在到 mnt 下 sudo mount dev sr0 mnt mount dev sr0 is write protected mounting read only 到 mnt目录下就可以看到光盘
  • 一条SQL语句求前面记录的平均值

    有算法要求如下 For i 1 i lt 10 i ta i t 1 t 2 t i i 用一条SQL语句实现它 分别用表变量 ta 和 t 来对应 ta 和 t declare t table id int d decimal 18 4
  • 第一课 认识Python

    相比大家都听说过Python是一门容易学习的语言 那么实际是怎么样呢 我们从以下几点看看 1 首先 Python是最容易学习和最好用的语言 Python容易阅读和编写 比较清晰 格式非常简洁 表达能力强 这样同样写一个程序 Python比其
  • uniApp条件编译以及跳转方法

    1 使用Uniapp的方法获取系统环境 仅在JS中可以使用 uni getSystemInfoSync platform 获取应用所在的平台 if uni getSystemInfoSync platform ios if uni getS
  • pickle与.pkl文件

    经常遇到在Python程序运行中得到了一些字符串 列表 字典等数据 想要保存下来 长长久久的 方便以后使用 这个时候Pickle模块就派上用场了 pickle 模块及其同类模块 cPickle 向 Python 提供了 pickle 支持
  • mipi介绍

    文章目录 1 MIPI简介 1 1 DSI layer 2 D PHY 2 1 D PHY介绍 2 2 电平状态 2 3 lane结构 2 4 data lane操作模式 2 4 1 escape mode和space one hot co
  • kylin启动netstat: n: unknown or uninstrumented protocol

    检查hadoop配置的时候出现问题 报错如下 lcc lcc apache kylin 2 6 0 hbase1x bin check env sh Retrieving hadoop conf dir KYLIN HOME is set
  • 史上最快的实例分割SparseInst Int8量化实录

    近期 YOLOv7里面借鉴 复 制 粘 贴 了一个新的模型 SparseInst 我借助YOLOv7的基建能力 将其导出到了ONNX 获得了一个非常不错的可以直接用OnnxRuntime 或者TensorRT跑的实例分割 后续也可能把lin
  • 数据挖掘十大算法

    参考 ICDM 数据挖掘十大算法
  • flutter 去除超出警报

    给超出内容套上SingleChildScrollView组件即可 SingleChildScrollView child
  • wpf datagrid自动生成列时特殊字符转换

    DataGrid控件可以根据DataTable自动生成行和列 但是如果列名包括一些特殊字符 的时候 会出现无法显示出数据或者显示DataRowView的情况 原因是这些字符是Xaml里用来标识绑定path和xpath的符号 例如我们会这么用
  • C语言补漏:字符串指针与字符数组传参

    字符串指针与字符数组传参 深信服的笔试上被吊打 其中对一道用指针做形参的题目印象十分深刻 借此恶补了一晚上指针 今天总结 以作警示 试想有如下情形 将一个字符串指针做形参赋值函数修改其字符串 函数结束后字符串被改变了吗 include
  • 快速上手Gobject

    转自 http blog csdn net acs713 article details 7778051 What is G object 很多人被灌输了这样一种概念 要写面向对象程序 那么就需要学习一种面向对象编程语言 例如C Java
  • chrome扩展开发调试

    chrome扩展由content scripts browser actions background等多个部分组成 其中 content scripts属于注入web页面 所以在contentscripts中的console log会被正
  • Uncaught TypeError: AMap.MarkerClusterer is not a constructor

    实验点聚合时报错 cluster new AMap MarkerClusterer map markers styles sts gridSize 80 改为 map plugin AMap MarkerClusterer function
  • SQL语法基础

    SQL语法基础 SQL语法基础 SQL 是用于访问和处理数据库的标准的计算机语言 SQL 语句 需要在数据库上执行的大部分工作都由 SQL 语句完成 tips SQL 对大小写不敏感 SELECT 与 select 是相同的 一些最重要的
  • 【每周一个编程技巧 - Java笔记】玩转SSM:SpringBoot+Mybatis多条件筛选

    SpringBoot Mybatis多条件筛选 在实际的业务开发系统中 做的最多的工作就是增 删 改 查操作 而这部分增 删 改 查的操作中又有80 的都是查询操作 本文记录的主要内容是 基于SpringBoot和Mybatis来实现条件查