【无标题】

mybatis 批量插入数据,xml文件到底如何写呀 ?这里备注一下

1. 批量插入数据:batchInsert

mapper 代码:

java 复制代码
package com.xxxx.mapper;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface XXXXRepoMapper extends Mapper<XXXXRepo> {

    void batchInsert(@Param("list") List<XXXXRepo> list);

}

mapper xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xx.yy.mapper.XXXRepoMapper">
    <resultMap id="BaseResultMap" type="com.xx.yy.XXXXRepo">
        <id column="ID" jdbcType="INTEGER" property="id" />
        <result column="code" jdbcType="VARCHAR" property="code" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="parent_code" jdbcType="VARCHAR" property="parentCode" />
        <result column="type" jdbcType="TINYINT" property="type" />
        <result column="active_flag" jdbcType="TINYINT" property="activeFlag" />
        <result column="creator" jdbcType="BIGINT" property="creator" />
        <result column="modifier" jdbcType="BIGINT" property="modifier" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime" />
    </resultMap>


    <sql id="table_name">
        t_xxx_repo
    </sql>

    <sql id="Base_Column_List">
        id,code,name,parent_code,type,active_flag,creator,modifier,create_time,modify_time
    </sql>

    <insert id="batchInsert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into <include refid="table_name"/>
        (<include refid="Base_Column_List"/>) values
        <foreach collection="list" index="index" item="item" separator=",">
            (null,#{item.code,jdbcType=VARCHAR},#{item.name,jdbcType=VARCHAR},#{item.parentCode,jdbcType=VARCHAR},#{item.type,jdbcType=TINYINT},1,
            #{item.creator,jdbcType=BIGINT},#{item.modifier,jdbcType=BIGINT},now(),now())
        </foreach>
    </insert>



</mapper>

2. 批量更新数据:batchUpdate

相关推荐
java—大象10 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s10 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
编程乐学(Arfan开发工程师)10 天前
73、单元测试-断言机制
服务器·数据库·servlet·单元测试·sqlite·log4j·mybatis
小时候的阳光10 天前
MyBatis 的一级缓存导致的数据一致性问题分析
缓存·mybatis·事务·隔离级别
烙印60110 天前
MyBatis原理剖析(三)--加载配置文件
服务器·tomcat·mybatis
demon755200310 天前
Mybatis-Plus支持多种数据库
mybatis·多数据库
能工智人小辰11 天前
二刷苍穹外卖 day03
java·开发语言·mybatis
2401_8260976211 天前
JavaEE-Mybatis初阶
java·java-ee·mybatis
剽悍一小兔11 天前
一个小BUG引发的对Mybatis-Plus的模糊查询的思考
bug·mybatis