IDEA入门搭建ssm,实现分页功能和拦截器

项目原理

今天学习内容是ssm项目实现分页功能和拦截器。

1.分页功能的实现思路是通过sql语句limit来确定页面的开始数以及数量多少;

2.bean层新建pageInfo类来保存分页信息;

package com.zr.bean;

import java.util.List;

public class PageInfo {
    private List list;
    private int totalCount;
    private int size;
    private int totalPage;
    private int currentPage;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "list=" + list +
                ", totalCount=" + totalCount +
                ", size=" + size +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                '}';
    }
}

3.在UserSerImpl中修改findAll()方法 

 @Override
    public PageInfo findAll(int currentPage,String username) {
        PageInfo pageInfo=new PageInfo();


        int totalCount=userDao.getTotalCount(username);
        pageInfo.setTotalCount(totalCount);
        double d=totalCount/5.0;
        int tp= (int) Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(5);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start=(pageInfo.getCurrentPage()-1)*5;
        List userList = userDao.findAll(start, 5,username);
        pageInfo.setList(userList);
        return pageInfo;
    }

4.修改前端代码 user_list.jsp


										

										
	
									${user.id}
									${user.username}
									${user.password}
									
												更新
												删除
												添加角色
												
											
										
								   

 

目录结构

IDEA入门搭建ssm,实现分页功能和拦截器_第1张图片

源码

UserController

package com.zr.controller;


import com.zr.bean.PageInfo;
import com.zr.bean.User;
import com.zr.service.IUserService;
import org.mybatis.spring.SqlSessionFactoryBean;
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 javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login.do")
    public ModelAndView login(User user,HttpSession session){

        boolean flag = userService.login(user.getUsername(),user.getPassword());
        ModelAndView modelAndView=new ModelAndView();
        if(flag){
            session.setAttribute("user",user);
            modelAndView.setViewName("main");
        }else {
            modelAndView.setViewName("../failer");
        }
        return modelAndView;
    }

    @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage, String username,
                                @RequestParam(defaultValue = "0") int type,
                                HttpSession session){
        if(type==1){
            session.setAttribute("searchname",username);
        }else if(type==0) {
            username= (String) session.getAttribute("searchname");
        }else if(type==2){
            session.removeAttribute("searchname");
        }
        PageInfo pageInfo = userService.findAll(currentPage,username);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("user-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    @RequestMapping("deleteById.do")
    public String delete(int id){
        userService.deleteById(id);
        return "redirect:findAll.do";

    }

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

    @RequestMapping("toUpdate.do")
    public ModelAndView toUpdate(int id){
        User user=userService.selectUserById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("user-update");
        return modelAndView;
    }

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


}

IUserDao

package com.zr.dao;

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

import java.util.List;

public interface IUserDao {
    User findUserByUsername(String username);
    List findAll(@Param("start") int start, @Param("size")int size,@Param("username") String username);

    void deleteById(int id);

    void add(User user);

    User selectUserById(int id);

    void update(User user);

    int getTotalCount(@Param("username") String username);
}

LoginFilter

package com.zr.filter;

import com.zr.bean.User;

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();
        User user= (User) session.getAttribute("user");
        String url=request.getRequestURL().toString();
        if(user==null && url.indexOf("login.do")==-1){
            response.sendRedirect("../login.jsp");
        }else {
            filterChain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
UserServiceImpl
import com.zr.bean.PageInfo;
import com.zr.bean.User;
import com.zr.dao.IUserDao;
import com.zr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public boolean login(String username, String password) {
        User user = userDao.findUserByUsername(username);
        if(user!=null&&user.getPassword().equals(password)){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo findAll(int currentPage,String username) {
        PageInfo pageInfo=new PageInfo();


        int totalCount=userDao.getTotalCount(username);
        pageInfo.setTotalCount(totalCount);
        double d=totalCount/5.0;
        int tp= (int) Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(5);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start=(pageInfo.getCurrentPage()-1)*5;
        List userList = userDao.findAll(start, 5,username);
        pageInfo.setList(userList);
        return pageInfo;
    }

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

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

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

    @Override
    public void update(User user) {
        userDao.update(user);
    }
}
IUserService
package com.zr.service;

import com.zr.bean.PageInfo;
import com.zr.bean.User;

import java.util.List;

public interface IUserService {

    boolean login(String username,String password);

    PageInfo findAll(int currentPage,String username);

    void deleteById(int id);

    void add(User user);

    User selectUserById(int id);

    void update(User user);
}

UserMapper.xml




    
    
    
        delete from tb_user where id =#{id}
    
    
        insert into tb_user(username,password) values (#{username},#{password})
    
    
    
        update tb_user set username=#{username},password=#{password} where id =#{id}
    

    

运行结果

查询所有

IDEA入门搭建ssm,实现分页功能和拦截器_第2张图片

查询1

 IDEA入门搭建ssm,实现分页功能和拦截器_第3张图片

 IDEA入门搭建ssm,实现分页功能和拦截器_第4张图片

翻页

 IDEA入门搭建ssm,实现分页功能和拦截器_第5张图片

你可能感兴趣的:(作业)