关于一个mybaits一个sql标签实现 既可以插入新的动态,又可以更新原来的动态。
我新建了一个表,让id为主键,id主键为自增。当前端插入新的一条动态时可以直接插入,当修改已有的动态时,根据获取的id判断原来的表中已经存了,则应该执行ON DUPLICATE KEY UPDATE.
ON DUPLICATE KEY UPDATE
<update id="saveOrUpdate" parameterType="com.second.hand.trading.server.model.NoticeModel">
insert into notice (id,name,content,user,time,img)
values (#{id,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR},#{user,jdbcType=VARCHAR},#{time,jdbcType=TIMESTAMP},#{img,jdbcType=VARCHAR})
ON DUPLICATE KEY UPDATE
name=VALUES(name),
content=VALUES(content),
user=VALUES(user),
img=VALUES(img),
</update>
现在的问题时,新增的时候,id主键没有自增,id主键值还得赋值,但是前端应该如何实现呢?
前端代码
//表格输入的信息
handleAdd() {
this.dialogFormVisible = true
this.form = {img: ''}
this.$nextTick(() => {
if(!editor) {
editor = new E("#richText")
editor.config.uploadImgServer = 'http://localhost:8080/file/'
editor.config.uploadFileName = 'file'
editor.create()
}
editor.txt.html('') // 清除内容
if(this.$refs.img) {
this.$refs.img.clearFiles();
}
if(this.$refs.file) {
this.$refs.file.clearFiles();
}
})
},
```java
save() {
const content = editor.txt.html()
console.log(content)
// 注意:这个地方需要手动赋值
this.form.content = content
//保存或修改内容
this.$api.savenotice(this.form).then(res => {
if (res.status_code === 1) {
this.$message.success("保存成功")
this.dialogFormVisible = false
this.load()
} else {
this.$message.error("保存失败")
}
})
},
```