mybatis使用in查询以及报java.lang.NullPointerException: target is null for method size异常解决办法
一、解决办法
<if test="param.projectIdList !=null and param.projectIdList.size()>0">
and project_id in
<foreach collection="list" item="projectId" index="index" open="(" close=")" separator=",">
#{projectId}
</foreach>
</if>
在外层条件判断处,不等于空并且size大小大于0这两个条件都需具备
二、如何使用
1、建立通用查询结果列
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,settlement_id,project_id,project_type_id,project_no,batno,active_code,name,telephone,sex,id_number,account_no,account_name,bank_name,bank_province,bank_city,account_phone,issue_amount,issue_time,invoice_amount,audit_status,transfer_status,err_msg,receipt_url,invoice_url,tax_personal,tax_value_add,staff_code,work_in,position,actived_time,contract_no,contract_file,contract_sign,contract_sign_status,create_time,update_time,deleted,calculation_method,service_amount
</sql>
以下字段填写你所需查询的字段
id,settlement_id,project_id,project_type_id,project_no,batno,active_code,name,telephone,sex,id_number,account_no,account_name,bank_name,bank_province,bank_city,account_phone,issue_amount,issue_time,invoice_amount,audit_status,transfer_status,err_msg,receipt_url,invoice_url,tax_personal,tax_value_add,staff_code,work_in,position,actived_time,contract_no,contract_file,contract_sign,contract_sign_status,create_time,update_time,deleted,calculation_method,service_amount
2、编写查询语句
<select id="getProjectStaffPageList" resultType="io.tensoon.springbootplus.business.vo.ProjectStaffQueryVo">
select
<include refid="Base_Column_List"/>
from
project_staff
where
transfer_status=1 and audit_status=1
<if test="param.projectId!=null">
and project_id = #{param.projectId}
</if>
</select>
3、使用in查询
<if test="param.projectIdList !=null and param.projectIdList.size()>0">
and project_id in
<foreach collection="list" item="projectId" index="index" open="(" close=")" separator=",">
#{projectId}
</foreach>
</if>
<select id="getProjectStaffPageList" resultType="io.tensoon.springbootplus.business.vo.ProjectStaffQueryVo">
select
<include refid="Base_Column_List"/>
from
project_staff
where
transfer_status=1 and audit_status=1
<if test="param.projectId!=null">
and project_id = #{param.projectId}
</if>
<if test="param.projectIdList !=null and param.projectIdList.size()>0">
and project_id in
<foreach collection="list" item="projectId" index="index" open="(" close=")" separator=",">
#{projectId}
</foreach>
</if>
</select>
collection: 传入参数的类型 数组用array 列表使用list
item: 传入参数的赋值对象 一般为#{projectId}此处projectId值
index: 起始位置
open="(" : 以 ‘(’ 开始
close=")": 以 ‘)’ 结束
separator=",": 以 ‘,’ 分隔开
每赋一个值都填充进 ‘(’ 和 ‘)’ 中间并以 ‘,’ 分隔开
最后拼接结果为: ‘(第一个值,第二个值,…)’