Mybatis批量新增、批量查詢

    /**
     * 通過商品分類ids查詢查詢商品分類
     */
    List<Category> queryCategoryByIds(List<Long> ids);
    <!-- 通過Ids集合查詢 -->
    <select id="queryCategoryByIds" resultMap="BaseResultMap" parameterType="java.util.List">
        select * from tb_category d
        where
        d.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    /**
     * 批量新增用戶
     *
     * @param userList 用戶集合
     * @return 影響的行數
     */
    int insertUserList(List<User> userList);

    <insert id="insertUserList" parameterType="java.util.List">
        insert into user(
        username,
        password,
        phone,
        email,
        address
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{username,jdbcType=VARCHAR},
            #{password,jdbcType=VARCHAR},
            #{phone,jdbcType=BIGINT},
            #{email,jdbcType=VARCHAR},
            #{address,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
    /**
     * 批量修改用戶信息
     *
     * @param ids
     * @return 影響的行數
     */
    int updateUserList(List<Integer> ids);
<update id="updateUserList" parameterType="java.util.List">
        update user
        <set>
            <if test="username != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=BIGINT},
            </if>
            <if test="name != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="address != null">
                address = #{address,jdbcType=VARCHAR}
            </if>
        </set>
        where id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=",">
            #{ids}
        </foreach>
    </update>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章