User

package com.lyc.pojo;

import lombok.Data;

@Data
public class User {
    private int id;
    private String count;
    private String password;
}

UserMapper

public interface UserMapper {
    //添加用户
    int addUser(User user);
    //显示用户
    List<User> queryUser();
    //根据ID查询用户
    User queryUserByID(@Param("id")int id);
    //模糊查询
    List<User> mquser(@Param("count")String count);
    //根据id修改用户信息
    int updateUserById(User user);
    //根据id删除用户
    int deleteUserById(@Param("id")int id);
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lyc.mapper.UserMapper">
    <insert id="addUser">
        insert into ssmbuild.user(id,count,password) values (null,#{count},#{password})
    </insert>
    <select id="queryUser" resultType="com.lyc.pojo.User">
        select * from ssmbuild.user
    </select>
    <select id="queryUserByID" parameterType="int" resultType="User">
        select * from ssmbuild.user where id = #{id}
    </select>
    <select id="mquser" resultType="User">
        select * from ssmbuild.user <if test="count !=null"> where count like #{count} </if>
    </select>
    <update id="updateUserById" parameterType="com.lyc.pojo.User">
        update ssmbuild.user set count=#{count},password=#{password} where id=#{id}
    </update>
    <delete id="deleteUserById" parameterType="int">
        delete from ssmbuild.user where id = #{id}
    </delete>
</mapper>

UserService

package com.lyc.service;

import com.lyc.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface UserService {
    //添加用户
    int addUser(User user);
    //显示用户
    List<User> queryUser();
    //根据ID查询用户
    User queryUserByID(@Param("id")int id);
    //模糊查询
    List<User> mquser(@Param("count")String count);
    //根据id修改用户信息
    int updateUserById(User user);
    //根据id删除用户
    int deleteUserById(@Param("id")int id);
}

UserServiceImpl

package com.lyc.service;

import com.lyc.mapper.UserMapper;
import com.lyc.pojo.User;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService{
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public List<User> queryUser() {
        return userMapper.queryUser();
    }

    @Override
    public User queryUserByID(int id) {
        return userMapper.queryUserByID(id);
    }

    @Override
    public List<User> mquser(String count) {
        return userMapper.mquser(count);
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public int deleteUserById(int id) {
        return userMapper.deleteUserById(id);
    }
}

Controller

@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserService userService;
    //跳转到成功页面
    @RequestMapping("/jumpSuccess")
    public String jumpSuccess()throws Exception{
        return "success";
    }
    //跳转添加用户页面
    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "adduser";
    }

    //跳转到修改用户界面
    @RequestMapping("/toupdateUser")
    public String toupdateUser(int id , Model model){
        model.addAttribute("queryUser", userService.queryUserByID(id));
        return "updateUser";
    }

    //修改用户信息
    @RequestMapping("/updateUserById")
    public String updateUserById(User user){
        System.out.println("updateBook=>"+user);
        userService.updateUserById(user);
        return "redirect:allUser";
    }

    //添加用户
    @RequestMapping("/addUser")
    public String adduser(User user){
        userService.addUser(user);
        return "redirect:allUser";
    }

    //删除用户
    @RequestMapping("/deleteUserById")
    public String deleteUserById(int id){
        userService.deleteUserById(id);
        return "redirect:allUser";
    }

    //跳转到全部用户信息页面
    @RequestMapping("/toAllUsers")
    public String allUsers(){
        return "allUser";
    }
    //用户信息
    @RequestMapping("allUser")
    public String allUser(Model model){
        List<User> list =userService.queryUser();
        model.addAttribute("list", list);
        return "allUser";
    }

    //模糊查询
    @RequestMapping("/mqUser")
    public String mqUser(@RequestParam(name = "count",required = false) String count, Model model){
        List<User> mq = userService.mquser(count == null ? null : "%"+count+"%");
        if (mq==null){
            mq = userService.queryUser();
            System.out.println("dsfjdskfhjdsfds====>"+mq);
            model.addAttribute("error", "未查到!");
        }else{
            model.addAttribute("list", mq);
            System.out.println("mq======>"+mq);
        }
        return "allUser";
    }
}

jsp

addUser.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../static/layui-v2.6.4/layui/css/layui.css" media="all">
    <script src="../../static/layui-v2.6.4/layui/layui.js" charset="utf-8"></script>
    <title>登录</title>
</head>
<body>
<form class="layui-form" action="/user/addUser">
    <div class="layui-form-item">
        <label class="layui-form-label">账号框</label>
        <div class="layui-input-inline">
            <input type="text" name="count" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码框 </label>
        <div class="layui-input-inline">
            <input type="password"
                   name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>

    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
</html>


allUser

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="../../static/layui-v2.6.4/layui/css/layui.css" media="all">


    <title>用户信息</title>
    <style>
        div{
            text-align: center;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h1>用户页面</h1>
        </div>
    </div>
</div>
<hr>
<div class="layui-row">
    <div class="layui-col-md4">
        <%--    toAddBook    --%>
        <a class="layui-btn" href="${pageContext.request.contextPath}/user/toAddUser">注册用户</a>
        <a class="layui-btn" href="${pageContext.request.contextPath}/user/allUser">显示全部用户</a>
    </div>

    <div class="layui-col-md4">
        <%--    搜索栏    --%>
        <form class="layui-form" action="${pageContext.request.contextPath}/user/mqUser" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red;font-weight: bold">${error}</span></label>
                <div class="layui-input-block">
                    <input  type="text" name="count"   lay-verify="required" placeholder="请输入要查询的用户名称" autocomplete="off" class="layui-input layui-input-inline">
                    <input type="submit" class="layui-btn-sm layui-btn-warm" value="查询" style="border: none">
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md12">
            <table class="layui-table">
                <colgroup>
                    <col width="200">
                    <col width="200">
                    <col width="200">
                    <col width="200">
                    <col>
                </colgroup>
                <thead>
                <tr>
                    <th>用户ID</th>
                    <th>用户账号</th>
                    <th>用户密码</th>
                    <th>基本操作</th>
                </tr>
                </thead>
                <%--  书籍从数据库中查询出来,从list中遍历出来 forEach --%>
                <tbody>
                <tr>
                    <c:forEach var="user" items="${list}">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.count}</td>
                    <td>${user.password}</td>
                    <td>
                        <a class="layui-btn layui-btn-xs layui-btn-danger " href="${pageContext.request.contextPath}/user/deleteUserById?id=${user.id}">删除</a>
                        <a class="layui-btn layui-btn-xs layui-btn-normal " href="${pageContext.request.contextPath}/user/toupdateUser?id=${user.id}">修改</a>
                    </td>
                </tr>
                </c:forEach>
                </tbody>
            </table>
            <div id="pg"></div>
        </div>
    </div>
</div>
</body>
</html>
posted on 2021-04-26 15:50  liuyunche  阅读(175)  评论(0编辑  收藏  举报