mybaits 中List的相关应用场景 如批量保存和条件为list查询

mybatis中根据list查询

应用场景为:当需要调用list参数进行查询时,查询所需条件是list,一般来说,list中保存的应该为int或者long类型的数据 而且都是习惯使用id保存在list中;

举例: 我们把书本id保存在list中,根据书本id 查询所有书本

<select id="listBook" resultMap="BaseResultMap" parameterType="java.util.List">
SELECT
book_name,author 
FROM book
WHERE id IN
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
<foreach>
 </select>

mybaits中批量插入

应用场景:当传入参数为list时,需要用list里面的数据,一次性保存到数据库中

举例:书本list为 bookList 。当list中有多个书本对象 就是是指bookList(book(name1,author1),book(name2,author2),book(name3,author3))

    <insert id="batchSaveBook" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="java.util.List">
        INSERT INTO book
        (book_name,author)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.book_name},#{item.author})
        </foreach>
    </insert>

猜你喜欢

转载自blog.csdn.net/qq_41831842/article/details/114638484