1.User.java
package com.zr.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 + '\'' +
'}';
}
}
2.PageInfo.java
package com.zr.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 +
'}';
}
}
3.UserController.java
package com.zr.controller;
import com.zr.bean.PageInfo;
import com.zr.bean.User;
import com.zr.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 deleteById(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/addUser.do")
public String addUser(User user)
{
userService.addUser(user);
return "redirect:findAll.do";
}
@RequestMapping("/findOne.do")
public ModelAndView findOne(int id)
{
User user=userService.findOne(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("user-update");
return modelAndView;
}
@RequestMapping("/update.do")
public String updateById(User user)
{
userService.updateUser(user);
return "redirect:findAll.do";
}
}
4.UserDao.java
package com.zr.DAO;
import com.zr.bean.User;
import java.util.List;
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll(int start);
void deleteById(int id);
void addUser(User user);
void updateUser(User user);
User findOne(int id);
int getTotalCount();
}
5.LoginFilter.java
package com.zr.filter;
import com.zr.bean.User;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFliter 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.getRequestURI();
if (user==null && url.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
6.UserService.java
package com.zr.service.impl;
import com.zr.DAO.UserDao;
import com.zr.bean.PageInfo;
import com.zr.bean.User;
import com.zr.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 List<User> findAll(){
// return userDao.findAll();
// }
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void addUser(User user)
{
userDao.addUser(user);
}
public void updateUser(User user)
{
userDao.updateUser(user);
}
public User findOne(int id)
{
return userDao.findOne(id);
}
}
7.IUserService.java
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<User> findAll(int currentPage);
void deleteById(int id);
public void addUser(User user);
public void updateUser(User user);
User findOne(int id);
}
8.UserMapping.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" >
<mapper namespace="com.zr.DAO.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="com.zr.bean.User">
select * from user where username=#{username}
</select>
<select id="findAll" parameterType="int" resultType="com.zr.bean.User">
select * from user limit #{start},5
</select>
<delete id="deleteById" parameterType="int" >
delete from user where id=#{id}
</delete>
<insert id="addUser" parameterType="user">
insert into user(username,password) values(#{username},#{password})
</insert>
<select id="findOne" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
<update id="updateUser" parameterType="user" >
update user set username=#{username},password=#{password} where id=#{id}
</update>
<select id="getTotalCount" resultType="int">
select count(*) from user
</select>
<!-- 模糊查询-->
<!-- <select id="likeName" parameterType="String" resultType="com.hp.bean.Hyou">-->
<!-- select * from user where username like "%"#{name}"%"-->
<!-- </select>-->
</mapper>