- on duplicate key update 后跟上唯一约束条件,也就是数据库中的唯一索引
- foreach中()后面不要有空格,如果报错
org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
除了检查,是否缺少或者多余,还要检查是否有多余空格
insert into xxxTable (
merchant_id, app_id, department_id,
`name`, mobile, token,
created_at, updated_at)
values
(
#{item.merchantId,jdbcType=INTEGER},#{item.appId,jdbcType=INTEGER},
#{item.departmentId},
#{item.name},#{item.mobile},#{item.token},
NOW(),NOW()
)
on duplicate key update department_id = values(department_id),updated_at = values(updated_at)
select count(*) as count from `${tableName}` where userid = #{user.userid}
UPDATE `${tableName}`
`app_id` = #{user.appId},
`yunid` = #{user.yunid},
`qr_scene` = #{user.qr_scene},
`openid` = #{user.openid},
`qr_scene_str` = #{user.qr_scene_str},
`nickname` = #{user.nickname},
`language` = #{user.language},
`city` = #{user.city},
`country` = #{user.country},
`remark` = #{user.remark},
`headimgurl` = #{user.headimgurl},
`province` = #{user.province},
`tagid_list` = #{user.tagIdList},
`subscribe_scene` = #{user.subscribe_scene},
`unionid` = #{user.unionid},
`subscribe` = #{user.subscribe},
`groupid` = #{user.groupid},
`subscribe_time` = #{user.subTime},
`sex` = #{user.sexStr},
`updated_at` = #{user.updatedAt},
WHERE userid = #{user.userid}
INSERT IGNORE INTO `${tableName}`
(
)
values
(
#{user.appId},#{user.yunid},#{user.userid},#{user.point},#{user.qr_scene},
#{user.openid},#{user.qr_scene_str},#{user.nickname},#{user.language},#{user.city},
#{user.country},#{user.remark},#{user.headimgurl},#{user.province},#{user.tagIdList},
#{user.subscribe_scene},#{user.unionid},#{user.subscribe},#{user.groupid},#{user.subTime},
#{user.sexStr},#{user.createdAt},#{user.updatedAt}
)
INSERT INTO
user(userid,sex,age)
VALUES('oCCtTxOz28457LUISKyOq4r94DYE','男',18)
ON DUPLICATE KEY UPDATE sex=VALUES(sex),age=VALUES(age)