欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

在mybatis中使用mapper进行if条件判断

时间:2021-12-03 09:27:23|栏目:JAVA代码|点击:

目的:

在使用mybatis框架中mapper文件有自动生成,但有时需要自己添加sql语句进行开发,当遇到需要使用 if进行条件判断的时候该怎么写?

查询sql语句如下:

<select id="queryData" parameterType="com.pojo.QueryDetailReq" resultType="com.pojo.MxDataInfo">
 select * from db_trd.tb_trd_secu_order where order_status=6
  <if test="channelNo!= null" >
   and channel_no = #{channelNo,jdbcType=INTEGER}
  </if>
  <if test="reportNo!=null" >
   and report_no = #{reportNo,jdbcType=INTEGER}
  </if>
  <if test="companyNo!= null" >
   and company_no = #{companyNo,jdbcType=VARCHAR}
  </if>
  <if test="orderNo!=null" >
   and order_no = #{orderNo,jdbcType=INTEGER}
  </if>
  <if test="stockCode!=null" >
   and stock_code = #{stockCode,jdbcType=VARCHAR}
  </if>
 </select>

语句解析:

1、if语句的格式 ;

2、test中的字段 为parameterType中 com.pojo.QueryDetailReq 的对象 (入参)

3、resultType 为返回查询数据对象 (结果集)

补充:mabatis mapper文件中 使用if条件插入字段和数据

有时候我们插入数据库数据的时候,插入字段都是不确定的,那么我们也可以用if条件来过滤一些字段

废话不多说,直接上代码

<insert id="ORDER_I" parameterType="hashmap">
  insert into t_order
  <trim prefix="(" suffix=")" suffixOverrides=",">
   <if test="orderNo != null">
    orderNo,
   </if>
   <if test="serviceName != null">
    serviceName,
   </if>
   <if test="idcard != null">
    idcard,
   </if>
   <if test="name != null">
    name,
   </if>
   <if test="requestData != null">
    requestData,
   </if>
   <if test="responseData != null">
    responseData,
   </if>
   <if test="status != null">
    status,
   </if>
   <if test="updatedTime != null">
    updatedTime,
   </if>
   <if test="completionTime != null">
    completionTime,
   </if>
   <if test="bae007 != null">
    bae007,
   </if>
   <if test="operId != null">
    operId,
   </if>
   <if test="operName != null">
    operName,
   </if>
   <if test="remark != null">
    remark,
   </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
   <if test="orderNo != null">
    #{orderNo},
   </if>
   <if test="serviceName != null">
    #{serviceName},
   </if>
   <if test="idcard != null">
    #{idcard},
   </if>
   <if test="name != null">
    #{name},
   </if>
   <if test="requestData != null">
    #{requestData},
   </if>
   <if test="responseData != null">
    #{responseData},
   </if>
   <if test="status != null">
    #{status},
   </if>
   <if test="updatedTime != null">
    #{updatedTime},
   </if>
   <if test="completionTime != null">
    #{completionTime},
   </if>
   <if test="bae007 != null">
    #{bae007},
   </if>
   <if test="operId != null">
    #{operId},
   </if>
   <if test="operName != null">
    #{operName},
   </if>
   <if test="remark != null">
    #{remark},
   </if>
  </trim>
 </insert>

经过测试,是可以实现的。

上一篇:Spring boot 基本部署方式

栏    目:JAVA代码

下一篇:详解Zookeeper基础知识

本文标题:在mybatis中使用mapper进行if条件判断

本文地址:http://www.codeinn.net/misctech/185667.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有