第三天:实现简单的添加和删除功能

实现用户管理系统的添加和删除用户信息功能

  • 开发环境
  • 具体实现
    • .java文件
    • .xml文件
    • .jsp文件
  • 运行界面

开发环境

  • 64位的Windows系统
  • Server version: 5.5.58
  • MySQL Community Server (GPL)
  • java version “1.8.0_211”
  • SQLyog
  • IDEA
  • Apache Maven 3.6.0

具体实现

.java文件

//UserDao.java
package com.ldb.dao;

import com.ldb.bean.User;

import java.util.List;

//UserDao -- 数据库交互层
public interface UserDao {

    //查询全部; ListUser user = new ArrayList<>()
    List<User> findAllUser();

    //添加用户
    int save(User user);
    
    //删除用户
    int delete(User user);
}
//UserService.java
package com.ldb.service;

import com.ldb.bean.User;

import java.util.List;

//业务逻辑层
public interface UserService {
    // 查询全部
    List<User> findAllUser();

    //添加用户
    int save(User user);
    
    //删除用户
    int delete(User user);
}
//UserServiceImpl.java
package com.ldb.service.impl;

import com.ldb.bean.User;
import com.ldb.dao.UserDao;
import com.ldb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//Spring通过扫描注解来控制每个层,告诉Spring这里是service层
public class UserServiceImpl implements UserService {

    @Autowired
    //自动注入注解
    private UserDao userDao;

    @Override
    //查询全部
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }

    @Override
    //添加用户
    public int save(User user) {
        return userDao.save(user);
    }

    //删除用户
    public int delete(User user){
        return userDao.delete(user);
    }
}
//UserController.java
package com.ldb.controller;


import com.ldb.bean.User;
import com.ldb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
//RequestMapping地址映射器,所有访问controller层都会访问这个地址,"/user"(根目录路径)指当前地址
public class UserController {

    @Autowired
    //controller层需要访问service层
    private UserService userService;
    private int num;

    //查询全部用户
    @RequestMapping("/findAllUser.do")
    //所有需要返回数据到页面的,全部都要放在ModelAndView中返回
    public ModelAndView findAllUser(){
        //1. 调用service方法查询并得到结果
        List<User> userList = userService.findAllUser();

        //2. 实例化ModelAndView
        ModelAndView mv = new ModelAndView();

        //3. 接受数据
        mv.addObject("userList", userList);

        //4. 返回数据到页面,已经在spring-mvc.xml文件中的视图的解析器中添加了文件的前缀/jsp/和后缀.jsp
        mv.setViewName("allUser");

        return mv;
    }

    //添加用户
    @RequestMapping("/toAddUser.do")
    public String toAddUser(){
        return "addUser";
    }
    @RequestMapping("/save.do")
    public ModelAndView save(User user){
        userService.save(user);
        return findAllUser();
    }
    
    //删除用户
    @RequestMapping("/delete.do")
    public ModelAndView delete(User user){
        userService.delete(user);
        return findAllUser();
    }
}

.xml文件

UserMapper.xml


<mapper namespace="com.ldb.dao.UserDao" >


    
    <select id="findAllUser" resultType="com.ldb.bean.User">
        select * from tb_user
    select>

    
    <insert id="save" parameterType="com.ldb.bean.User">
        insert into tb_user value (#{id}, #{username}, #{password})
    insert>

    
    <delete id="delete" parameterType="com.ldb.bean.User">
        delete from tb_user where id = #{id}
    delete>
    
mapper>

.jsp文件

//显示所有用户界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>user列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于SSM框架的管理系统:简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>用户列表 —— 显示所有用户</small>
                </h1>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" href="${pageContext.request.contextPath}/user/toAddUser.do">新增</a>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>用户名</th>
                    <th>密码</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${userList}" var="userInfo">
                    <tr>
                        <td>${userInfo.id}</td>
                        <td>${userInfo.username}</td>
                        <td>${userInfo.password}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${userInfo.id}">更改</a> |
                            //删除功能按键
                            <a href="${pageContext.request.contextPath}/user/delete.do?id=${userInfo.id}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
//addUser.jsp
//添加用户界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>新增用户</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    基于SSM框架的管理系统:简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>新增用户</small>
                </h1>
            </div>
        </div>
    </div>
    
    <form action="${pageContext.request.contextPath}/user/save.do"
              method="post">&nbsp;&nbsp;&nbsp;&nbsp;id:<input type="text" name="id"><br><br><br>
        用户姓名:<input type="text" name="username"><br><br><br>
        用户密码:<input type="text" name="password"><br><br><br>
        <input type="submit" value="添加" >
    </form>

</div>

运行界面

添加用户界面,输入完成点击添加按钮,自动跳转到全部用户界面
第三天:实现简单的添加和删除功能_第1张图片
点击删除选项,自动删除该项并刷新显示全部用户界面
第三天:实现简单的添加和删除功能_第2张图片

你可能感兴趣的:(ssm)