【无标题】

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

相关推荐
晴空๓14 小时前
Spring Boot项目如何使用MyBatis实现分页查询
spring boot·后端·mybatis
一缕叶1 天前
mybatis(78/134)
mybatis
安清h2 天前
【基于SprintBoot+Mybatis+Mysql】电脑商城项目之用户注册
数据库·后端·mysql·spring·mybatis
yang_shengy2 天前
【JavaEE】Spring(5):Mybatis(上)
spring·java-ee·mybatis
字节全栈_PVK2 天前
Flask数据的增删改查(CRUD)_flask删除数据自动更新
数据库·flask·mybatis
花心蝴蝶.3 天前
MyBatis 入门
java·spring boot·后端·mybatis
or77iu_N3 天前
SpringBoot 中的测试jar包knife4j(实现效果非常简单)
java·开发语言·spring boot·后端·mybatis·jar
苏-言4 天前
SSM框架探秘:Spring 整合 Mybatis 框架
java·spring·mybatis
yang_shengy4 天前
【JavaEE】Spring(6):Mybatis(下)
spring·java-ee·mybatis
康惠桀4 天前
Mybatis-plus缓存
缓存·mybatis