在做del业务时,传递参数,和接口中入参注释

例如这样一条url  ,http://xxx.xxx.xxx/zhouyi2/roleDel/35  ,这样的一个url地址,他的传递参数过程是

1.  首先在controller里,在地址映射中用 {} 大括号,把id,作为占位符。再在方法中 用@PathVariable 把地址变量,传递进来。

@RequestMapping("/roleDel/{id}")
    public String roleDel(@PathVariable("id") Integer id) {
        
        roleService.deleteRole(id);
        return "redirect:/getRoles";
    } 

 2. 在mybatis的映射xml文件中,用#号,作为占位符,构建delete语句。

<delete id="deleteRole">
delete from tbl_role where id = #{id}
</delete>

3. 这个xml文件中,需要知道这个id从哪里传来。需要用@Param注释,在接口中传进去。

其中    public int deleteRole(@Param("id") Integer id);

就是传进去的参数。

public interface RoleMapper {

    public int addRole(Role role);
    
    public int modifyRole(Role role);
    
    public int deleteRole(@Param("id") Integer id);   // 本例
    
    public Role getRoleById(Integer id);
    
    public Role getRoleByName(String name);
    
    public List<Role> getRoles();
    
}

另外,在delete页面的前端,可以用onclick事件,增加确认按钮,如下

<a href="${pageContext.request.contextPath }/roleDel/${role.id} ">
<
button type="button" class="btn btn-primary btn-xs" onclick="return confirm('确定要删除吗?')">删除</button>
</
a>

在bootstrap中,可以给button外面再包裹一层href的a标签。

原文地址:https://www.cnblogs.com/sdgtxuyong/p/12038249.html