spring和mybatis 整和(二)商品信息的修改

1.2  商品修改

需求

操作流程:

1、进入商品查询列表页面

2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)

         要修改的商品从数据库查询,根据商品id(主键)查询商品信息

3、在商品修改页面,修改商品信息,修改后,点击提交

 

步骤总结 : 1.用逆向工程生成ItemsMapper.java和ItemsMapper.xml

                     2.编写service接口 ,和serviceImpl实现类  用于被controller调用

                     3.编写 controller

                     4.编写jsp界面

1.2.1  Dao(mapper代理)

使用逆向工程自动生成的代码:

ItemsMapper.java

ItemsMapper.xml

 

1.2.2  Service

  //根据id查询信息

    public ItemsCustom findItemsById(Integer id)throws Exception;

    //修改商品

    /**

     *

     * @param id  修改商品的id id 是告诉其他开发者这个接口id是必传的

     * @paramitemsCustom  修改的商品的信息

     * @throws Exception

     */

    public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;

1.2.3  serviceImpl 

public ItemsCustomfindItemsById(Integer id) throwsException {

        // TODO Auto-generated method stub

       Items items=itemsMapper.selectByPrimaryKey(id);

       //展示的商品信息可能经过中间业务处理

       //原始的po类满足不了页面展示所以应该返回扩展类

       ItemsCustom itemsCustom=new ItemsCustom();

    //items的属性值拷贝到itemsCustom   org.springframework.beans 用错包不会拷贝,且不报错

       org.springframework.beans.BeanUtils.copyProperties(items,itemsCustom);

       //BeanUtils.copyProperties(items,itemsCustom);  

       System.out.println("1"+itemsCustom.getName());

       return itemsCustom;

    }

    public void updateItems(Integer id, ItemsCustom itemsCustom)

           throws Exception {

       // 添加业务校验,通常在service接口对关键类进行校验

       //校验id是否为空如果为空抛出异常  Integer便于检验用Integer不用int

       //更新商品信息使用updateByPrimaryKeyWithBLOBs能根据id 更新 items表中的所有字段包括大文本类型字段

       //updateByPrimaryKeyWithBLOBs要求必须传入id

       itemsCustom.setId(id);

       itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);

    }

1.2.4  controller

修改商品信息显示页面:  返回modelAndview

//商品信息修改界面的显示

       @RequestMapping("/editItems")

           public ModelAndView editItems()throws Exception{

           //调用service根据商品id查询商品信息   Model

            ItemsCustomitemsCustom=itemsService.findItemsById(2);

           System.out.println(itemsCustom.getName());

           //返回ModelAndView

           ModelAndView modelAndView=new ModelAndView();

           //将商品信息放到model

           modelAndView.addObject("itemsCustom",itemsCustom);

           //商品修改页面

           modelAndView.setViewName("items/editItems");

           return modelAndView;

       }

修改商品信息提交:   返回string

//商品修改提交

    @RequestMapping("/editItemSubmit")

    public String editItemSubmit(Items items)throws Exception{

      

       System.out.println(items);

 

       itemService.saveItem(items);

      

       return "success";

    }

1.2.5  页面

/WEB-INF/jsp/item/itemsList.jsp

/WEB-INF/jsp/item/editItem.jsp

摘自传智博客燕青老师视频


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值