小白ssm+maven+分页查询+事务管理+crud

Springmvc+Spring+Mybatis+Maven

目录

Springmvc+Spring+Mybatis+Mave

小白ssm+maven+分页查询+事务管理+crud_第1张图片


 

 

  1. 环境准备

    发工具:Eclipse Java EE IDE for Web Developers Version: Oxygen.3a Release
    开发环境:Jdk 1.8.0_171
    服务器:apache-tomcat-7.0.78-windows-x64

  2. 创建maven project
    小白ssm+maven+分页查询+事务管理+crud_第2张图片小白ssm+maven+分页查询+事务管理+crud_第3张图片小白ssm+maven+分页查询+事务管理+crud_第4张图片
  3. 创建好工程后:
    小白ssm+maven+分页查询+事务管理+crud_第5张图片

看见工程有红叉,是因为缺少web.xml 文件,解决:

右键项目 --> Java EE Tools --> Generate Deployment Descriptor Stub

 

  1. 编写pom.xml文件
    
    	4.0.0
    	com.ljh
    	maven_ssm
    	0.0.1-SNAPSHOT
    	war
    	
    		
    			
    				org.apache.maven.plugins
    				maven-compiler-plugin
    				3.2
    				
    					1.7
    					1.7
    					UTF-8
    				
    			
    
    			
    				org.apache.tomcat.maven
    				tomcat7-maven-plugin
    				2.2
    				
    					8080
    					UTF-8
    				
    			
    		
    	
    
    	
    		
    			javax.servlet
    			servlet-api
    			3.0-alpha-1
    			provided
    		
    		
    			junit
    			junit
    			3.8.1
    			test
    		
    		
    		
    			org.springframework
    			spring-test
    			4.2.5.RELEASE
    		
    		
    			org.springframework
    			spring-webmvc
    			4.3.12.RELEASE
    		
    		
    			org.springframework
    			spring-core
    			4.3.12.RELEASE
    		
    		
    			org.springframework
    			spring-aop
    			4.3.12.RELEASE
    		
    		
    			aspectj
    			aspectjweaver
    			1.5.4
    		
    		
    			aspectj
    			aspectjrt
    			1.5.4
    		
    		
    			org.codehaus.jackson
    			jackson-mapper-asl
    			1.9.11
    		
    		
    		
    			org.mybatis
    			mybatis
    			3.1.1
    		
    		
    			org.mybatis
    			mybatis-spring
    			1.1.1
    		
    		
    			mysql
    			mysql-connector-java
    			5.1.21
    		
    		
    			c3p0
    			c3p0
    			0.9.1.2
    			jar
    		
    		
    		
    			org.slf4j
    			slf4j-api
    			1.7.5
    		
    		
    			org.slf4j
    			slf4j-log4j12
    			1.7.5
    		
    		
    			log4j
    			log4j
    			1.2.17
    		
    		
    			org.springframework
    			spring-context-support
    			4.3.12.RELEASE
    			jar
    			compile
    		
    		
    			javax.servlet
    			jstl
    			1.2
    		
    	
    
  2. 编写pojo(实体类User和分页工具类PageUtil)
    package com.ljh.user.pojo;
    
    public class User {
    	private Integer userId;
    	private String userName;
    	private String userPwd;
    	private String userEmail;
    	private Integer userAge;
    	private String userIdCardNo;
    	
    	@Override
    	public String toString() {
    		return "User [userId=" + userId + ", userName=" + userName + ", userPwd=" + userPwd + ", userEmail=" + userEmail
    				+ ", userAge=" + userAge + ", userIdCardNo=" + userIdCardNo + "]";
    	}
    	
    	public Integer getUserId() {
    		return userId;
    	}
    	public void setUserId(Integer userId) {
    		this.userId = userId;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getUserPwd() {
    		return userPwd;
    	}
    	public void setUserPwd(String userPwd) {
    		this.userPwd = userPwd;
    	}
    	public String getUserEmail() {
    		return userEmail;
    	}
    	public void setUserEmail(String userEmail) {
    		this.userEmail = userEmail;
    	}
    	public Integer getUserAge() {
    		return userAge;
    	}
    	public void setUserAge(Integer userAge) {
    		this.userAge = userAge;
    	}
    	public String getUserIdCardNo() {
    		return userIdCardNo;
    	}
    	public void setUserIdCardNo(String userIdCardNo) {
    		this.userIdCardNo = userIdCardNo;
    	}
    	
    }
    package com.ljh.page.pojo;
    
    import java.util.List;
    
    /**
     * 分页工具类.泛型类
     */
    public class PageUtil{
       //属性
        private int pageNumber;//总记录数
        private int pageCount;//总页数
        private int pageIndex;//当前页
        private int pageSize;//每页大小
        private List list;//当前页的数据
        
        public int getPageNumber() {
            return pageNumber;
        }
        public void setPageNumber(int pageNumber) {
            this.pageNumber = pageNumber;
        }
        public int getPageCount() {
            return pageCount;
        }
        public void setPageCount(int pageCount) {
            this.pageCount = pageCount;
        }
        public int getPageIndex() {
            return pageIndex;
        }
        public void setPageIndex(int pageIndex) {
            this.pageIndex = pageIndex;
        }
        public int getPageSize() {
            return pageSize;
        }
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
        public List getList() {
            return list;
        }
        public void setList(List list) {
            this.list = list;
        }
    
    }
  3. 编写控制层UserController
    package com.ljh.user.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.ljh.page.pojo.PageUtil;
    import com.ljh.user.pojo.User;
    import com.ljh.user.service.UserService;
    
    /**
     * 控制层
     * @author 46656
     *
     */
    @Controller
    @RequestMapping("user")
    public class UserController {
    	@Autowired
    	private UserService userServiceImp;
    	
    	/**
    	 * 用户注册
    	 * @param user
    	 * @return
    	 */
    	@RequestMapping("reg")
    	public ModelAndView reg(User user) {
    		userServiceImp.reg(user);
    		ModelAndView mav = new ModelAndView();
    		mav.setViewName("forward:/user/getAllUser.action");
    		return mav;	
    	}
    	
    	/**
    	 * 查询全部用户
    	 * @return
    	 */
    	@RequestMapping("getAllUser")
    	public ModelAndView getAllUser() {
    		List userList = userServiceImp.getAllUser();
    		ModelAndView mav = new ModelAndView();
    		mav.setViewName("getAllUser");
    		mav.addObject("userList", userList);
    		return mav;
    	}
    	
    	/**
    	 * 根据ID查找用户
    	 * @param user
    	 * @return
    	 */
    	@RequestMapping("getUserById")
    	public ModelAndView getUserById(User user) {
    		User userGet = userServiceImp.getUserById(user);
    		ModelAndView mav = new ModelAndView();
    		mav.setViewName("updateUser");
    		mav.addObject("userGet", userGet);
    		return mav;
    	}
    	
    	
    	/**
    	 * 修改用户
    	 * @param user
    	 * @return
    	 */
    	@RequestMapping("updateUser")
    	public ModelAndView updateUser(User user) {
    		userServiceImp.updateUser(user);
    		ModelAndView mav = new ModelAndView();
    		mav.setViewName("forward:/user/getAllUser.action");
    		return mav;		
    	}
    	
    	/**
    	 * 删除用户
        * 带事务控制
    	 * @param user
    	 * @return
    	 */
    	@RequestMapping("delUser")
    	public ModelAndView delUser(User user) {
    		userServiceImp.delUser(user);
    		ModelAndView mav = new ModelAndView();
    		mav.setViewName("redirect:/user/getAllUser.action");
    		return mav;
    	}
    	
    	/**
    	 * 登录
    	 * @param user
    	 * @return
    	 */
    	@RequestMapping("login")
    	public ModelAndView login(User user) {
    		
    		User userLogined = userServiceImp.login(user);
    		ModelAndView mav = new ModelAndView();
    		
    		if(null == userLogined || "".equals(userLogined)) {
    			mav.setViewName("forward:/login.jsp");
    			mav.addObject("fail", "fail");
    		}else {
    			mav.setViewName("forward:/user/getAllUser.action");
    			mav.addObject("userLogined", userLogined);
    		}
    		
    		return mav;
    			
    	}
    	
    	
    	/**
    	 * 分页
    	 * @param request
    	 * @param model
    	 * @return
    	 */
        @RequestMapping("getUserByPage")
        public ModelAndView getUserByPage(HttpServletRequest request, Model model) {
            int pageIndex = 1;	//设置初始的当前页,页面显示的都是第一页
            int pageSize = 10;	//此处的值与sql语句的limit后面的数值保持一致
            
            PageUtil pageUtil = new PageUtil();  List list = new ArrayList();
            
            if (request.getParameter("pageIndex") != null) {
                pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            }	//获取页面上点击'上一页或者下一页'传过来的pageindex
           
            pageUtil.setPageIndex(pageIndex);	//保存
            
            int number = userServiceImp.pageCount();	//获得总数据量
            
            pageUtil.setPageNumber(number);	//保存
            pageUtil.setPageSize(pageSize);	//保存
            pageUtil.setPageCount((int) Math.ceil((double) (pageUtil.getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
            
            int index = (pageIndex - 1) * pageSize;	//计算出每一页从数据库中第几条数据开始取值,也就是limit后面的第一个数字
            
            list = userServiceImp.getUserByPage(index);	//取得数据库中的值
            
            pageUtil.setList(list);	//保存到工具类中的集合
            
            model.addAttribute("pageUtil", pageUtil);	//传递到页面,存入值栈中
            return new ModelAndView("getUserByPage");	//跳转的相关页面
    	
        }
    
    }
  4. 编写页务层(这里只写UserService实现,省略接口了)
    package com.ljh.user.service.imp;
    
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import com.ljh.user.mapper.UserMapper;
    import com.ljh.user.pojo.User;
    import com.ljh.user.service.UserService;
    
    public class UserServiceImp implements UserService{
    
    	@Autowired
    	private UserMapper userMapperImp;
    	
    	public void reg(User user) {
    		userMapperImp.reg(user);
    	}
    
    	public List getAllUser() {
    		return userMapperImp.getAllUser();
    	}
    
    	public void updateUser(User user) {
    		userMapperImp.updateUser(user);
    	}
    
    	public User getUserById(User user) {
    		return userMapperImp.getUserById(user);
    	}
    
    	public void delUser(User user) {
    		userMapperImp.delUser(user);
    		//int i = 1/0;	//测试事务控制
    	}
    
    	public User login(User user) {
    		return userMapperImp.login(user);
    	}
    
    	//获取总页数
        public int pageCount(){
            return userMapperImp.pageCount();
        }
        //分页显示所有用户
        public List getUserByPage(int index){
            return userMapperImp.getUserByPage(index);
        }
    
    }
  5. 编写持久化层接口&UserMapper.xml(同上不记录接口了,以下是UserMapper.xml)
    
    
      
    	
    	  
      	
      		insert into userinfo(userName,userPwd,userEmail,userAge,userIdCardNo)
         	values
         	(#{userName},#{userPwd},#{userEmail},#{userAge},#{userIdCardNo})
      	
      	
      	
      	
      	
      	
      	
      	
      	
      	
      		update userinfo
      		
      			userName=#{userName},userPwd=#{userPwd},userEmail=#{userEmail},userAge=#{userAge},userIdCardNo=#{userIdCardNo}
      		
      		
      			userId=#{userId}
      		
      	
      	
      	
         
            delete from userinfo where userId=#{userId}
         
         
        
    	
      	 	
      	
      	
    	
       		
     		
      		
      		
      		
      		
    	
    	
    	
    	
    	
    	
    	
    
  6. 编写springmvc.xml文件
    
    
    	
    	
    	
    	
    	
    	
    		
    		
    	
    
  7. 配置applicationContext-mapper.xml
    
    
            
        
        
        	
        	
        	
        	
        
        
        
        
        	
        	
        	
        
        
        
        
    		
    	
    
  8. 配置applicationContext-service.xml:
    
    
            
    	        
    
  9. 配置applicationContext-transcation.xml:
    
    	
    	
    	
    	
    		
    	
    
    	
    	
    		
    			
    			
    			
    			
    			
    		
    	
    
    	
    		
    		
    		
    		
    	
    	
    
  10. 配置mybatis-config.xml文件
    
    
    
    	
    		
    	
    
  11. 配置web.xml
    
    
    	maven_ssm
    	
    		index.html
    		index.htm
    		index.jsp
    		default.html
    		default.htm
    		default.jsp
    	
    
        
    	
    		maven_ssm
    		org.springframework.web.servlet.DispatcherServlet
    	    
    	    contextConfigLocation
    	    classpath:spring/springmvc.xml
    	    
    	    1
    	
    
    	
    		maven_ssm
    		*.action
    	
    	
    	
    	
    		org.springframework.web.context.ContextLoaderListener
    	
    	
    	
    		contextConfigLocation
    		classpath*:spring/applicationContext-*.xml
    	
    
    
  12. 最后别忘了把log4j.properties放进去以及右键项目-->maven-->update project一下
  13. 再写出2个比较值得记录的jsp页面
    1. Login.jsp
      <%@ page language="java" contentType="text/html; charset=UTF-8"
      	pageEncoding="UTF-8"%>
      
      
      
      
      Insert title here
      
      
      	

      exam0723 登录页面

      Login ${fail }

      用户名:
      密码:
    2. getUserByPage.jsp(分页查询全部用户信息)
      <%@ page language="java" contentType="text/html; charset=UTF-8"
      	pageEncoding="UTF-8"%>
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      
      
      
      
      maven_ssm 分页查询所有用户
      
      
      

      欢迎 ${userLogined.userName}

      用户名 密码 邮箱 年龄 身份证号码 操作
      ${user.userName } ${user.userPwd } ${user.userEmail } ${user.userAge } ${user.userIdCardNo } 删除修改
      当前第${pageUtil.pageIndex}页/共${pageUtil.pageCount}页  共${pageUtil.pageNumber}条记录   首页  下一页   上一页   尾页
  14. 整体视图:
  15. 现在来跑一下
    1. 右键项目 --> Run As --> Maven Build
    2. 第一次运行报错别慌:update project一下再跑一次即可

你可能感兴趣的:(学习记录)