【无标题】

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

相关推荐
☀Mark_LY1 小时前
MyBatis-Flex入门以及多数据源配置
java·mybatis
人道领域3 小时前
javaWeb从入门到进阶(SpringBoot基础案例2)
java·开发语言·mybatis
九皇叔叔3 小时前
【06】SpringBoot3 MybatisPlus 修改(Mapper)
java·spring boot·mybatis·mybatisplus
mc_故事与你4 小时前
前后端分离项目(springboot+vue+mybatis)-教学文档(SpringBoot3+Vue2)-4 (正在编写)
vue.js·spring boot·mybatis
秃头续命码农人4 小时前
谈谈对Spring、Spring MVC、SpringBoot、SpringCloud,Mybatis框架的理解
java·spring boot·spring·mvc·maven·mybatis
bekote14 小时前
mybatis-plus-generator模版
mybatis
cyforkk19 小时前
MyBatis Plus 字段自动填充:生产级实现方案与原理分析
mybatis
九皇叔叔19 小时前
【03】SpringBoot3 MybatisPlus BaseMapper 源码分析
java·开发语言·mybatis·mybatis plus
马猴烧酒.1 天前
【Mybatis出现bug】应为 <statement> 或 DELIMITER,得到 ‘id‘
java·bug·mybatis
一只程序熊1 天前
SpringBoot class java.lang.String cannot be cast to class java.lang.Long
spring·mybatis