超详细超实用!!!零基础java开发之云风笔记更新数据接口开发(十)

云风网
云风笔记
云风知识库

一、service/NoteApi新增updateNode接口定义

public interface NoteApi {
    ...
    int updateNote(NoteManage noteManage);
}

二、service/impl/NoteServiceImpl接口实现逻辑

public class NoteServiceImpl implements NoteApi {
    @Autowired
    NoteMapper noteMapper;
    ...

    public int updateNote(NoteManage noteManage){
        int count = 0;
        try {
            count = noteMapper.updateNote(noteManage);
        }catch (Exception err){
            System.out.println(err);
        }
        return count;
    }

}

三、mapper/NoteMapper新增接口

...
import org.apache.ibatis.annotations.Param;

public interface NoteMapper {
    ...
    int updateNote(@Param("note") NoteManage noteManage);
}
四、新增sql语句
<update id="updateNote"  parameterType="com.example.study.note.NoteManage">
    update note
    <trim prefix="SET" suffixOverrides=",">
        <if test="null != note.name and '' != note.name">
            name=#{note.name},
        </if>
        <if test="null != note.type and '' != note.type">
            type=#{note.type},
        </if>
        <if test="null != note.content and '' != note.content">
            name=#{note.content},
        </if>
        <if test="null != note.desc and '' != note.desc">
            type=#{note.desc},
        </if>
    </trim>
    where id=#{note.id}
</update>
五、NoteManage添加获取id/设置id
private int id;
public NoteManage(String name, String type, String content, String desc,String searchName,int id) {
    this.name = name;
    this.type = type;
    this.content = content;
    this.desc = desc;
    this.searchName = searchName;
    this.id = id;
}
...
public void setNoteId(int id){
    this.id = id;
}
public int getNoteId(){
    return id;
}
六、控制类NoteController新增更新逻辑
 @RequestMapping(value = "/updateNote",method = RequestMethod.POST)
 public Response updateNote(@RequestBody NoteManage noteManage){
     int id = noteManage.getNoteId();
     int count = service.updateNote(noteManage);
     if(id!=0){
         if(count>0){
             Response response =  new Response(true,"更新成功",200);
             return  response;
         }else {
             Response response = new Response(false,"更新失败",400);
             return  response;
         }
     }else {
         Response response = new Response(false,"请传入id",400);
         return  response;
     }
 }
六、接口请求测试

数据库原数据

在这里插入图片描述

更新笔记名称

在这里插入图片描述
在这里插入图片描述

更新成功!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值