【无标题】

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

相关推荐
哈喽姥爷20 小时前
Spring Boot--yml配置信息书写和获取
java·数据库·spring boot·mybatis
奔跑你个Run2 天前
mybatis plus 使用wrapper输出SQL
mybatis
躲在云朵里`3 天前
Spring Scheduler定时任务实战:从零掌握任务调度
java·数据库·mybatis
Java小白程序员4 天前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
山楂树下懒猴子4 天前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
Mr_hwt_1234 天前
基于mybatis-plus动态数据源实现mysql集群读写分离和从库负载均衡教程(详细案例)
数据库·spring boot·mysql·mybatis·mysql集群
Z_z在努力5 天前
【杂类】Spring 自动装配原理
java·spring·mybatis
little_xianzhong5 天前
关于对逾期提醒的定时任务~改进完善
java·数据库·spring boot·spring·mybatis
MadPrinter5 天前
SpringBoot学习日记 Day11:博客系统核心功能深度开发
java·spring boot·后端·学习·spring·mybatis
奔跑吧邓邓子5 天前
【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
java·spring boot·实战·mybatis·数据库交互