1. 官方提供的几个扩展方法:
2. 使用方法:
2.1. 创建自定义SQL注入器
package com.jerry.config;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author jerry
* @ClassName SqlInjector
* @Description 自定义SQL注入器 MybatisPlus批量插入
* @Date 2023/5/11 10:47
* @Version 1.0
*/
@Component
public class SqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, com.baomidou.mybatisplus.core.metadata.TableInfo tableInfo) {
// 防止父类的方法不可使用 继承了DefaultSqlInjector(默认注入器),调用DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// 添加批量插入的方法 官方提供方法给注入但是不直接提供 官方的解释是:不同的数据库支持度不一样!!
methodList.add(new InsertBatchSomeColumn());
// 更新or插入,根据唯一约束判断是执行更新还是删除,相当于提供insert on duplicate key update支持
methodList.add(new Upsert());
return methodList;
}
}
2. 2 在Mapper中定义与插件方法名一致的方法即可,如:insertBatchSomeColumn
即可实现批量写入
InsertBathSonmeColumn
批量插入,拼接SQL实现的批量插入操作
SQL格式
==> Preparing: INSERT INTO vote_result (id,wx_user_id,user_vote_id,vote_result_content) VALUES (?,?,?,?) , (?,?,?,?) , (?,?,?,?) , (?,?,?,?)
Upsert
根据表的唯一键是否已存在来确定是
insert
还是update
,只允许通过表的唯一键来做判断
<sql id="Base_Column_List">
id
,platform_id,comp_name,
comp_abbr,start_date,expiry_date,
status,del_flag,br_hkid,
contact_name,contact_tel,contact_fax,
contact_email,address,comp_pre_fix_id,
driver_pre_fix_id,client_count,helper_count,
parent_count,online_start_order,show_banner,
remark,create_by,create_time,
update_by,update_time
</sql>
<insert id="upsert">
INSERT INTO `company` (<include refid="Base_Column_List"/>)
VALUES
<foreach collection="companyList" item="item" separator=",">
(
#{item.id},
#{item.platformId},
#{item.compName},
#{item.compAbbr},
#{item.startDate},
#{item.expiryDate},
#{item.status},
#{item.delFlag},
#{item.brHkid},
#{item.contactName},
#{item.contactTel},
#{item.contactFax},
#{item.contactEmail},
#{item.address},
#{item.compPreFixId},
#{item.driverPreFixId},
#{item.clientCount},
#{item.helperCount},
#{item.parentCount},
#{item.onlineStartOrder},
#{item.showBanner},
#{item.remark},
#{item.createBy},
#{item.createTime},
#{item.updateBy},
#{item.updateTime}
)
</foreach>
ON DUPLICATE KEY UPDATE
`id` = VALUES(`id`),
`platform_id` = VALUES(`platform_id`),
`comp_name` = VALUES(`comp_name`),
`comp_abbr` = VALUES(`comp_abbr`),
`start_date` = VALUES(`start_date`),
`expiry_date` = VALUES(`expiry_date`),
`status` = VALUES(`status`),
`del_flag` = VALUES(`del_flag`),
`br_hkid` = VALUES(`br_hkid`),
`contact_name` = VALUES(`contact_name`),
`contact_tel` = VALUES(`contact_tel`),
`contact_fax` = VALUES(`contact_fax`),
`contact_email` = VALUES(`contact_email`),
`address` = VALUES(`address`),
`comp_pre_fix_id` = VALUES(`comp_pre_fix_id`),
`driver_pre_fix_id` = VALUES(`driver_pre_fix_id`),
`client_count` = VALUES(`client_count`),
`helper_count` = VALUES(`helper_count`),
`parent_count` = VALUES(`parent_count`),
`online_start_order` = VALUES(`online_start_order`),
`show_banner` = VALUES(`show_banner`),
`remark` = VALUES(`remark`),
`update_by` = VALUES(`update_by`),
`update_time` = VALUES(`update_time`)
</insert>
AlwaysUpdateSomeColumnById
根据id更新固定的那几个字段不包含逻辑删除
//根据id更新固定的那几个字段不包含逻辑删除 不更新name
methodList.add(new AlwaysUpdateSomeColumnById(t->!t.getColumn().equals("name")));
LogicDeleteBatchByIds
根据id批量逻辑删除 需要标识逻辑删除的字段 逻辑删除默认是0/1
/**
* 删除状态(0-正常,1-已删除)
*/
@TableLogic
private String delFlag;