使用SSM框架 实现登录、注册、分页和增删改操作

1.

applicaltion.xml

使用SSM框架 实现登录、注册、分页和增删改操作_第1张图片



        
        
        
        
         
        
        
 

2.

db.properties

使用SSM框架 实现登录、注册、分页和增删改操作_第2张图片

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=123456
jdbc.minIdle=3
jdbc.maxActive=20
jdbc.initialSize=3
jdbc.maxWait=10000

3.

log4j.properties

使用SSM框架 实现登录、注册、分页和增删改操作_第3张图片

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.xin=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4.

mybatis-config.xml

使用SSM框架 实现登录、注册、分页和增删改操作_第4张图片





	
	


5.

spring-dao.xml

使用SSM框架 实现登录、注册、分页和增删改操作_第5张图片



        
        
        
        	
        

6.

spring-db.xml

使用SSM框架 实现登录、注册、分页和增删改操作_第6张图片



        
        
        
        
        
        	
        	
        	
        	
        	
        	
        	
        	
        	
        
        
        
        	
        	
        	
        

7.*

spring-tx.xml

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190505211338378.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1dpbnNvbTE=,size_16,color_FFFFFF,t_70)


        
         
        
        	
        
        
        
        
        

8.

springmvc-config.xml

使用SSM框架 实现登录、注册、分页和增删改操作_第7张图片



        
        
        
        
        
        
        
        	
        	
        	
        	
        

9.

web.xml



   
    
    	contextConfigLocation
    	classpath:applicationContext.xml
    
    
    	
    		org.springframework.web.context.ContextLoaderListener
    	
    
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
	
		
			contextConfigLocation
			classpath:springmvc-config.xml
		
		1
	
  
    springmvc
    /
  
  
  
  	encodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		utf-8
  	
  	
  
  
  		encodingFilter
  		/*
  
 

10.User实体类

package com.xin.po;

public class User {
	private Integer user_id;
	private String user_name;
	private String password;
	private Integer user_age;
	public Integer getUser_id() {
		return user_id;
	}
	public void setUser_id(Integer user_id) {
		this.user_id = user_id;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getUser_age() {
		return user_age;
	}
	public void setUser_age(Integer user_age) {
		this.user_age = user_age;
	}
	@Override
	public String toString() {
		return "User [user_id=" + user_id + ", user_name=" + user_name
				+ ", password=" + password + ", user_age=" + user_age + "]";
	}
	
}

11.page实体类

package com.xin.po;

import java.util.List;

public class page {
	private int currPage;//当前页数
    private int pageSize;//每页显示的记录数
    private int totalCount;//总记录数
    private int totalPage;//总页数
    private List lists;//每页的显示的数据
	public int getCurrPage() {
		return currPage;
	}
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List getLists() {
		return lists;
	}
	public void setLists(List lists) {
		this.lists = lists;
	}
	@Override
	public String toString() {
		return "page [currPage=" + currPage + ", pageSize=" + pageSize
				+ ", totalCount=" + totalCount + ", totalPage=" + totalPage
				+ ", lists=" + lists + "]";
	}
	
    
}

12.UserDao接口

package com.xin.dao;

import java.util.HashMap;
import java.util.List;

import com.xin.po.User;
import com.xin.po.page;

public interface UserDao {
	/***
	 * 根据编号查询客户信息
	 * @param id 用户编号
	 * @return 客户信息
	 */
	public List list();//查询所有
	public User get(Integer id);//查询单条
	public User getLogin(User user);//登录页
	public Integer delete(Integer id);// 删除
	public Integer update(User user); // 修改
	public Integer add(User user);// 添加
	// 查询用户记录总数
    public int getUserCount(page p);
    /**
     * 查询用户记录总数
     * @return
     */
    int selectCount();

   //分页操作,调用findByPage limit分页方法
    public List findByPage(HashMap map);
    //page findByPage(int currentPage);
}

13.UserService接口

package com.xin.service;
import java.util.List;

import com.xin.po.User;
import com.xin.po.page;

public interface UserService {
	public List list();//查询所有
	public User get(Integer id);//查询单条
	public Integer delete(Integer id);// 删除
	public Integer update(User user); // 修改
	public Integer add(User user);// 添加
	public User getLogin(User user);//登录页
	public Integer getUserCount(page p);
	 /**
     * 查询用户记录总数
     * @return
     */
    int selectCount();

	//分页操作,调用findByPage limit分页方法
    public  page findByPage(int currentPage);
	//List findByPage(HashMap map);
}

## 14.UserServiceImpl实现接口
package com.xin.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xin.dao.UserDao;
import com.xin.po.User;
import com.xin.po.page;
import com.xin.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	
	@Override
	public Integer delete(Integer id) {
		// TODO Auto-generated method stub
		return userDao.delete(id);
	}
	@Override
	public Integer update(User user) {
		// TODO Auto-generated method stub
		return userDao.update(user);
	}
	@Override
	public Integer add(User user) {
		// TODO Auto-generated method stub
		return userDao.add(user);
	}
	@Override
	public List list() {
		// TODO Auto-generated method stub
		return userDao.list();
	}
	@Override
	public User get(Integer id) {
		// TODO Auto-generated method stub
		return userDao.get(id);
	}
	@Override
	public User getLogin(User user) {
		// TODO Auto-generated method stub
		return userDao.getLogin(user);
	}
	@Override
	public Integer getUserCount(page p) {
		// TODO Auto-generated method stub
		return userDao.getUserCount(p);
	}
	/*@Override
	public List findByPage(HashMap map) {
		// TODO Auto-generated method stub
		return userDao.findByPage(map);
	}*/
	@Override
	public page findByPage(int currentPage) {
		HashMap map = new HashMap();
		page pageBean = new page();
		
	    //封装当前页数
        pageBean.setCurrPage(currentPage);
        
		//每页显示的数据
		int pageSize=5;
		pageBean.setPageSize(pageSize);
		
		//封装总记录数
		int totalCount = userDao.selectCount();
		pageBean.setTotalCount(totalCount);
		
		//封装总页数
		double tc = totalCount;
        Double num =Math.ceil(tc/pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());
      
		map.put("start",(currentPage-1)*pageSize);
		map.put("size", pageBean.getPageSize());
		//封装每页显示的数据
		List lists = userDao.findByPage(map);
		pageBean.setLists(lists);
		
		return pageBean;
	}
	@Override
	public int selectCount() {
		// TODO Auto-generated method stub
		return userDao.selectCount();
	}
	
}

15.controller控制器

package com.xin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xin.po.User;
import com.xin.po.page;
import com.xin.service.UserService;
@Controller
@RequestMapping("/user")
public class CustomerController {
	@Autowired
	private UserService userService;
	@RequestMapping("/setlogin")
	public String setFindUserById(){
		return "login";
	}
	//登录页
	@RequestMapping("/getlogin")
	public ModelAndView getLogin(HttpSession session,User user,Model model){
		User u=userService.getLogin(user);
		if(u==null){
			session.setAttribute("msg", "登录失败,请重新登录");
		/*model.addAttribute("msg", );*/
			ModelAndView mav=new ModelAndView("redirect:setlogin");
			return mav;
		}
		if (user.getUser_name()!=null&&user.getPassword()!=null&&user.getUser_name().equals(u.getUser_name())&&user.getPassword().equals(u.getPassword())) {
			model.addAttribute("msg", "登录成功");
			session.setAttribute("msg", user.getUser_name());
			ModelAndView mav=new ModelAndView("redirect:getfind");
			return mav;
		}
		model.addAttribute("msg", "登录失败,请重新登录");
		ModelAndView mav=new ModelAndView("redirect:setlogin");
		return mav;
	
}
//查询所有的页面
@RequestMapping("getfind")
public ModelAndView list(){
	ModelAndView mav = new ModelAndView();
	List list = userService.list();
	mav.addObject("list", list);
	mav.setViewName("main");
	return mav;
}
//删除页面
@RequestMapping("/delete")
public ModelAndView delete(Integer id){
	userService.delete(id);
	ModelAndView mav=new ModelAndView("redirect:getfind");
	return mav;
}

// 单条明细修改跳转到修改页面
	@RequestMapping("/editgo")
	public ModelAndView editgo(Integer id) {
		User user = userService.get(id);
		ModelAndView mav = new ModelAndView("editgo");
		mav.addObject("user", user);
		return mav;
	}
//修改页面
@RequestMapping("/update")
public ModelAndView update(User user){
	userService.update(user);
	ModelAndView mav=new ModelAndView("redirect:getfind");
	return mav;
}
// 添加   跳转 到添加页面
	@RequestMapping("addgo")
	public ModelAndView addgo() {
		ModelAndView mav = new ModelAndView("add");
		return mav;
	}
 
	// 添加数据
	@ResponseBody
	@RequestMapping("toadd")
	public ModelAndView add(User user) {
		userService.add(user);
		ModelAndView mav = new ModelAndView("redirect:getfind");
		return mav;
	}
	@RequestMapping("/main")
	public String  main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model){
		model.addAttribute("pagemsg", userService.findByPage(currentPage));//回显分页数据
		return "main";
	}

}

16.LoginInterceptor拦截器

package com.xin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		String url=arg0.getRequestURI();
		if (url.indexOf("/getlogin")!=0) {
			return true;
		}
		Object name=arg0.getSession().getAttribute("user");
		if (name!=null) {
			return true;
		}
		arg0.setAttribute("msg","登录失败");
		arg0.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(arg0, arg1);
		return false;
	}

}

17.Mapper.xml


  
  
  
  
   
  
  	delete from tbl_user where user_id=#{user_id}
  
  
  	update tbl_user set user_name=#{user_name},user_age=#{user_age},password=#{password} where user_id=#{user_id}
  
  
  	insert into tbl_user(user_name,user_age,password) values(#{user_name},#{user_age},#{password})
  
  
	
	
		
		
		
		
	

18.注册页面 add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'add.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    
添加用户信息
姓名
年龄
密码
返回

使用SSM框架 实现登录、注册、分页和增删改操作_第8张图片

19.修改界面 editgo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'editgo.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
   	
修改用户信息
编号
姓名
密码
年龄
返回

20.查询界面 find.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'find.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    

21.登录界面 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
  ${msg }
   	
用户名
密码
注册

22.主页面 main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




  
    
    
    My JSP 'main.jsp' starting page
    

  
  

   
   		
编号: 姓名: 密码: 年龄: 操作:
${user.user_id } ${user.user_name } ${user.password } ${user.user_age } 删除 修改
第${requestScope.pagemsg.currPage }/ ${requestScope.pagemsg.totalPage}页 总记录数:${requestScope.pagemsg.totalCount } 每页显示:${requestScope.pagemsg.pageSize} [首页] [上一页] [下一页] [尾页]

你可能感兴趣的:(SpringMVC,SSM)