当前位置: 代码迷 >> 综合 >> mybatis传入array
  详细解决方案

mybatis传入array

热度:13   发布时间:2023-09-20 11:38:51.0

1.mapper.java中

List<MacTicket> selectByPrimaryKeyList(String[] stringArray);

mapper.xml中

<select id="selectByPrimaryKeyList" parameterType="int" resultMap="BaseResultMap">

<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">

#{id,jdbcType=VARCHAR}

</foreach>

亲测,重点说下parameterType实际不写也可以,或者写其他的都行,但是collection="array"这个必须写,mapper.java中的String[]也必须写,如果mapper.java传入是对象时,对象中包含数组对象时,在mapper.xml中取值时通过collection=数组名类似map取值。

< if test="arrayName!=null and arrayName.size()>0 ></if>判断不为null和个数大于0,如果写arrayName!=''时会报错,invalid comparision: java.util.Arraylist and java.lang.String

2.mapper.xml中if判断

<sql> sql片段中表的别名例如ticket和sql语句中尽量统一,引用此sql片段的语句的别名遵循sql片段的表别名

<if test="statusCd!=null and statusCd!=''"></if> if判断时null和空串都要判断

<if test="name !=null and name!=''">and ticket.name like concat ('name',#{name},'%')</if>  模糊查询

<if test="createTimeStart=null and createTimeStart!=''">and ticket.create_time &gt;#{createTimeStart}</if> 大于等于

<if test="createTimeEnd=null and createTimeEnd!=''">and ticket.create_time &lt;#{createTimeEnd}</if> 小于等于

</sql>

  相关解决方案