mybatis 中 使用记录

1、mybatis 中  使用模糊查询,防止sql注入写法:

<select id="selectAllList" resultMap="BaseResultMap" parameterType="com.jidou.entity.OilCardBatch">
    select
    <include refid="Base_Column_List"/>
    from oil_card_batch
    where 1=1
    <if test="batchNumber!=null and batchNumber != '' ">
        and batch_number like concat('%',#{batchNumber,jdbcType=VARCHAR},'%')
    </if>
    <if test="batchName!=null and batchName != '' ">
        and batch_name like concat('%', #{batchName,jdbcType=VARCHAR},'%')
    </if>
    <if test="applianceChannel!=null and applianceChannel != '' ">
        and appliance_channel like concat('%',#{applianceChannel,jdbcType=VARCHAR},'%')
    </if>
    order by create_time desc
</select>

2、mybatis 中, 开始时间,结束时间的写法如下:

<select id="selectByBrandChannel"  resultMap="BaseResultMap">
    SELECT
    ct.*
    cd.*
    FROM
    coupon_detail cd
    LEFT JOIN coupon_template ct ON cd.template_id = ct.template_id
    <if test="brandChannel!=null and brandChannel!=''">
        where cd.brand_channel=#{brandChannel}
    </if>
    <if test="startDate!=null and startDate.trim() neq ''">
        and date_format(cd.create_time,'%Y-%m-%d %H:%i:%s') &gt;= str_to_date(#{startDate},'%Y-%m-%d %H:%i:%s')
    </if>
    <if test="endDate!=null and endDate.trim() neq ''">
        and date_format(cd.create_time,'%Y-%m-%d %H:%i:%s') &lt;= str_to_date(#{endDate},'%Y-%m-%d %H:%i:%s')
    </if>
</select>

3、参数自动根据“,”拆分拼接

<select id="couponDetailByTemplateIdList" resultType="com.jidou.coupon.entity.NewCouponDetail">
    select * from  coupon_detail
    where
        user_id = #{userId}
    and
        template_id in
    <if test="templateId != null and templateId != ''">
        <foreach item="item" index="index" collection="templateId.split(',')" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>

4、批量插入

<insert id="batchInsert" parameterType="java.util.List">
   insert into coupon_nissan_receive
   (
   template_id,
   user_id,
   create_time
   )
    values
    <foreach collection ="list" item="item" separator =",">
    (
    #{item.templateId},
    #{item.userId},
    #{item.createTime}
    )
    </foreach>
</insert>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值