前言
相信用过MySQL的都知道on duplicate key update这个关键字吧,这是MySQL用来防止重复插入的,前提是你的表中加了唯一索引,表的结构和插入语句如下,唯一索引不懂的请自行百度.
语句的意思就是当发生了冲突时不报错而自行更新修改时间
CREATE TABLE `test` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(20) DEFAULT NULL COMMENT '用户名称',
`create_time` bigint NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `user_name` (`user_name`) USING BTREE
) ENGINE=InnoDB COMMENT='test过滤表';
dao 层
int saveOnDuplicate(UserRoleFilter userRoleFilter);
插入语句
<insert id="saveOnDuplicate" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
insert into ac_user_role_filter (user_name, create_time)
values (#{userName}, #{createTime})
on duplicate key

最低0.47元/天 解锁文章
4008

被折叠的 条评论
为什么被折叠?



