<if test=“state!=null and state==‘0‘ “> mybatis中使用if test判断参数值得问题

本文探讨了在MyBatis中正确使用条件判断的方法,强调了在OGNL表达式中应使用双引号或toString()进行字符串比较,避免因类型不匹配导致的逻辑错误。

记录一个使用mybatis中的小问题

使用
当条件中有判断值是, 要是用 ==,而不能使用单个 =,
mybatis是用OGNL表达式来解析的,在OGNL的表达式中,’1’会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。
总结下使用方法:单个的字符要写到双引号里面或者使用.toString()才行!

以下为错误示范:

     <if test="customerState!=null and customerState= '1' ">
        and rc.user_id is not null
    </if>
    <if test="customerState!=null and customerState='2'">
        and rc.name_tags_dcount is not null
    </if>
    <if test="customerState!=null and customerState='3'">
        and rc.name_tags_scount is not null
    </if>
    <if test="customerState!=null and customerState='4'">
        and rc.status = '7'
    </if>
    <if test="customerState!=null and customerState='5'">
        and rc.status = '11'
    </if>

查看打印出的sql
WHERE
1 = 1
AND rc.shareId = ?
AND rc.user_id IS NOT NULL
AND rc.name_tags_dcount IS NOT NULL
AND rc.name_tags_scount IS NOT NULL
AND rc.STATUS = ‘7’
AND rc.STATUS = ‘11’
很明显这不是我想要的, 修改一下

	<if test="customerState!=null and customerState== '1'.toString()">
        and rc.user_id is not null
    </if>
    <if test="customerState!=null and customerState=='2'.toString()">
        and rc.name_tags_dcount is not null
    </if>
    <if test="customerState!=null and customerState=='3'.toString()">
        and rc.name_tags_scount is not null
    </if>
    <if test="customerState!=null and customerState=='4'.toString()">
        and rc.status = '7'
    </if>
    <if test="customerState!=null and customerState=='5'.toString()">
        and rc.status = '11'
    </if>

控制台打印sql为

FROM
	room_clues rc
	LEFT JOIN cloud_user cu ON cu.id = rc.user_id
	LEFT JOIN cartype ct ON ct.id = rc.carTypeId
	LEFT JOIN cartype ct1 ON ct1.id = rc.deal_car_type_id 
WHERE
	1 = 1 
	AND rc.shareId = ?
<?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.htsc.pos.api.ta.forwardOrder.mapper.PosDsOrderForwardMapper"> <!-- 基础结果映射 --> <resultMap id="PosDsOrderForwardMap" type="com.htsc.pos.api.ta.forwardOrder.entity.PosDsOrderForward"> <id column="id" property="id" jdbcType="NUMERIC"/> <result column="manager_id" property="managerId" jdbcType="NUMERIC"/> <result column="business_date" property="businessDate" jdbcType="TIMESTAMP"/> <result column="prdt_code" property="prdtCode" jdbcType="VARCHAR"/> <result column="prdt_name" property="prdtName" jdbcType="VARCHAR"/> <result column="inv_name" property="invName" jdbcType="VARCHAR"/> <result column="inv_cust_type" property="invCustType" jdbcType="NUMERIC"/> <result column="inv_cert_type" property="invCertType" jdbcType="VARCHAR"/> <result column="inv_cert_num" property="invCertNum" jdbcType="VARCHAR"/> <result column="business_type" property="businessType" jdbcType="NUMERIC"/> <result column="trading_amount" property="tradingAmount" jdbcType="NUMERIC"/> <result column="trading_share_num" property="tradingShareNum" jdbcType="NUMERIC"/> <result column="charge_discount" property="chargeDiscount" jdbcType="VARCHAR"/> <result column="creator" property="creator" jdbcType="VARCHAR"/> <result column="creator_name" property="creatorName" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="modifier" property="modifier" jdbcType="VARCHAR"/> <result column="modifier_name" property="modifierName" jdbcType="VARCHAR"/> <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP"/> <result column="state" property="state" jdbcType="NUMERIC"/> <result column="trading_account" property="tradingAccount" jdbcType="VARCHAR"/> <result column="is_valid" property="isValid" jdbcType="NUMERIC"/> <result column="cool_off_period_flag" property="coolOffPeriodFlag" jdbcType="NUMERIC"/> <result column="request_no" property="requestNo" jdbcType="VARCHAR"/> <result column="redeem_type" property="redeemType" jdbcType="NUMERIC"/> <result column="charge_type" property="chargeType" jdbcType="NUMERIC"/> <result column="transaction_cost" property="transactionCost" jdbcType="NUMERIC"/> <result column="achievement_fee_discount" property="achievementFeeDiscount" jdbcType="VARCHAR"/> <result column="investor_id" property="investorId" jdbcType="NUMERIC"/> <result column="send_szt_flag" property="sendSztFlag" javaType="NUMERIC"/> </resultMap> <!-- 基础列名 --> <sql id="Base_Column_List"> id, manager_id, business_date, prdt_code, prdt_name, inv_name, inv_cust_type, inv_cert_type, inv_cert_num, business_type, trading_amount, trading_share_num, charge_discount, creator, creator_name, create_time, modifier, modifier_name, modify_time, state, trading_account, is_valid, cool_off_period_flag, request_no, redeem_type, charge_type, transaction_cost, achievement_fee_discount, investor_id </sql> <!-- 根据主键查询 --> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="PosDsOrderForwardMap"> SELECT <include refid="Base_Column_List"/> FROM POS_DS_ORDER_FORWARD WHERE id = #{id,jdbcType=NUMERIC} </select> <!-- 查询条件对象 --> <select id="getPosDsOrderForwardList" parameterType="com.htsc.pos.api.ta.forwardOrder.req.PosDsOrderForwardReq" resultMap="PosDsOrderForwardMap"> SELECT <include refid="Base_Column_List"/> FROM POS_DS_ORDER_FORWARD WHERE <if test="managerId != null"> AND manager_id = #{managerId} </if> <if test="businessDateStart != null"> AND BUSINESS_DATE <![CDATA[>=]]> #{businessDateStart} </if> <if test="businessDateEnd != null"> AND BUSINESS_DATE <![CDATA[<=]]> #{businessDateEnd} </if> <if test="prdtCode != null"> AND PRDT_CODE = #{prdtCode} </if> <if test="prdtCodeList != null "> AND PRDT_CODE IN <foreach item="item" collection="prdtCodeList" index="index" open="(" separator="," close=")"> #{item} </foreach> </if> <if test="invName != null"> AND INV_NAME LIKE '%'||#{invName}||'%' </if> <if test="businessType != null"> AND business_type = #{businessType} </if> <if test="states != null and states.size() > 0"> AND state IN <foreach collection="states" item="state" open="(" close=")" separator=","> #{state} </foreach> </if> <if test="investorId != null"> AND investor_id = #{investorId} </if> <if test="sendSztFlag != null"> AND send_szt_flag = #{sendSztFlag} </if> <if test="isValid != null">AND IS_VALID = #{isValid}</if> ORDER BY ID DESC </select> <!-- 插入记录 --> <insert id="insert" parameterType="com.htsc.pos.api.ta.forwardOrder.entity.PosDsOrderForward"> <selectKey keyProperty="id" resultType="java.lang.Long" order="BEFORE"> SELECT SEQ_POS_DS_ORDER_FORWARD.NEXTVAL FROM DUAL </selectKey> INSERT INTO POS_DS_ORDER_FORWARD ( id, manager_id, business_date, prdt_code, prdt_name, inv_name, inv_cust_type, inv_cert_type, inv_cert_num, business_type, trading_amount, trading_share_num, charge_discount, creator, creator_name, create_time, modifier, modifier_name, modify_time, state, trading_account, is_valid, cool_off_period_flag, request_no, redeem_type, charge_type, transaction_cost, achievement_fee_discount, investor_id ) VALUES ( #{id,jdbcType=NUMERIC}, #{managerId,jdbcType=NUMERIC}, #{businessDate,jdbcType=TIMESTAMP}, #{prdtCode,jdbcType=VARCHAR}, #{prdtName,jdbcType=VARCHAR}, #{invName,jdbcType=VARCHAR}, #{invCustType,jdbcType=NUMERIC}, #{invCertType,jdbcType=VARCHAR}, #{invCertNum,jdbcType=VARCHAR}, #{businessType,jdbcType=NUMERIC}, #{tradingAmount,jdbcType=NUMERIC}, #{tradingShareNum,jdbcType=NUMERIC}, #{chargeDiscount,jdbcType=VARCHAR}, #{creator,jdbcType=VARCHAR}, #{creatorName,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{modifier,jdbcType=VARCHAR}, #{modifierName,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP}, #{state,jdbcType=NUMERIC}, #{tradingAccount,jdbcType=VARCHAR}, #{isValid,jdbcType=NUMERIC}, #{coolOffPeriodFlag,jdbcType=NUMERIC}, #{requestNo,jdbcType=VARCHAR}, #{redeemType,jdbcType=NUMERIC}, #{chargeType,jdbcType=NUMERIC}, #{transactionCost,jdbcType=NUMERIC}, #{achievementFeeDiscount,jdbcType=VARCHAR}, #{investorId,jdbcType=NUMERIC} ) </insert> <!-- 根据主键ID更新记录 --> <update id="updateById" parameterType="com.htsc.pos.api.ta.forwardOrder.entity.PosDsOrderForward"> UPDATE POS_DS_ORDER_FORWARD <set> <if test="managerId != null">manager_id = #{managerId,jdbcType=NUMERIC},</if> <if test="businessDate != null">business_date = #{businessDate,jdbcType=TIMESTAMP},</if> <if test="prdtCode != null">prdt_code = #{prdtCode,jdbcType=VARCHAR},</if> <if test="prdtName != null">prdt_name = #{prdtName,jdbcType=VARCHAR},</if> <if test="invName != null">inv_name = #{invName,jdbcType=VARCHAR},</if> <if test="invCustType != null">inv_cust_type = #{invCustType,jdbcType=NUMERIC},</if> <if test="invCertType != null">inv_cert_type = #{invCertType,jdbcType=VARCHAR},</if> <if test="invCertNum != null">inv_cert_num = #{invCertNum,jdbcType=VARCHAR},</if> <if test="businessType != null">business_type = #{businessType,jdbcType=NUMERIC},</if> <if test="tradingAmount != null">trading_amount = #{tradingAmount,jdbcType=NUMERIC},</if> <if test="tradingShareNum != null">trading_share_num = #{tradingShareNum,jdbcType=NUMERIC},</if> <if test="chargeDiscount != null">charge_discount = #{chargeDiscount,jdbcType=VARCHAR},</if> <if test="modifier != null">modifier = #{modifier,jdbcType=VARCHAR},</if> <if test="modifierName != null">modifier_name = #{modifierName,jdbcType=VARCHAR},</if> <if test="modifyTime != null">modify_time = #{modifyTime,jdbcType=TIMESTAMP},</if> <if test="state != null">state = #{state,jdbcType=NUMERIC},</if> <if test="tradingAccount != null">trading_account = #{tradingAccount,jdbcType=VARCHAR},</if> <if test="isValid != null">is_valid = #{isValid,jdbcType=NUMERIC},</if> <if test="coolOffPeriodFlag != null">cool_off_period_flag = #{coolOffPeriodFlag,jdbcType=NUMERIC},</if> <if test="requestNo != null">request_no = #{requestNo,jdbcType=VARCHAR},</if> <if test="redeemType != null">redeem_type = #{redeemType,jdbcType=NUMERIC},</if> <if test="chargeType != null">charge_type = #{chargeType,jdbcType=NUMERIC},</if> <if test="transactionCost != null">transaction_cost = #{transactionCost,jdbcType=NUMERIC},</if> <if test="achievementFeeDiscount != null">achievement_fee_discount = #{achievementFeeDiscount,jdbcType=VARCHAR},</if> <if test="investorId != null">investor_id = #{investorId,jdbcType=NUMERIC},</if> <if test="sendSztFlag != null">send_szt_flag = #{sendSztFlag,jdbcType=NUMERIC},</if> </set> WHERE id = #{id,jdbcType=NUMERIC} </update> </mapper> 检查一下这个mapper哪里有问题
最新发布
11-10
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

意田天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值