第16周---SpringBoot练习2(增删改查练习)

一、how2j网站上Category表练习

教程:SpringBoot系列教材 (十七)- CRUD+分页 - Springboot使用Mybatis实现完整的 增删改查 CRUD和分页 (how2j.cn)

之前的练习只实现了查找功能,本次练习在上次练习的基础上增加了增删查找功能以及分页功能。

1、增加CRUD方法主要在CategoryMapper与CategoryController中进行添加。

以下是实现各功能对应Mapper类、Controller类中的代码。Mapper类中采用注解的方式

(1).查找所有数据:

@Select("select * from category_ ")
    List<Category> findAll();     其中findAll()方法与控制类中的映射对应。

@RequestMapping("/listCategory")    (控制类中对应的映射)
    public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        List<Category> cs=categoryMapper.findAll();
        PageInfo<Category> page = new PageInfo<>(cs);
        m.addAttribute("page", page);        
        return "listCategory";
    }

(2).插入数据:

@Insert(" insert into category_ ( name ) values (#{name}) ")
    public int save(Category category); 

@RequestMapping("/addCategory")
    public String listCategory(Category c) throws Exception {
        categoryMapper.save(c);
        return "redirect:listCategory";
    }

(3).删除数据:

@Delete(" delete from category_ where id= #{id} ")
    public void delete(int id);

@RequestMapping("/deleteCategory")
    public String deleteCategory(Category c) throws Exception {
        categoryMapper.delete(c.getId());
        return "redirect:listCategory";
    }

(4).更新数据:

要先通过id获取要更新的数据行,定位到编辑页面,再将更新后的数据显示在listCategory.jsp页面。

@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); 

@RequestMapping("/editCategory")
    public String listCategory(int id,Model m) throws Exception {
        Category c= categoryMapper.get(id);
        m.addAttribute("c", c);
        return "editCategory";
    }

@RequestMapping("/updateCategory")
    public String updateCategory(Category c) throws Exception {
        categoryMapper.update(c);
        return "redirect:listCategory";
    }

 

 2、分页功能

在pom.xml中增加对PageHelper的支持,记得更改完pom.xml后要更新maven:右击项目→Maven→Update Project。

新增PageHelperConfig.java

 

 3、重启测试再访问地址

要重启eclipse,再运行Application.java,最后再访问测试地址。(因为在pom中增加了新jar的依赖,所以需要手动重启)

直接运行Application类的主方法这一启动方式是SpringBoot的特有方式。

 二、对product_表、users_表进行增删改查操作

利用spingboot来编写,需要编写以下内容:

1.Application.java:与之前练习代码相同,不需要更改。

2.实体类(Product.java,Users.java)

3.Mapper类(ProductMapper.java,UsersMapper.java):编写增删改查的对应sql语句。由于此次练习的表的字段较Category多,记得按照sql语句要求。比如:

insert into product_ ( name,price,cid ) values (#{name},#{price},#{cid})

update product_ set name=#{name},price=#{price},cid=#{cid} where id=#{id}    各字段中间用逗号隔开

 4.控制类(ProductController.java,UsersController.java):增删改查的映射

 5.application.properties:与之前练习一样

6.要在main/webapp/WEB-INF/jsp 下新建listProduct.jsp与editProduct.jsp。

一开始直接在之前的list.Category.jsp进行更改,后面出现了如下错误,当我新建jsp页面将其复制过去,就可以运行出来了。

 

 7.pom.xml:与之前练习相同

最后访问127.0.0.1:8080/listProduct,成功进行增加删除,只不过在编辑时,我发现我更改price与cid提交后,其提交值会出现在name里面,而其显示0.0.第一反应是觉得自己更新的sql语句写错了,导致提交到的数据到了name中,但是看了sql语句似乎没有写错,所以今天还没有找到,晚点问问同学看是什么原因。

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值