SELECT LAST_INSERT_ID() AS ID INSERT INTO rp_feno_app_rh_hard_device(CREATE_DATE ,MODIFY_DATE ,IMEI ,IS_UPGRADE ,FENO_APP_RH_HARD_ID ,IS_DEFAULT)VALUES(NOW(),NOW(),#{item.imei,jdbcType=VARCHAR},#{item.isUpgrade,jdbcType=BOOLEAN},#{item.fenoAppRHHard.id,jdbcType=BIGINT},#{item.isDefault,jdbcType=BOOLEAN})
在mybatis的xml文件中,使用foreach动态标签拼接SQL语句,每一条数据的更新语句对应一条update语句,多条语句最终使用";"号进行拼接。
update rp_feno_app_rh_hard_device id=${item.id},imei=${item.imei} where id = ${item.id}
查询效率较低,配置多
UPDATE rp_feno_app_rh_hard_device WHEN id=#{i.id,jdbcType=BIGINT} THEN NOW() WHEN id=#{i.id,jdbcType=BIGINT} THEN #{i.imei,jdbcType=VARCHAR} WHEREID = #{i.id,jdbcType=BIGINT}
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方文档:https://baomidou.com/pages/24112f/
// 插入(批量)
boolean saveBatch(Collection entityList);
// 插入(批量)
boolean saveBatch(Collection entityList, int batchSize);
批量插入解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList, int batchSize);
批量插入或更新解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
// 根据ID 批量更新
boolean updateBatchById(Collection entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection entityList, int batchSize);
批量更新解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |