【无标题】

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

相关推荐
晚安code19 小时前
MyBatis-Plus 分页插件与字段自动填充:两行配置告别 CRUD 重复劳动
mybatis
SQL-First布道者2 天前
持久层框架的评价标准:只有一个
java·spring boot·spring·tomcat·mybatis·spring jdbc
步行cgn2 天前
MyBatis 高级映射与延迟加载完全指南
java·tomcat·mybatis
mashirro2 天前
记一次基于ruoyi-vue新增小程序服务模块,小程序登陆相关
vue.js·小程序·mybatis
边境悍匪2 天前
蜗牛学苑 Java 智能体学习 Day24|SpringBoot 整合 MyBatis、分层解耦思维导图复盘
java·数据库·spring boot·学习·mybatis
抓哇小菜鸡3 天前
Spring Boot + 本地大模型(Ollama/DeepSeek) + MyBatis-Plus 企业级智能体数据分析系统从零到一源码全解析
spring boot·后端·mybatis
步行cgn3 天前
MyBatis 批量插入时 Parameter ‘carNum‘ not found 错误详解
mybatis
何以解忧,唯有..3 天前
深入理解与应对:Redis 缓存雪崩、击穿、穿透三大经典问题
redis·缓存·mybatis
andongni2034 天前
后端参数校验
spring boot·后端·mybatis