mybatis批量插入

来源:互联网 发布:日照淘宝客服 编辑:程序博客网 时间:2024/06/06 17:32

BookAndBookTypeMapper.xml

<insert id="insertBookAndBookTypeBatch" useGeneratedKeys="true" parameterType="java.util.List">        INSERT INTO wor_book_booktype (wor_book_booktype.BookId,wor_book_booktype.BookTypeId)        VALUES        <foreach collection="list" item="BookAndBookTypeEntity" index="index" separator=",">            (#{BookAndBookTypeEntity.bookId},#{BookAndBookTypeEntity.bookTypeId})        </foreach>    </insert>
BookAndBookTypeServiceImpl

public String saveOrUpdateBookType(List<BookAndBookTypeEntity> bookAndBookTypeEntityList) {        String result = "";        try        {            if (this.bookAndBookTypeMapper.findBookTypeByBookId(bookAndBookTypeEntityList.get(0).getBookId()).size() == 0)            {                 this.bookAndBookTypeMapper.insertBookAndBookTypeBatch(bookAndBookTypeEntityList);            } else            {                this.bookAndBookTypeMapper.deleteBookAndBookType(bookAndBookTypeEntityList.get(0).getBookId());                this.bookAndBookTypeMapper.insertBookAndBookTypeBatch(bookAndBookTypeEntityList);            }        } catch (Exception e)        {            e.printStackTrace();            return null;        }        return result;    }



0 0
原创粉丝点击