ssm注册修改查询实现

修改上次框架
在usermapper中添加sql语句

<update id="update" parameterType="User">
        update user set name=#{name},password=#{password} where id=#{id}
    </update>
    
    <select id="searchByName" parameterType="String" resultType="User">
        select * from user where name like concat('%',#{name},'%')
    </select>

在userdao中添加代码

int add(User user);

    int update(User user);

    User findUserById(Integer id);

    List<User> searchByName(String name);

在userservice中添加代码

boolean add(User user);

    User findById(Integer id);

    boolean update(User user);

    List<User> searchByName(String name);

在userserviceimpl中添加代码

@Override
    public boolean add(User user) {
        int ad=userDao.add(user);
        if (ad>0){
            return true;
        }
        return false;
    }

    @Override
    public User findById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public boolean update(User user) {
        int upd=userDao.update(user);
        if (upd>0){
            return true;
        }
        return false;
    }

    @Override
    public List<User> searchByName(String name) {
        return userDao.searchByName(name);
    }

在usercontroller中添加代码

@RequestMapping("/add.do")
    public String add(User user){
        boolean ad=userService.add(user);
        if (ad){
            return "redirect:findAll.do";
        }
        return "../failure";
    }

    @RequestMapping("/findById.do")
    public String findById(int id, Model model){
        User user=userService.findById(id);
        model.addAttribute("user",user);
        return "../modify";
    }

    @RequestMapping("/update.do")
    public String update(User user){
        boolean upd=userService.update(user);
        if (upd){
                return "redirect:findAll.do";
        }
        return "../failure";
    }

修改main页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>列表</title>
</head>
<body>
<div>
    <form action="/user/findAll.do">
        <input id="name" type="search" name="name" value="${name}">
        <button onclick="form.submit()">搜索</button>
    </form>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>用户名</th>
            <th>密码</th>
            <th>操作</th>
        </tr>
        </thead>

        <tbody>
        <c:forEach items="${userList}" var="user">
            <tr>
                <td>${user.id}</td>
                <td>${user.name}</td>
                <td>${user.password}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/user/delete.do?id=${user.id}">删除</a>
                    <a href="${pageContext.request.contextPath}/user/findById.do?id=${user.id}">修改</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

添加modify.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
</head>
<body>
<div>
    <form action="/user/update.do" method="post">
        <input name="id" value="${user.id}" type="hidden">
        <div>
            <label for="name">用户名:</label>
            <input name="name" type="text" id="name" value="${user.name}">
        </div>
        <div>
            <label for="password">密码:</label>
            <input name="password" type="text" id="password" value="${user.password}">
        </div>
        <div>
            <input type="submit" value="提交">
        </div>
        <a href="javascript:window.history.go(-1)">返回</a>
    </form>
</div>
</body>
</html>

添加add.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增</title>
</head>
<body>
<div>
    <form action="/user/add.do" method="post">
        <div>
            <label for="name">用户名:</label>
            <input name="name" type="text" id="name">
        </div>
        <div>
            <label for="password">密码:</label>
            <input name="password" type="text" id="password">
        </div>
        <div>
            <input type="submit" value="提交">
        </div>
        <a href="javascript:window.history.go(-1)">返回</a>
    </form>
</div>
</body>
</html>

修改index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<form action="/user/login.do" method="post">
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    <input type="submit" value="login">
    <a href="add.jsp">注册</a>
    <a href="/user/findAll.do?name=">查看数据</a>
</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值