package com. zhongruan. bean;
import java. util. List;
public class PageInfo < T> {
private List< T> 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;
}
@Override
public String toString ( ) {
return "PageInfo{" +
"list=" + list +
", totalPage=" + totalPage +
", size=" + size +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}' ;
}
}
package com. zhongruan. bean;
public class User {
public User ( ) {
}
public User ( int id, String username, String password) {
this . id = id;
this . username = username;
this . password = password;
}
public User ( String username, String password) {
this . username = username;
this . password = password;
}
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 + '\'' +
'}' ;
}
}
package com. zhongruan. controller;
import com. zhongruan. bean. PageInfo;
import com. zhongruan. bean. User;
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 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) {
PageInfo< User> pageInfo= userService. findAll ( currentPage) ;
ModelAndView modelAndView = new ModelAndView ( ) ;
modelAndView. addObject ( "pageInfo" , pageInfo) ;
modelAndView. setViewName ( "user-list" ) ;
return modelAndView;
}
@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. setViewName ( "user-update" ) ;
modelAndView. addObject ( "user" , user) ;
return modelAndView;
}
@RequestMapping ( "/update.do" )
public String update ( User user) {
userService. update ( user) ;
return "redirect:findAll.do" ;
}
}
package com. zhongruan. dao;
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 selectUserId ( int id) ;
void update ( User user) ;
int getTotalCount ( ) ;
}
package com. zhongruan. filter;
import com. zhongruan. 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 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 ( ) {
}
}
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 ;
} else {
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. selectUserId ( id) ;
}
@Override
public void update ( User user) {
userDao. update ( user) ;
}
}
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) ;
PageInfo< User> findAll ( int currentPage) ;
void deleteById ( int id) ;
void add ( User user) ;
User selectUserById ( int id) ;
void update ( User user) ;
}
< ? 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" >
< mapper namespace= "com.zhongruan.dao.UserDao" >
< select id= "findUserByUserName" parameterType= "string" resultType= "com.zhongruan.bean.User" >
select * from tb_user where username= #{ username}
< / select>
< select id= "findAll" resultType= "com.zhongruan.bean.User" parameterType= "int" >
select * from tb_user limit #{ start} , 5
< / select>
< delete id= "deleteById" parameterType= "int" >
delete from tb_user where id = #{ id}
< / delete>
< insert id= "add" parameterType= "user" >
insert into tb_user ( username, password) values ( #{ username} , #{ password} )
< / insert>
< select id= "selectUserId" parameterType= "int" resultType= "com.zhongruan.bean.User" >
select * from tb_user where id= #{ id}
< / select>
< update id= "update" parameterType= "user" >
update tb_user set username= #{ username} , password= #{ password} where id= #{ id}
< / update>
< select id= "getTotalCount" resultType= "int" >
select count ( * ) from tb_user
< / select>
< / mapper>
< ? xml version= "1.0" encoding= "UTF-8" ? >
< web- app xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://xmlns.jcp.org/xml/ns/javaee"
xsi: schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version= "3.1" >
< ! -- 配置加载类路径的配置文件 -- >
< context- param>
< param- name> contextConfigLocation< / param- name>
< param- value> classpath* : applicationContext. xml< / param- value>
< / context- param>
< ! -- 配置监听器 -- >
< listener>
< listener- class > org. springframework. web. context. ContextLoaderListener< / listener- class >
< / listener>
< listener>
< listener- class > org. springframework. web. context. request. RequestContextListener< / listener- class >
< / listener>
< ! -- 解决中文乱码过滤器 -- >
< filter>
< filter- name> characterEncodingFilter< / filter- name>
< filter- class > org. springframework. web. filter. CharacterEncodingFilter< / filter- class >
< init- param>
< param- name> encoding< / param- name>
< param- value> UTF- 8 < / param- value>
< / init- param>
< / filter>
< filter- mapping>
< filter- name> characterEncodingFilter< / filter- name>
< url- pattern>