mybatis 遇到的坑:<if test=““>判断空值情况

当在写sql时可能会遇到 需要不为空执行一个情况,判断是空值执行另一个情况

比如:

判断地址字段不为空的进行更新,判断为空的填写默认值

<if test="record.address!= null and record.address !='' " >

ADDRESS=#{record.address}

</if>

<if test="record.address== null or record.address=='' " >

ADDRESS='默认的值'

</if>

此时走默认情况下就不会进行正常的更新

原因:因为mybatis会把''解析为字符,java是强类型语言

修改: 使用 toString()  进行转换

<if test="record.address== null or record.address==''.toString() " >

ADDRESS='默认的值'

</if>

foreach 内嵌入 <trim prefix="(" suffix=")" suffixOverrides=","> <if test="searchValue != null">searchvalue,</if> <if test="createBy != null">createby,</if> <if test="createTime != null">createtime,</if> <if test="updateBy != null">updateby,</if> <if test="updateTime != null">updatetime,</if> <if test="remark != null">remark,</if> <if test="params != null">params,</if> <if test="id != null">id,</if> <if test="serialId != null and serialId != ''">serialid,</if> <if test="purchaserOrganizationId != null and purchaserOrganizationId != ''">purchaserorganizationid,</if> <if test="organizationId != null and organizationId != ''">organizationid,</if> <if test="vehicleId != null and vehicleId != ''">vehicleid,</if> <if test="purchaserOrganizationCode != null">purchaserorganizationcode,</if> <if test="erpPlantCd != null">erpplantcd,</if> <if test="materialCode != null">materialcode,</if> <if test="vehicleSeries != null">vehicleseries,</if> <if test="batchNo != null and batchno != ''">batchno,</if> <if test="arrivalYymm != null and arrivalYymm != ''">arrivalyymm,</if> <if test="quantity != null and quantity != ''">quantity,</if> <if test="contractNo != null and contractNo != ''">contractno,</if> <if test="lotNo != null and lotNo != ''">lotno,</if> <if test="itemId != null and itemId != ''">itemid,</if> <if test="itemCode != null">itemcode,</if> <if test="partDiv != null">partdiv,</if> <if test="kdType != null">kdtype,</if> <if test="pushFlag != null">pushflag,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="searchValue != null">#{searchValue},</if> <if test="createBy != null">#{createBy},</if> <if test="createTime != null">#{createTime},</if> <if test="updateBy != null">#{updateBy},</if> <if test="updateTime != null">#{updateTime},</if> <if test="remark != null">#{remark},</if> <if test="params != null">#{params},</if> <if test="id != null">#{id},</if> <if test="serialId != null and serialId != ''">#{serialId},</if> <if test="purchaserOrganizationId != null and purchaserOrganizationId != ''">#{purchaserOrganizationId},</if> <if test="organizationId != null and organizationId != ''">#{organizationId},</if> <if test="vehicleId != null and vehicleId != ''">#{vehicleId},</if> <if test="purchaserOrganizationCode != null">#{purchaserOrganizationCode},</if> <if test="erpPlantCd != null">#{erpPlantCd},</if> <if test="materialCode != null">#{materialCode},</if> <if test="vehicleSeries != null">#{vehicleSeries},</if> <if test="batchNo != null and batchNo != ''">#{batchNo},</if> <if test="arrivalYymm != null and arrivalYymm != ''">#{arrivalYymm},</if> <if test="quantity != null and quantity != ''">#{quantity},</if> <if test="contractNo != null and contractNo != ''">#{contractNo},</if> <if test="lotNo != null and lotNo != ''">#{lotNo},</if> <if test="itemId != null and itemId != ''">#{itemId},</if> <if test="itemCode != null">#{itemCode},</if> <if test="partDiv != null">#{partDiv},</if> <if test="kdType != null">#{kdType},</if> <if test="pushFlag != null">#{pushFlag},</if> </trim>
最新发布
11-15
<?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.ruoyi.bpm.mapper.RfProjectReportMapper"> <resultMap type="RfProjectReport" id="RfProjectReportResult"> <result property="id" column="id" /> <result property="userId" column="user_id" /> <result property="userName" column="user_name" /> <result property="applicant" column="applicant" /> <result property="department" column="department" /> <result property="processNo" column="process_no" /> <result property="title" column="title" /> <result property="projectCategory" column="project_category" /> <result property="signCompany" column="sign_company" /> <result property="projectName" column="project_name" /> <result property="projectNo" column="project_no" /> <result property="clientCompanyName" column="client_company_name" /> <result property="clientShortCode" column="client_short_code" /> <result property="salesLeader" column="sales_leader" /> <result property="initialContact" column="initial_contact" /> <result property="contactInfo" column="contact_info" /> <result property="position" column="position" /> <result property="reportTime" column="report_time" /> <result property="projectLocation" column="project_location" /> <result property="planStartTime" column="plan_start_time" /> <result property="planEndTime" column="plan_end_time" /> <result property="budgetAmount" column="budget_amount" /> <result property="projectType" column="project_type" /> <result property="afterSalesType" column="after_sales_type" /> <result property="projectIntro" column="project_intro" /> <result property="projectLevel" column="project_level" /> <result property="remark" column="remark" /> <result property="createBy" column="create_by" /> <result property="createTime" column="create_time" /> <result property="updateBy" column="update_by" /> <result property="updateTime" column="update_time" /> </resultMap> <sql id="selectRfProjectReportVo"> select id, user_id, user_name, department, title, project_category, sign_company, project_name, project_no, client_company_name, client_short_code, sales_leader, initial_contact, contact_info, position, report_time, project_location, plan_start_time, plan_end_time, budget_amount, project_type, after_sales_type, project_intro, project_level, remark, create_by, create_time, update_by, update_time from rf_project_report </sql> <select id="selectRfProjectReportList" parameterType="RfProjectReport" resultMap="RfProjectReportResult"> <include refid="selectRfProjectReportVo"/> <where> <if test="userId != null "> and user_id = #{userId}</if> <if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if> <if test="department != null and department != ''"> and department = #{department}</if> <if test="title != null and title != ''"> and title = #{title}</if> <if test="projectCategory != null and projectCategory != ''"> and project_category = #{projectCategory}</if> <if test="signCompany != null and signCompany != ''"> and sign_company = #{signCompany}</if> <if test="projectName != null and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if> <if test="projectNo != null and projectNo != ''"> and project_no = #{projectNo}</if> <if test="clientCompanyName != null and clientCompanyName != ''"> and client_company_name like concat('%', #{clientCompanyName}, '%')</if> <if test="clientShortCode != null and clientShortCode != ''"> and client_short_code = #{clientShortCode}</if> <if test="salesLeader != null and salesLeader != ''"> and sales_leader = #{salesLeader}</if> <if test="initialContact != null and initialContact != ''"> and initial_contact = #{initialContact}</if> <if test="contactInfo != null and contactInfo != ''"> and contact_info = #{contactInfo}</if> <if test="position != null and position != ''"> and position = #{position}</if> <if test="reportTime != null "> and report_time = #{reportTime}</if> <if test="projectLocation != null and projectLocation != ''"> and project_location = #{projectLocation}</if> <if test="planStartTime != null "> and plan_start_time = #{planStartTime}</if> <if test="planEndTime != null "> and plan_end_time = #{planEndTime}</if> <if test="budgetAmount != null and budgetAmount != ''"> and budget_amount = #{budgetAmount}</if> <if test="projectType != null and projectType != ''"> and project_type = #{projectType}</if> <if test="afterSalesType != null and afterSalesType != ''"> and after_sales_type = #{afterSalesType}</if> <if test="projectIntro != null and projectIntro != ''"> and project_intro = #{projectIntro}</if> <if test="projectLevel != null and projectLevel != ''"> and project_level = #{projectLevel}</if> <if test="remark != null and remark !=''" > and remark = #{remark}</if> <if test="createBy != null and createBy !=''" > and create_by = #{createBy}</if> <if test="createTime != null and createTime !=''" > and create_time = #{createTime}</if> <if test="updateBy != null and updateBy !=''" > and update_by = #{updateBy}</if> <if test="updateTime != null and updateTime !=''" > and update_time = #{updateTime}</if> </where> </select> <select id="selectRfProjectReportById" parameterType="Integer" resultMap="RfProjectReportResult"> <include refid="selectRfProjectReportVo"/> where id = #{id} </select> <insert id="insertRfProjectReport" parameterType="RfProjectReport"> insert into rf_project_report <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null">id,</if> <if test="userId != null">user_id,</if> <if test="userName != null">user_name,</if> <if test="department != null">department,</if> <if test="title != null">title,</if> <if test="projectCategory != null">project_category,</if> <if test="signCompany != null">sign_company,</if> <if test="projectName != null">project_name,</if> <if test="projectNo != null">project_no,</if> <if test="clientCompanyName != null">client_company_name,</if> <if test="clientShortCode != null">client_short_code,</if> <if test="salesLeader != null">sales_leader,</if> <if test="initialContact != null">initial_contact,</if> <if test="contactInfo != null">contact_info,</if> <if test="position != null">position,</if> <if test="reportTime != null">report_time,</if> <if test="projectLocation != null">project_location,</if> <if test="planStartTime != null">plan_start_time,</if> <if test="planEndTime != null">plan_end_time,</if> <if test="budgetAmount != null">budget_amount,</if> <if test="projectType != null">project_type,</if> <if test="afterSalesType != null">after_sales_type,</if> <if test="projectIntro != null">project_intro,</if> <if test="projectLevel != null">project_level,</if> <if test="remark != null">remark,</if> <if test="createBy != null">create_by,</if> <if test="createTime != null">create_time,</if> <if test="updateBy != null">update_by,</if> <if test="updateTime != null">update_time,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null">#{id},</if> <if test="userId != null">#{userId},</if> <if test="userName != null">#{userName},</if> <if test="department != null">#{department},</if> <if test="title != null">#{title},</if> <if test="projectCategory != null">#{projectCategory},</if> <if test="signCompany != null">#{signCompany},</if> <if test="projectName != null">#{projectName},</if> <if test="projectNo != null">#{projectNo},</if> <if test="clientCompanyName != null">#{clientCompanyName},</if> <if test="clientShortCode != null">#{clientShortCode},</if> <if test="salesLeader != null">#{salesLeader},</if> <if test="initialContact != null">#{initialContact},</if> <if test="contactInfo != null">#{contactInfo},</if> <if test="position != null">#{position},</if> <if test="reportTime != null">#{reportTime},</if> <if test="projectLocation != null">#{projectLocation},</if> <if test="planStartTime != null">#{planStartTime},</if> <if test="planEndTime != null">#{planEndTime},</if> <if test="budgetAmount != null">#{budgetAmount},</if> <if test="projectType != null">#{projectType},</if> <if test="afterSalesType != null">#{afterSalesType},</if> <if test="projectIntro != null">#{projectIntro},</if> <if test="projectLevel != null">#{projectLevel},</if> <if test="remark != null">#{remark},</if> <if test="createBy != null">#{createBy},</if> <if test="createTime != null">#{createTime},</if> <if test="updateBy != null">#{updateBy},</if> <if test="updateTime != null">#{updateTime},</if> </trim> </insert> <update id="updateRfProjectReport" parameterType="RfProjectReport"> update rf_project_report <trim prefix="SET" suffixOverrides=","> <if test="userId != null">user_id = #{userId},</if> <if test="userName != null">user_name = #{userName},</if> <if test="department != null">department = #{department},</if> <if test="title != null">title = #{title},</if> <if test="projectCategory != null">project_category = #{projectCategory},</if> <if test="signCompany != null">sign_company = #{signCompany},</if> <if test="projectName != null">project_name = #{projectName},</if> <if test="projectNo != null">project_no = #{projectNo},</if> <if test="clientCompanyName != null">client_company_name = #{clientCompanyName},</if> <if test="clientShortCode != null">client_short_code = #{clientShortCode},</if> <if test="salesLeader != null">sales_leader = #{salesLeader},</if> <if test="initialContact != null">initial_contact = #{initialContact},</if> <if test="contactInfo != null">contact_info = #{contactInfo},</if> <if test="position != null">position = #{position},</if> <if test="reportTime != null">report_time = #{reportTime},</if> <if test="projectLocation != null">project_location = #{projectLocation},</if> <if test="planStartTime != null">plan_start_time = #{planStartTime},</if> <if test="planEndTime != null">plan_end_time = #{planEndTime},</if> <if test="budgetAmount != null">budget_amount = #{budgetAmount},</if> <if test="projectType != null">project_type = #{projectType},</if> <if test="afterSalesType != null">after_sales_type = #{afterSalesType},</if> <if test="projectIntro != null">project_intro = #{projectIntro},</if> <if test="projectLevel != null">project_level = #{projectLevel},</if> <if test="remark != null">remark = #{remark},</if> <if test="createBy != null">create_by = #{createBy},</if> <if test="createTime != null">create_time = #{createTime},</if> <if test="updateBy != null">update_by = #{updateBy},</if> <if test="updateTime != null">update_time = #{updateTime},</if> </trim> where id = #{id} </update> <delete id="deleteRfProjectReportById" parameterType="Integer"> delete from rf_project_report where id = #{id} </delete> <delete id="deleteRfProjectReportByIds" parameterType="String"> delete from rf_project_report where id in <foreach item="id" collection="array" open="(" separator="," close=")"> #{id} </foreach> </delete> </mapper>怎么解决
09-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值