在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>
经过测试,是可以实现的。
栏 目:JAVA代码
下一篇:详解Zookeeper基础知识
本文标题:在mybatis中使用mapper进行if条件判断
本文地址:http://www.codeinn.net/misctech/185667.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




