SSM整合-简单的CRUD----U

SSM整合增删改查之修改

一、先根据id查询信息,然后在controller层把查询的信息通过model传输到前端跳转的页面

首页(index.html)

<div class="col-md-2">
 <a th:href="@{'/edit/'+${category.id}}">
 <button type="button" class="btn btn-w-m btn-info">编辑</button></a>
</div>

controller层

 @RequestMapping("/edit/{id}")
    public String showEdit(@PathVariable("id") Integer id,Model model){
        Category category = categoryService.getCategoryById(id);
        model.addAttribute("category", category);
        return "Edit";
    }

service层

//service接口中
Category getCategoryById(Integer id);

//service的实现类
	@Override
    public Category getCategoryById(Integer id) {
        return categoryMapper.getCategoryById(id);
    }

Dao层

/**
     * 通过id获取需要修改的分类
     * @param id 修改分类的编号
     * @return 返回分类
     */
    Category getCategoryById(Integer id);
<!--Category getCategoryById(Integer id);-->
    <select id="getCategoryById" resultType="Category">
        select * from category where id = #{id};
    </select>

二、通过表单的提交来修改信息

进行修改的页面(edit.html)

<form th:action="@{/edit}" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="hidden" name="id" th:value="${category.id}">
     名称:<input type="text" name="title" th:value="${category.title}">
     <input type="submit" value="修改"/>
</form>

controller层

@PutMapping("/edit")
    public String editCategoryName(Category category){
        categoryService.updateCategory(category);
        System.out.println(category);
        return "redirect:/category";
    }

service层

//service接口
void updateCategory(Category category);

//service实现类
@Override
    public void updateCategory( Category category) {
        categoryMapper.updateCategory(category);
    }

Dao层

/**
     * 修改
     * @param category 修改的分类
     */
    void updateCategory(Category category);
<!--void updateCategory(Category category);-->
    <update id="updateCategory">
        update category set title = #{title} where id = #{id}
    </update>

注:因为在controller里是通过请求方式的不同,来执行controller方法的,所以需要设置请求方式过滤器。
get—>查询(页面跳转)
post—>添加
put----->修改
delete---->删除

配置请求方式过滤器

<!--请求方法过滤器-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <!--/*则能够匹配所有请求, / 只能匹配静态资源-->
    </filter-mapping>

问题记录:

1、在配置请求方式的过滤器时,设置为 / 后出现(Request method ‘POST’ not supported)的问题。后修改为 /* 。即可解决
2、在通过控制器方法进行修改时,设置方法参数为 Integer id,pojo对象,通过id进行修改时,Mybatis报参数错误。最后,不要id参数,把sql语句修改为 #{title} where id=#{id}即可。因为是把pojo下的实体类与sql语句映射生成最终执行的sql’语句,最后由mybatis框架执行sql并将结果映射成java对象并返回。
错误方法与sql语句为:
void updateCategory(Integer id,Category category);
update category set title = #{category.title} where id = #{id}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值