springboot的增删改查

一、how2j网站上Category表练习
教程:SpringBoot系列教材 (十七)- CRUD+分页 - Springboot使用Mybatis实现完整的 增删改查 CRUD和分页 (how2j.cn)。之前的练习只实现了查找功能,本次练习在上次练习的基础上增加了增删查找功能以及分页功能。

二、增加CURD方法主要是在CategoryMapper和CategoryController中进行的

CategoryMapper中主要代码:

@Select("select * from category_ ")

    List<Category> findAll();#查询表中所有数据    

  @Insert(" insert into category_ ( name ) values (#{name}) ")

    public int save(Category category); #增加操作

    @Delete(" delete from category_ where id= #{id} ")

    public void delete(int id);#删除操作 

    @Select("select * from category_ where id= #{id} ")

    public Category get(int id);#查询操作

    @Update("update category_ set name=#{name} where id=#{id} ")

    public int update(Category category); #修改操作

CategoryController中主要是与其相对应的操作

@RequestMapping("/addCategory")       #通过访问addcategory地址来实现增加操作

public String listCategory(Category c) throws Exception {

categoryMapper.save(c);   #调用categorymapper中的save()方法

return "redirect:listCategory";

}

@RequestMapping("/deleteCategory")

public String deleteCategory(Category c) throws Exception {

categoryMapper.delete(c.getId());

return "redirect:listCategory";

}

@RequestMapping("/updateCategory")

public String updateCategory(Category c) throws Exception {

categoryMapper.update(c);

return "redirect:listCategory";

}

@RequestMapping("/editCategory")

public String listCategory(int id,Model m) throws Exception {

Category c= categoryMapper.get(id);

m.addAttribute("c", c);

return "editCategory";

}

最终运行结果

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值