基于spring+mybatis框架的管理系统:简单实现增、删、改、查。(完)

1.User

package com.zhongruan.bean;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2.IUserDao

package com.zhongruan.dao;

import com.zhongruan.bean.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface IUserDao {
    List<User> findAll(@Param("searchname") String searchname);
    User selectByUserName(String name);
    void deleteById(int id);
    User selectById(int id);
    void updateById(User user);
    void insert(User user);

}

3.IUserService

package com.zhongruan.service;

import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.User;


import java.util.List;

public interface IUserService {
    List<User> findAll(int currentPage,int size,String searchname);
    Boolean login(String username,String password);
    void deleteById(int id);
    void updateById(User user);
    void insert(User user);
    User selectById(int id);

}

4.UserService

package com.zhongruan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.bean.User;

import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class UserService implements IUserService {


  @Autowired
  private IUserDao userDao;


    @Override
    public List<User> findAll(int currentPage,int size,String searchname) {
        PageHelper.startPage(currentPage,size);
        return userDao.findAll(searchname);
    }


  @Override
  public Boolean login(String username, String password) {
   User user=userDao.selectByUserName(username);

   if (user!=null && password.equals(user.getPassword())){
     return true;
   }
    return false;
  }

    @Override
    public void deleteById(int id) {
         userDao.deleteById(id);
    }

       @Override
    public User selectById(int id) {
        return userDao.selectById(id);
    }



    @Override
    public void updateById(User user) {
         userDao.updateById(user);
    }

    @Override
    public void insert(User user) {
        userDao.insert(user);
    }
}

5.LoginFilter

package com.zhongruan.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        HttpSession session=request.getSession();
        if (session.getAttribute("username")==null&&request.getRequestURI().indexOf("/login.do")==-1){
            response.sendRedirect("index.jsp");
        }else {
            filterChain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}

6.UserController

package com.zhongruan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zhongruan.bean.User;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "5") int size,
                                String searchname, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        String username= (String) session.getAttribute("username");
        if (username==null){
          modelAndView.setViewName("failure.jsp");
            return modelAndView;
        }
        if (searchname!=null){
            session.setAttribute("searchname",searchname);
        }else {
            searchname= (String) session.getAttribute("searchname");
        }
       List<User> users=userService.findAll(pageNum,size, searchname);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        modelAndView.setViewName("allUser.jsp");
        modelAndView.addObject("pageInfo",pageInfo);
        return modelAndView;
    }


    @RequestMapping("/login.do")
    public String login(User user,HttpSession session){
      Boolean flag=userService.login(user.getUsername(),user.getPassword());
      if (flag){
          session.setAttribute("username",user.getUsername());
          return "redirect:/findAll.do";
      }else {
          return "failure.jsp" ;
      }
    }

    @RequestMapping("/delete.do")
    public String deleteById(int id,HttpSession session){
       /*String username= (String) session.getAttribute("username");
       if (username==null){
           return "failure.jsp";
       }*/
        userService.deleteById(id);
        return "redirect:/findAll.do";
    }


    @RequestMapping("/toupdate.do")
    public ModelAndView toUpdate(int id){
       User user=userService.selectById(id);
       ModelAndView mv=new ModelAndView();
       mv.addObject("user",user);
       mv.setViewName("updateUser.jsp");
       return mv;
    }

    @RequestMapping("/update.do")
    public String updateById(User user){
        userService.updateById(user);
        return "redirect:/findAll.do";
    }


    @RequestMapping("/insert.do")
    public String insert(User user){
        userService.insert(user);
        return "redirect:/findAll.do";
    }



}

你可能感兴趣的:(基于spring+mybatis框架的管理系统:简单实现增、删、改、查。(完))