mybatis in 语句参数传递

传递字符串

接口

    List<DepotHeadVo4InDetail> findByAll(
            @Param("beginTime") String beginTime,
            @Param("endTime") String endTime,
            @Param("dids") String dids);

mapper 定义

    <select id="findByAll" parameterType="com.jsh.erp.datasource.entities.DepotItemExample" resultMap="ResultWithInfoExMap">
        select dh.Number, di.UnitPrice,di.OperNumber,di.AllPrice
        from jsh_depothead dh
        inner join jsh_depotitem di on di.HeaderId=dh.id and ifnull(di.delete_Flag,'0') !='1'
        where dh.OperTime >=#{beginTime} and dh.OperTime &lt;=#{endTime}
        <if test="dids != null and dids != ''">
            and di.DepotId in
            <foreach item="did" index="index" collection="dids.split(',')" open="(" separator="," close=")">
                #{did}
            </foreach>
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        ORDER BY OperTime DESC,Number desc
    </select>

传递字符串数组

接口

    int batchDeleteSerialNumberByIds(@Param("updateTime") Date updateTime,
        @Param("updater") Long updater, @Param("ids") String ids[]);

mapper 定义

    <update id="batchDeleteSerialNumberByIds">
        update jsh_serial_number
        set update_Time=#{updateTime},updater=#{updater},delete_Flag='1'
        where 1=1
        and id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>

传递 List 集合

接口

int batchDelete(@Param("voipList") List<String> voipList);

mapper 定义

    <delete id="batchDelete">
        DELETE FROM app_user_data_authentication
        where voip in
        <foreach item="item" index="index" collection="voipList" open="(" separator="," close=")">
            #{item}
        </foreach>
        and type = 1
    </delete>

接口

    int deleteByEsimInfoList(@Param("esimInfoList") List<EsimInfo> esimInfoList);

mapper 定义

    <delete id="deleteByEsimInfoList">
      delete from esim_info where 1=1
      <if test="esimInfoList!=null">
          and (eid in
          <foreach item="item" index="index" collection="esimInfoList" open="(" separator="," close=")">
              #{item.eid}
          </foreach>
          or imsi in
          <foreach item="item" index="index" collection="esimInfoList" open="(" separator="," close=")">
              #{item.imsi}
          </foreach>
          )
      </if>
    </delete>

传递字符串 ($ 获取参数,不安全,不建议使用)

接口

    List<DepotHeadVo4InDetail> findByAll(
            @Param("beginTime") String beginTime,
            @Param("endTime") String endTime,
            @Param("dids") String dids);

mapper 定义

    <select id="findByAll" parameterType="com.jsh.erp.datasource.entities.DepotItemExample" resultMap="ResultWithInfoExMap">
        select dh.Number,di.UnitPrice,di.OperNumber,di.AllPrice
        from jsh_depothead dh
        inner join jsh_depotitem di on di.HeaderId=dh.id and ifnull(di.delete_Flag,'0') !='1'
        where dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="dids != null">
            and di.DepotId in (${dids})
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        ORDER BY OperTime DESC,Number desc

    </select>

自己拼接 sql

接口

@DeleteProvider(type = RulesProvider.class, method = "batchDelete")
int deleteStandardRule(@Param("rolesId") String ids);

拼接 sql

class RulesProvider {
    /* 批量删除 */
    public String batchDelete(Map map) {
        String id= (String)map.get("rolesId");
        StringBuilder sb = new StringBuilder();
        sb.append("delete from devops_rules where id in (");
        String [] ids=id.split(",");
        for (int i = 0; i < ids.length; i++) {
            sb.append("'").append(ids[i]).append("'");
            if (i < ids.length - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }
}