//PageInfo
package com.zhongruan.bean;
import java.util.List;
public class PageInfo {
private List list;
private int totalPage;
private int size;
private int totalCount;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
//UserController
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = “1”) int currentPage){
PageInfo pageInfo = userService.findAll(currentPage);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject(“pageInfo”,pageInfo);
modelAndView.setViewName(“user-list”);
return modelAndView;
}
//UserDao
package com.zhongruan.dao;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.User;
import java.util.List;
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll(int start);
void deleteById(int id);
void add(User user);
User selectById(int id);
void Update(User user);
int getTotalCount();
}
//LoginFilter
package com.zhongruan.filter;
import com.zhongruan.bean.User;
import javax.jws.soap.SOAPBinding;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.LogRecord;
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 uri = request.getRequestURI();
if(user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
//UserService
package com.zhongruan.service.impl;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
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 UserDao 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<User> findAll(int currentPage){
PageInfo<User> pageInfo = new PageInfo<>();
pageInfo.setSize(5);
int tc = userDao.getTotalCount();
pageInfo.setTotalCount(tc);
int tp=(int)Math.ceil(tc/5.0);
pageInfo.setTotalPage(tp);
if(currentPage<1){
pageInfo.setCurrentPage(1);
}else if(currentPage>tp){
pageInfo.setCurrentPage(tp);
}else {
pageInfo.setCurrentPage(currentPage);
}
int start = (pageInfo.getCurrentPage()-1)*5;
List<User> userList = userDao.findAll(start);
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.selectById(id);
}
@Override
public void Update(User user) { userDao.Update(user); }
}
//IUserService
package com.zhongruan.service;
import com.zhongruan.bean.PageInfo;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserService {
boolean login(String username,String password);
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void Update(User user);
PageInfo<User> findAll(int currentPage);
}
//UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
select * from wzbc1 where username=#{username}
<select id="findAll" resultType="user" parameterType="int">
select * from wzbc1 limit #{start},5
</select>
<delete id="deleteById" parameterType="int">
delete from wzbc1 where id = #{id}
</delete>
<insert id="add" parameterType="user">
insert into wzbc1 (id,username,password) values (#{id},#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from wzbc1 where id=#{id}
</select>
<update id="Update" parameterType="user">
update wzbc1 set id=#{id}, username=#{username}, password=#{password} where id= #{id}
<select id="getTotalCount" resultType="int">
select count(*) from wzbc1
</select>
//user-list.jsp
</div>
//web.xml
loginFilter
com.zhongruan.filter.LoginFilter
loginFilter
*.do