Spring+MyBatis+Ajax 实现无刷新分页

1.导入相关jar:放在文章尾部

2.环境搭建,编写相关配置文件:

web.xml:



  AjaxPage
  
    index.jsp
  
  
  
  
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  
  
  	org.springframework.web.context.ContextLoaderListener
  

	

applicationContext.xml:



        
        
        
        
        
        
        
        
        
        	
	    	
	    	
	    	
        
        
        
        
			
        
        
        
        
        	
        	
        	
        
        
        
        
        
         
         
			
				
				
				
				
			
         
		
		  
		 
		 	
		 	
		 
		 
		 
        
        

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm
jdbc.username = root
jdbc.password = root

实体类:

package com.tao.pojo;

public class Dept {
	
	private int id;
	private String name;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package com.tao.pojo;

public class Emp {
	
	private int id;
	private String empno;
	private String name;
	private int deptid;
	private Dept dept;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getEmpno() {
		return empno;
	}
	public void setEmpno(String empno) {
		this.empno = empno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getDeptid() {
		return deptid;
	}
	public void setDeptid(int deptid) {
		this.deptid = deptid;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
}
package com.tao.pojo;

import java.util.List;

public class PageInfo {
	
	private int pageSize;
	private int pageNumber;
	private long total;
	private List list;
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public long getTotal() {
		return total;
	}
	public void setTotal(long total) {
		this.total = total;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	
}

数据层接口及映射文件:

package com.tao.mapper;

import java.util.List;

import com.tao.pojo.Emp;

public interface EmpMapper {
	
	//查询员工及其部门
	List selByPage(int pageStart,int pageSize);

	//查询员工总记录数
	long selCount();
	
}



	
	
		
		
		
		
		
	
	
	
		
		
	

	
	
	


关于 association 的使用,也有其他的写法 自己弄懂 会写就行

Spring+MyBatis+Ajax 实现无刷新分页_第1张图片

业务层及其实现类:

package com.tao.service;

import com.tao.pojo.PageInfo;

public interface EmpService {
	
	PageInfo show(int pageSize,int pageNumber);

}
package com.tao.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tao.mapper.EmpMapper;
import com.tao.pojo.PageInfo;
import com.tao.service.EmpService;

@Service
public class EmpServiceImpl implements EmpService{

	@Resource
	private EmpMapper empMapper;
	
	@Override
	public PageInfo show(int pageSize, int pageNumber) {
		PageInfo p = new PageInfo();
		p.setPageSize(pageSize);
		p.setPageNumber(pageNumber);
		p.setList(empMapper.selByPage(pageSize*(pageNumber-1), pageSize));
		long count = empMapper.selCount();
		p.setTotal(count%pageSize==0?count/pageSize:count/pageSize+1);
		return p;
	}

}

控制层:

package com.tao.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tao.pojo.PageInfo;
import com.tao.service.EmpService;
import com.tao.service.impl.EmpServiceImpl;

@WebServlet("/show")
public class ShowServlet extends HttpServlet{
	
	private EmpService empService;
	
	private static final long serialVersionUID = 1L;

	
	@Override
	public void init() throws ServletException {
		//spring在web项目中容器是webApplicationContext
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		empService = wac.getBean("empServiceImpl",EmpServiceImpl.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
		
		PageInfo p = empService.show(pageSize, pageNumber);
		PrintWriter out = resp.getWriter();
		
		ObjectMapper objectMapper = new ObjectMapper();
		out.println(objectMapper.writeValueAsString(p));
		out.flush();
		out.close();
	}
	
	
}

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>






Insert title here


员工编号 姓名 所在部门
上一页 下一页

相关jar:

Spring+MyBatis+Ajax 实现无刷新分页_第2张图片

你可能感兴趣的:(MyBatis,Spring)