mybatis对mysql进行插入或者更新(含批量)

一、插入或更新(insertOrUpdate) 单条记录

使用ON DUPLICATE KEY UPDATE,如果主键存在,即更新表

mybatis xml文件写法如下:

<insert id="insertOrUpdate" parameterType="xxxxx">
    INSERT into case_warning_rule (
    case_definition_dept_id,
    commitment_day,
    legal_period,
    approval_day,
    approval_warning,
    commitment_warning,
    commitment_unusual,
    legal_warning,
    legal_unusual,
    update_time
    ) VALUES (
    #{caseDefinitionDeptId},
    #{commitmentDay},
    #{legalPeriod},
    #{approvalDay},
    #{approvalWarning},
    #{commitmentWarning},
    #{commitmentUnusual},
    #{legalWarning},
    #{legalUnusual},
    #{updateTime}
    ) ON DUPLICATE KEY UPDATE
    commitment_day =VALUES (commitment_day),
    legal_period =VALUES (legal_period),
    approval_day =VALUES (approval_day),
    approval_warning =VALUES (approval_warning),
    commitment_warning =VALUES (commitment_warning),
    commitment_unusual =VALUES (commitment_unusual),
    legal_warning =VALUES (legal_warning),
    legal_unusual =VALUES (legal_unusual),
    update_time =VALUES (update_time)
</insert>

或者

<insert id="updateAndInsert" parameterType="com.codyy.ms.entity.IpAreaDO">
	INSERT INTO IP_AREA (IP,AREA_NAME,AREA_CODE,UPDATE_TIME)
	VALUE (
		#{ip,jdbcType=VARCHAR},
        #{areaName,jdbcType=VARCHAR},
        #{areaCode,jdbcType=VARCHAR},  
        #{updateTime,jdbcType=TIMESTAMP})

	ON DUPLICATE KEY UPDATE
    	AREA_NAME = #{areaName,jdbcType=VARCHAR},
    	AREA_CODE = #{areaCode,jdbcType=VARCHAR},
    	UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP}
</insert>

ip为主键或唯一键

或者

<insert id="insertOrUpdatePlan" 
        parameterType="com.happy.hhome.bean.plan.PlanFlowDO">

	insert into planflow
	(
    	plan_no,
    	flow_no,
    	plan_type,
    	plan_invest,
    	plan_profit,
    	plan_balance
	)
	values (
    	#{planNo},
    	#{flowNo},
    	#{planType},
    	#{planInvest},
    	#{planProfit},
    	#{planBalance}
	)
	on duplicate key update
    	plan_type     = values(plan_type),
    	plan_invest   = values(plan_invest),
    	plan_profit   = values(plan_profit),
    	plan_balance  = values(plan_balance)
</insert>

注意:on duplicate key update语句后面,等号前后两个值,均使用表字段,例:plan_type = values(plan_type)

一、插入或更新(insertOrUpdateBatch) 批量

mybatis 批量新增,如果列表里面有id那么则更新,mysql数据库。

VALUES里面取的是sql的字段,如果是关键字,注意要加上``

<insert id="batchAddOrUpdate" 
        parameterType="java.util.List" 
        useGeneratedKeys="true" 
        keyProperty="id">

	insert into t_schedule(
		id,
		doctorId,
		deptId,
		`type`,
		`date`,
		`desc`,
		startTime,
		endTime,
		`number`)
	values
	<foreach collection="list" item="item" index="index" separator=",">
    	(
    	#{item.id},
    	#{item.doctorId},
    	#{item.deptId},
    	#{item.type},
    	#{item.date},
    	#{item.desc},
    	#{item.startTime},
    	#{item.endTime},
    	#{item.number}
    	)
	</foreach>
	ON DUPLICATE KEY UPDATE
		doctorId=VALUES(doctorId),
		deptId=VALUES(deptId),
		`type`=VALUES(`type`),
		`date`=VALUES(`date`),
		`desc`=VALUES(`desc`),
		startTime=VALUES(startTime),
		endTime=VALUES(endTime),
		`number`=VALUES(`number`)
</insert>

或者

使用<foreach>生成values()语句即可,同样的,on duplicate key update语句后面,等号前后两个值,均使用表字段,例:plan_type = values(plan_type)

<insert id="insertOrUpdatePlanList"
        parameterType="com.happy.hhome.bean.plan.PlanFlowDO">

	insert into planflow
	(
    	plan_no,
    	flow_no,
    	plan_type,
    	plan_invest,
    	plan_profit,
    	plan_balance
	)
	values
	<foreach collection="list" item="item" index= "index" separator="," >
	(
    	#{item.planNo},
    	#{item.flowNo},
    	#{item.planType},
    	#{item.planInvest},
    	#{item.planProfit},
    	#{item.planBalance}
	)
	</foreach>
	on duplicate key update
    	plan_type     = values(plan_type),
    	plan_invest   = values(plan_invest),
    	plan_profit   = values(plan_profit),
    	plan_balance  = values(plan_balance)
</insert>

参考:

https://blog.csdn.net/qq_34820670/article/details/80941979?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

https://blog.csdn.net/qq_24520119/article/details/78132491?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

https://blog.csdn.net/u013788943/article/details/105405517?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

https://blog.csdn.net/weixin_43822795/article/details/96587755?utm_medium=distribute.pc_relevant.none-task-blog-title-7&spm=1001.2101.3001.4242

对于MybatisMySQL批量插入批量更新,可以使用以下方法: 1. 批量插入 使用MyBatis的foreach标签,可以很方便地实现批量插入操作。示例如下: ```xml <insert id="batchInsert" parameterType="java.util.List"> insert into my_table (col1, col2, col3) values <foreach collection="list" item="item" separator=","> (#{item.col1}, #{item.col2}, #{item.col3}) </foreach> </insert> ``` 其中,parameterType指定参数类型为List,collection属性指定要插入的List对象,item指定List中的元素,separator指定分隔符。 在Java代码中,调用该方法时,将List对象作为参数传入即可。 2. 批量更新 对于批量更新,可以使用MySQL的replace into语句来实现。示例如下: ```xml <update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" separator=";"> replace into my_table (id, col1, col2, col3) values (#{item.id}, #{item.col1}, #{item.col2}, #{item.col3}) </foreach> </update> ``` 其中,replace into语句会先尝试插入记录,如果记录已存在,则更新记录。参数类型和调用方式与批量插入相同。 3. 存在即更新 如果只想更新已存在的记录,可以使用MySQL的on duplicate key update语句。示例如下: ```xml <update id="updateIfExists" parameterType="com.example.MyEntity"> insert into my_table (id, col1, col2, col3) values (#{id}, #{col1}, #{col2}, #{col3}) on duplicate key update col1 = values(col1), col2 = values(col2), col3 = values(col3) </update> ``` 其中,id字段为主键。如果该记录已存在,则更新col1、col2、col3字段;否则插入新记录。参数类型为MyEntity,调用方式为传入一个MyEntity对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值