mysql 实现insert update 功能

insert update 实现

复制代码
<insert id="insertOrUpdateSelective" keyColumn="id" keyProperty="id" parameterType="com.xxx.xxx.UserDO" useGeneratedKeys="true">
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="accountId != null">
account_id,
</if>
<if test="gmtCreate != null">
gmt_create,
</if>
<if test="gmtModified != null">
gmt_modified,
</if>
<if test="isDelete != null">
is_delete,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id},
</if>
<if test="name != null">
#{name},
</if>
<if test="accountId != null">
#{accountId},
</if>
<if test="gmtCreate != null">
#{gmtCreate},
</if>
<if test="gmtModified != null">
#{gmtModified},
</if>
<if test="isDelete != null">
#{isDelete},
</if>
</trim>
ON duplicate key update
<trim suffixOverrides=",">
<if test="id != null">
id =#{id},
</if>
<if test="name != null">
name =#{name},
</if>
<if test="accountId != null">
account_id = #{accountId},
</if>
<if test="gmtCreate != null">
gmt_create = #{gmtCreate},
</if>
<if test="gmtModified != null">
gmt_modified = #{gmtModified},
</if>
<if test="isDelete != null">
is_delete = #{isDelete},
</if>
</trim>
</insert>
复制代码

 

insert 注解

@Insert ({”insert into sys_role (role_name, enabled, create_by, create_ time )”, 
” values(#{roleName}, #{enabled}, #{createBy },”, 
”#{ createTime, jdbcType=TIMESTAMP })”}) 
@Options(useGeneratedKeys =true, keyProperty =”id”) 
int insert2(SysRole sysRole);

 

返回非自增主键
复制代码
@Insert ({”insert into sys_role {role_name, enabled, create_by, create time ) ”, 
” values {#{roleName} , #{enabled}, #{createBy },”, 
”#{ createTime , jdbcType= TIMESTAMP }) ” }} 
@SelectKey{statement =”SELECT LAST INSERT ID {)”, 
keyProperty =”id”, 
resultType = Long.class, 
before = false) 
int insert3{SysRole sysRole); 
使用@ SelectKey 注解,以下代码是前面 XML 中配置的 selectKeyo
<selectKey keyColumn=”id” resultType=”long” keyProperty=”id” order=”AFTER”>
SELECT LAST INSERT ID {) 
</selectKey>
复制代码

 

posted @   流星小子  阅读(456)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示