Java之MyBatis 批量插入数据,两种方式(注解,XML)

x33g5p2x  于2022-02-18 转载在 Java  
字(1.0k)|赞(0)|评价(0)|浏览(481)

1、MyBatis之 XML 批量插入数据

int insertStudentList(List<SysStudent> sysStudent);
<insert id="insertStudentList">
        insert into sys_student(sut_id, stu_name, stu_year) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.sutId},#{item.stuName},#{item.stuYear})
        </foreach>
</insert>

2、MyBatis之 注解 插入数据

public interface SysStudentMapper extends Mapper<SysStudent> {

    public String tableName = "sys_student";
    public String columnNoId = "sut_id,stu_name,stu_year,;

    @Insert("<script> " +
            "insert into " + tableName +
            "(" + columnNoId + ") " +
            "values " +
            "<foreach collection=\"items\" index=\"index\" item=\"item\" separator=\",\"> "
            +
            "(#{item.sutId},#{item.stuName},#{item.stuYear})"
            +
            "</foreach> " +
            "</script>")
    int batchSave(@Param("items") List<SysStudent> items);

相关文章

最新文章

更多