重生之我在地球Online当程序员32

MyBatis使用
接口有多个参数

方式1:使用 #{param1} 内置的参数param1对应dao接口的第一个参数

接口:
    List<Owner> findByPage(Integer start, Integer pageSize);
映射:
    SELECT id,user_name userName,tel,sex,identity,remarks FROM `owner`
    limit #{param1},#{param2}

方式2:@Param注解指定映射文件中占位符的值

接口:
   List<Owner> findByPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize);
映射:
    SELECT id,user_name userName,tel,sex,identity,remarks FROM `owner`
    limit #{start},#{pageSize}
动态SQL:set标签

【MyBatis官网】

*set* 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号

接口:

void update(Owner owner);

映射:

<update id="update">
    update owner
    <set>
        <if test="userName != null and userName.trim() != ''">
            user_name=#{userName},
        </if>
        <if test="tel != null and tel.trim() != ''">
            tel=#{tel},
        </if>
        <if test="sex != null">
            sex=#{sex},
        </if>
        <if test="identity != null">
            identity=#{identity},
        </if>
        <if test="remarks != null">
            remarks=#{remarks}
        </if>
    </set>
    where id=#{id}
</update>
动态SQL:foreach标签

接口:

void delBatch(@Param("ids") Integer[] ids);

映射:

<delete id="delBatch">
    delete from owner where id in
    <foreach collection="ids" open="(" item="temp" separator="," close=")">
        #{temp}
    </foreach>
</delete>

collection: 遍历的集合

open: 拼接的开始

item: 定义一个变量存储迭代的每一个元素

separator: 分隔符

close: 拼接的结束

练习:条件分页查询

需求:根据用户名、电话、性别查询,用户

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值