SpringMVC之AJAX带分页的增删改查

架包支持:

SpringMVC之AJAX带分页的增删改查_第1张图片


form.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


  
	
  
  
	
编号 姓名 薪水 操作
当前页 共页 共





web.xml:




	
	
	
		contextConfigLocation
		classpath:/spring.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		utf
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		utf
		/*
	

 
  	myencode
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  

 
 
 	hidden
 	org.springframework.web.filter.HiddenHttpMethodFilter
 
 
 	hidden
 	/*
 

  
  
  	spring
  	org.springframework.web.servlet.DispatcherServlet 
    
    	contextConfigLocation
    	/WEB-INF/springmvc.xml
    
    0
  

  
  	spring
  	*.action
  	/
  

  
    index.jsp
  


图片素材:




springmvc.xml:



	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
	
	
	
	
	
		
		
		
			
			
				
					
						
						text/html;charset=UTF-8
						
						application/x-www-form-urlencoded
						application/json;charset=UTF-8
					
				
			
		
			
				
						
							
							text/html;charset=UTF-8
							
							application/x-www-form-urlencoded
										
				
			
		
	
	



spring.xml:



	
	
	
		
	

	
	
		
		
	

	
	
	
	
		
		
		
		
	
	
		
	






Action类:

package cn.et.springmvc.lesson05.emp.controller;

import java.io.IOException;
import java.io.OutputStream;

import net.sf.json.JSONObject;

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.ResponseBody;

import cn.et.springmvc.lesson05.emp.entity.Emp;
import cn.et.springmvc.lesson05.emp.service.EmpService;
import cn.et.springmvc.lesson05.emp.utils.PageEntity;

@Controller
public class AjaxAction {

	@Autowired
	private EmpService service;
	
	/**
	 * ajax请求跳转是没有用的,所以返回字节数组
	 * 		表示将json转换为字节输出给响应流
	 * 	
	 * 	@ResponseBody 这里修改了返回值,所以必须要加上这句代码
	 * 这里没有返回值,使用分页
	 * @return
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="/ajaxQuery")
	public void ajaxQuery(String ename, String curPage, OutputStream os) throws IOException{
		int intCurPage = Integer.parseInt(curPage);
		PageEntity pe = service.query(ename,intCurPage);
		//将数组、对象或者集合转换成字符串的json 再传给xhr对象
		//把list集合转换成json字符串
		String json = JSONObject.fromObject(pe).toString();
		os.write(json.getBytes("UTF-8"));
	}
	
	/**
	 * 新增
	 * 如果返回值为void,那么形参必须配上OutputStream os不然会报错的
	 * @param ename
	 * @param sal
	 * @param os
	 * @throws IOException 
	 */
	@RequestMapping(value="/ajaxAdd")
	public void ajaxAdd(String ename,String sal,OutputStream os) throws IOException{
		try {
			service.add(ename, sal);
			os.write("1".getBytes("UTF-8"));
		} catch (Exception e) {
			os.write("0".getBytes("UTF-8"));
		}
	}
	
	/**
	 * 删除
	 * 如果返回值为void,那么形参必须配上OutputStream os不然会报错的
	 * @throws IOException 
	 */
	@RequestMapping(value="/ajaxDelete")
	public void ajaxDelete(String eno,OutputStream os) throws IOException{
		try {
			service.delete(eno);
			os.write("1".getBytes("UTF-8"));
		} catch (Exception e) {
			os.write("0".getBytes("UTF-8"));
		}
	}
	

	/**
	 * 修改
	 * 如果返回值为void,那么形参必须配上OutputStream os不然会报错的
	 * @throws IOException 
	 */
	@RequestMapping(value="/ajaxUpdate")
	public void ajaxUpdate(Emp emp,OutputStream os) throws IOException{
		try {
			service.update(emp);
			os.write("1".getBytes("UTF-8"));
		} catch (Exception e) {
			os.write("0".getBytes("UTF-8"));
		}
	}
	
	/**
	 * 需要配置org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
	 * 	才可以直接返回其它对象
	 * @param ename
	 * @param curPage
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/ajaxQuery2")
	public PageEntity ajaxQuery(String ename, String curPage) throws IOException{
		int intCurPage = Integer.parseInt(curPage);
		PageEntity pe = service.query(ename,intCurPage);
		//将数组、对象或者集合转换成字符串的json 再传给xhr对象
		//把list集合转换成json字符串
		return pe;
	}
	
}


dao接口:

package cn.et.springmvc.lesson05.emp.dao;

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

import cn.et.springmvc.lesson05.emp.entity.Emp;


public interface EmpDao {
	
	/**
	 * 查询总一共有多少符合条件的数据
	 * @param ename
	 * @return
	 */
	public abstract int queryCount(String ename);

	/**
	 * 通过用户名查询
	 * @param ename
	 * @return
	 */
	public abstract List> query(String ename);
	
	/**
	 * 查询当前页的数据 
	 * @param ename
	 * @param start
	 * @param end
	 * @return
	 */
	public abstract List> queryPage(String ename,int start, int end);

	/**
	 * 新增
	 */
	public abstract void add(String ename, String sal);

	/**
	 * 删除
	 */
	public abstract void delete(String eno);

	/**
	 * 修改
	 */
	public abstract void update(Emp emp);

}



daoImpl实现类:

package cn.et.springmvc.lesson05.emp.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.et.springmvc.lesson05.emp.dao.EmpDao;
import cn.et.springmvc.lesson05.emp.entity.Emp;

@Repository
public class EmpDaoImpl implements EmpDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	

	/**
	 *  查询总条数
	 * @see cn.et.emp.dao.impl.EmpDao#query(java.lang.String)
	 */
	public int queryCount(String ename){
		if(ename==null){
			ename="";
		}
		String sql="select count(rowid) as TC from emp2 where  ename like '%"+ename+"%' ";
		Map map=jdbcTemplate.queryForMap(sql);
		return ((BigDecimal)map.get("TC")).intValue();
	}
	
	/**
	 *  查询
	 * @see cn.et.emp.dao.impl.EmpDao#query(java.lang.String)
	 */
	public List> query(String ename){
		List> list = null;
		if(ename == null || ename == ""){
			String sql = "select * from emp2";
			list = jdbcTemplate.queryForList(sql);
		}else{
			String sql = "select * from emp2 where ename like ?";
			list = jdbcTemplate.queryForList(sql,"%"+ename+"%");
		}
		return list;
	}
	
	/**
	 * 查询当前页的数据 
	 */
	public List> queryPage(String ename,int start, int end){
		List> list = null;
		if(ename == null || ename == ""){
			String sql = "select * from (select e.*,rownum rn from emp2 e)" +
			" where rn between "+start+" and "+end;
			list = jdbcTemplate.queryForList(sql);
		}else{
			String sql = "select * from (select e.*,rownum rn from emp2 e where ename like ?)" +
			" where rn between "+start+" and "+end;
			list = jdbcTemplate.queryForList(sql,"%"+ename+"%");
		}
		return list;
	}
	
	/** 
	 * 新增
	 * @see cn.et.emp.dao.impl.EmpDao#add(java.lang.String, java.lang.String)
	 */
	public void add(String ename,String sal){
		String sql = "insert into emp2 values((select nvl(max(eno),0)+1 from emp2),?,"+sal+")";
		jdbcTemplate.update(sql, ename);
	}
	
	/**
	 *  删除
	 * @see cn.et.emp.dao.impl.EmpDao#delete(java.lang.String)
	 */
	public void delete(String eno){
		String sql = "delete from emp2 where eno="+eno;
		jdbcTemplate.update(sql);
	}
	
	/**
	 *  修改
	 * @see cn.et.emp.dao.impl.EmpDao#update(en.et.emp.entity.Emp)
	 */
	public void update(Emp emp){
		String sql = "update emp2 set ename=?,sal="+emp.getSal()+" where eno="+emp.getEno();
		jdbcTemplate.update(sql, emp.getEname());
	}
}



service接口:

package cn.et.springmvc.lesson05.emp.service;

import cn.et.springmvc.lesson05.emp.entity.Emp;
import cn.et.springmvc.lesson05.emp.utils.PageEntity;


public interface EmpService {

	/* 查询
	 * @see cn.et.emp.dao.impl.EmpDao#query(java.lang.String)
	 */
	public abstract PageEntity query(String ename,int curPage);

	/* 新增
	 * @see cn.et.emp.dao.impl.EmpDao#add(java.lang.String, java.lang.String)
	 */
	public abstract void add(String ename, String sal);

	/* 删除
	 * @see cn.et.emp.dao.impl.EmpDao#delete(java.lang.String)
	 */
	public abstract void delete(String eno);

	/* 修改
	 * @see cn.et.emp.dao.impl.EmpDao#update(en.et.emp.entity.Emp)
	 */
	public abstract void update(Emp emp);

}



serviceImpl实现类:

package cn.et.springmvc.lesson05.emp.service.impl;

import java.util.List;

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

import cn.et.springmvc.lesson05.emp.dao.EmpDao;
import cn.et.springmvc.lesson05.emp.entity.Emp;
import cn.et.springmvc.lesson05.emp.service.EmpService;
import cn.et.springmvc.lesson05.emp.utils.PageEntity;
import cn.et.springmvc.lesson05.emp.utils.PageUtils;


/**
 * 服务层掉用dao层的方法 
 * @author Administrator
 *
 */
@Service
public class EmpServiceImpl implements EmpService {

	//自动装配
	@Autowired
	private EmpDao dao;
	
	
	/* 查询
	 * @see cn.et.emp.dao.impl.EmpDao#query(java.lang.String)
	 */
	public PageEntity query(String ename,int curPage){
		//获取到总条数
		int total = dao.queryCount(ename);
		//再创建PageEntity实体类把当前页,每页显示多少条数据和总共多少条数据传进去
		PageEntity pe = PageUtils.calc(curPage, 10, total);
		List list = dao.queryPage(ename, pe.getStartIndex(), pe.getEndIndex());
		pe.setData(list);
		return pe;
	}
	
	/* 新增
	 * @see cn.et.emp.dao.impl.EmpDao#add(java.lang.String, java.lang.String)
	 */
	public void add(String ename,String sal){
		dao.add(ename, sal);
	}
	
	/* 删除
	 * @see cn.et.emp.dao.impl.EmpDao#delete(java.lang.String)
	 */
	public void delete(String eno){
		dao.delete(eno);
	}
	
	/* 修改
	 * @see cn.et.emp.dao.impl.EmpDao#update(en.et.emp.entity.Emp)
	 */
	public void update(Emp emp){
		dao.update(emp);
	}

	public PageEntity query(String ename) {
		// TODO Auto-generated method stub
		return null;
	}
	
}



utils工具包---PageEntity:

package cn.et.springmvc.lesson05.emp.utils;

import java.util.List;

public class PageEntity {
	
	/**
	 * 数据库查询的总条数
	 */
	private int totalSize;
	
	/**
	 * 每页显示的条数
	 */
	private int pageSize=10;
	
	/**
	 * 总页数
	 * 
	 * totalSize=20 pageSize=10
	 * 最后公式:pageCount=(totalSize%pageSize==0?totalSize/pageSize:(totalSize/pageSize+1))
	 */
	private int pageCount;
	
	/**
	 * 当前页
	 */
	private int curPage;
	
	/**
	 * 上一页
	 * prevPage=curPage==1?curPage:curPage-1
	 */
	private int prevPage;
	
	/**
	 * 下一页
	 * 最后公式:nextPage=curPage



utils工具包---PageUtils:

package cn.et.springmvc.lesson05.emp.utils;

public class PageUtils {

	/**
	 * 计算上一页、下一页等...
	 * @param curPage	当前页
	 * @param pageSize	每页显示的条数
	 * @param totalSize	数据库查询的总条数
	 * @return
	 */
	 public static PageEntity calc(int curPage, int pageSize, int totalSize){
		 PageEntity pe = new PageEntity();
		 pe.setCurPage(curPage);
		 pe.setPageSize(pageSize);
		 pe.setTotalSize(totalSize);
		 int pageCount=(totalSize%pageSize==0?totalSize/pageSize:(totalSize/pageSize+1));
		 int prevPage=curPage==1?curPage:curPage-1;
		 int nextPage=curPage



Entity实体类:

package cn.et.springmvc.lesson05.emp.entity;

public class Emp {

	private String eno;
	
	private String ename;
	
	private String sal;

	public Emp(){}
	
	public Emp(String eno, String ename, String sal) {
		super();
		this.eno = eno;
		this.ename = ename;
		this.sal = sal;
	}

	public String getEno() {
		return eno;
	}

	public void setEno(String eno) {
		this.eno = eno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getSal() {
		return sal;
	}

	public void setSal(String sal) {
		this.sal = sal;
	}
	
	
}


jdbc.properties:


url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
driverClass=oracle.jdbc.OracleDriver
account=scott
password=tiger









你可能感兴趣的:(经典项目)