spring+springmvc+myBatis+jquery+ajax+jackson的增删改查

时间:2022-10-20 20:55:39

src

cn.et.emp


cn.et.emp.controller

AjaxAction

package cn.et.emp.controller;

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

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.emp.entity.Emp;
import cn.et.emp.service.EmpService;
import cn.et.emp.utils.PageEntity;

@Controller
public class AjaxAction {

@Autowired
private EmpService service;

/**
* ajax请求跳转是没有用的,所以返回字节数组
* 表示将json转换为字节输出给响应流
*
* @ResponseBody 这里修改了返回值,所以必须要加上这句代码
* 这里没有返回值,使用分页
* @return
* @throws IOException
*/
@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"));
}

@RequestMapping("json")
public void action(String ename,OutputStream os) throws UnsupportedEncodingException, IOException{
os.write("SMITH".toString().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;
}

}


cn.et.emp.entity

Emp

package cn.et.emp.controller;

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

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.emp.entity.Emp;
import cn.et.emp.service.EmpService;
import cn.et.emp.utils.PageEntity;

@Controller
public class AjaxAction {

@Autowired
private EmpService service;

/**
* ajax请求跳转是没有用的,所以返回字节数组
* 表示将json转换为字节输出给响应流
*
* @ResponseBody 这里修改了返回值,所以必须要加上这句代码
* 这里没有返回值,使用分页
* @return
* @throws IOException
*/
@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"));
}

@RequestMapping("json")
public void action(String ename,OutputStream os) throws UnsupportedEncodingException, IOException{
os.write("SMITH".toString().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;
}

}


cn.et.emp.mapper

EmpMapper

package cn.et.emp.controller;

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

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.emp.entity.Emp;
import cn.et.emp.service.EmpService;
import cn.et.emp.utils.PageEntity;

@Controller
public class AjaxAction {

@Autowired
private EmpService service;

/**
* ajax请求跳转是没有用的,所以返回字节数组
* 表示将json转换为字节输出给响应流
*
* @ResponseBody 这里修改了返回值,所以必须要加上这句代码
* 这里没有返回值,使用分页
* @return
* @throws IOException
*/
@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"));
}

@RequestMapping("json")
public void action(String ename,OutputStream os) throws UnsupportedEncodingException, IOException{
os.write("SMITH".toString().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;
}

}


EmpMapper.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">

<!--
接口映射
namespace必需跟接口的全名一致
-->
<mapper namespace="cn.et.emp.mapper.EmpMapper">
<!--
select的id必须和接口的方法名一致
resultType和接口返回值的泛型一致
没有返回值就不写

#{}语法格式是(实际上是使用jdbc的?点位符传参是安全的 )
第一种#{0}
第二种#{param1}
第三种指定别名#{}

${}是字符串拼接,拼接字符串的话要添加'' 是不安全的

查询总一共有多少符合条件的数据
-->
<select id="queryCount" resultType="java.util.Map">
select count(rowid) as TC from emp2 where ename like #{ename}
</select>

<!-- 通过用户名查询 -->
<select id="query" resultType="java.util.Map">
select * from emp2 where ename like #{ename} order by eno desc
</select>

<!-- 查询当前页的数据 -->
<select id="queryPage" resultType="java.util.Map">
select * from
(select e.*,rownum rn from (select * from emp2 where ename like #{ename} order by eno desc) e)
where rn between #{start} and #{end} order by eno desc
</select>

<!-- 新增 -->
<insert id="add">
insert into emp2 values((select nvl(max(eno),0)+1 from emp2),#{ename},#{sal})
</insert>

<!-- 删除 -->
<delete id="delete">
delete from emp2 where eno=#{eno}
</delete>

<!-- 修改 -->
<update id="update">
update emp2 set ename=#{ename},sal=#{sal} where eno=#{eno}
</update>
</mapper>


cn.et.emp.service

EmpService

<?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">

<!--
接口映射
namespace必需跟接口的全名一致
-->
<mapper namespace="cn.et.emp.mapper.EmpMapper">
<!--
select的id必须和接口的方法名一致
resultType和接口返回值的泛型一致
没有返回值就不写

#{}语法格式是(实际上是使用jdbc的?点位符传参是安全的 )
第一种#{0}
第二种#{param1}
第三种指定别名#{}

${}是字符串拼接,拼接字符串的话要添加'' 是不安全的

查询总一共有多少符合条件的数据
-->
<select id="queryCount" resultType="java.util.Map">
select count(rowid) as TC from emp2 where ename like #{ename}
</select>

<!-- 通过用户名查询 -->
<select id="query" resultType="java.util.Map">
select * from emp2 where ename like #{ename} order by eno desc
</select>

<!-- 查询当前页的数据 -->
<select id="queryPage" resultType="java.util.Map">
select * from
(select e.*,rownum rn from (select * from emp2 where ename like #{ename} order by eno desc) e)
where rn between #{start} and #{end} order by eno desc
</select>

<!-- 新增 -->
<insert id="add">
insert into emp2 values((select nvl(max(eno),0)+1 from emp2),#{ename},#{sal})
</insert>

<!-- 删除 -->
<delete id="delete">
delete from emp2 where eno=#{eno}
</delete>

<!-- 修改 -->
<update id="update">
update emp2 set ename=#{ename},sal=#{sal} where eno=#{eno}
</update>
</mapper>


cn.et.emp.service.impl

EmpServiceImpl

package cn.et.emp.service.impl;

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

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

import cn.et.emp.entity.Emp;
import cn.et.emp.mapper.EmpMapper;
import cn.et.emp.service.EmpService;
import cn.et.emp.utils.PageEntity;
import cn.et.emp.utils.PageUtils;


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

//自动装配
@Autowired
private EmpMapper mapper;


/* 查询
* @see cn.et.emp.dao.impl.EmpDao#query(java.lang.String)
*/
public PageEntity query(String ename,int curPage){
if(ename==null){
ename="";
}
//获取到总条数
Map map = mapper.queryCount("%"+ename+"%");
String total = map.get("TC").toString();
int intTotal = Integer.parseInt(total);
//再创建PageEntity实体类把当前页,每页显示多少条数据和总共多少条数据传进去
PageEntity pe = PageUtils.calc(curPage, 10, intTotal);
List list = mapper.queryPage("%"+ename+"%", pe.getStartIndex(), pe.getEndIndex());
pe.setData(list);
return pe;
}

/* 新增
* @see cn.et.emp.mapper.impl.EmpDao#add(java.lang.String, java.lang.String)
*/
public void add(String ename,String sal){
if(ename==null){
ename="";
}
mapper.add(ename, sal);
}

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

/* 修改
* @see cn.et.emp.mapper.impl.EmpDao#update(en.et.emp.entity.Emp)
*/
public void update(Emp emp){
mapper.update(emp.getEno(),emp.getEname(),emp.getSal());
}

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

}


cn.et.emp.utils

PageEntity

package cn.et.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<pageCount?curPage+1:curPage;
*/
private int nextPage;


/**
*
* startIndex=(curPage-1)*pageSize+1
* endIndex=curPage*pageSize
*/
private int startIndex;

private int endIndex;

private List data;

private String queryName;

public String getQueryName() {
return queryName;
}

public void setQueryName(String queryName) {
this.queryName = queryName;
}

public int getTotalSize() {
return totalSize;
}

public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getPageCount() {
return pageCount;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public int getCurPage() {
return curPage;
}

public void setCurPage(int curPage) {
this.curPage = curPage;
}

public int getPrevPage() {
return prevPage;
}

public void setPrevPage(int prevPage) {
this.prevPage = prevPage;
}

public int getNextPage() {
return nextPage;
}

public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}

public int getStartIndex() {
return startIndex;
}

public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}

public int getEndIndex() {
return endIndex;
}

public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
}

public List getData() {
return data;
}

public void setData(List data) {
this.data = data;
}



}


PageUtils

package cn.et.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<pageCount?curPage+1:curPage;
int startIndex=(curPage-1)*pageSize+1;
int endIndex=curPage*pageSize;

pe.setPageCount(pageCount);
pe.setPrevPage(prevPage);
pe.setNextPage(nextPage);
pe.setStartIndex(startIndex);
pe.setEndIndex(endIndex);
return pe;
}
}


jdbc.properties

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


log4j.properties

log4j.rootLogger=debug, ServerDailyRollingFile, stdout 
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=C\://logs/notify-subscription.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n


spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">

<!-- springmvc的配置只能扫描控制层 spring配置文件不能扫描控制层 -->
<context:component-scan base-package="cn.et">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 读取jdbc.properties文件 -->
<context:property-placeholder
location="classpath:/cn/et/emp/utils/jdbc.properties" />

<!-- 数据库连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driverClass}"></property>
<property name="username" value="${account}"></property>
<property name="password" value="${password}"></property>
<!--
initialSize 10
默认生成10个连接,那么要用到连接的时候就直接拿一条出来用就可以了,
就不用等到用的时候再去产生连接
-->
<property name="initialSize" value="10"></property>
<!-- 发起一条测试的sql语句去连接一下数据库,看是否可以正常连接数据库 -->
</bean>

<!-- 创建一个jdbc模板SqlSessionFactoryBean -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 这里一定要用sqlSessionFactoryBeanName,不然加载不了driverClass -->
<!-- 扫描接口映射和注解和xml文件 -->
<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
<!--
如果映射的是Mapper接口直接放到mapper包里面扫描就发好了
如果映射的是xml配置文件需要把配置文件名改成对应的接口一样的名称,并都要放到mapper包下
-->
<property name="basePackage" value="cn.*..*.mapper"></property>
</bean>

<!-- 事务管理器 spring帮助我们控制事务 -->
<bean id="transManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- aspectjweaver 事务架包 -->
<!-- 当切点拦截到某个操作的方法 发送通知给tx定义的通知管理 调用事务管理器 提交和回滚 -->
<tx:advice id="myAdvice" transaction-manager="transManager">
<tx:attributes>
<!-- 默认的配置 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="query*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!-- *代表了除了上面配置的方法都不使用事务 -->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 切面 -->
<aop:config>
<aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" />
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" />
</aop:config>
</beans>




form.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="jquery-2.2.4.js"></script>
<script type="text/javascript">

//当前页
var curPage=0;
//上一页
var prevPage=0;
//下一页
var nextPage=0;
//总页数
var pageCount=0;
//总条数
var totalSize=0;



//查询方法
function query(curP){
var ename=$("#qename").val();
//声明一个XMLHttpRequest变量并用兼容性的方法创建对象

$.ajax({
url:"${pageContext.request.contextPath}/ajaxQuery",//表示请求的路径
dataType:"json",// 表示响应的数据类型 json的方式传参
data:{ename:ename,curPage:curP},//ajax的传参
success:function(pe){
//读取pe里的页信息
curPage=pe.curPage;
prevPage=pe.prevPage;
nextPage=pe.nextPage;
pageCount=pe.pageCount;
totalSize=pe.totalSize;

$("#curPage").text(curPage);
$("#pageCount").text(pageCount);
$("#totalSize").text(totalSize);

//获取到list集合
var oJson = pe.data;

//首先获取取table的dom对象
$("tr[mark='delete']").remove();

for(var i=0; i<oJson.length; i++){
$("#tbody").append("<tr mark='delete'><td>"+oJson[i].ENO+"</td><td>"+oJson[i].ENAME+"</td><td>"+oJson[i].SAL
+"</td><td><button onclick=openUpdate("+oJson[i].ENO+",'"+oJson[i].ENAME+"',"+oJson[i].SAL
+")>修改</button><button onclick='del("+oJson[i].ENO+")'>删除</button></td>");
}

},
/*异常的处理*/
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("调用服务器失败--"+XMLHttpRequest.status+"--"+errorThrown);
}
});

}

//新增方法
function add(){
var url="${pageContext.request.contextPath}/ajaxAdd";
var param=$("#AddDivForm").serialize();
ajaxSend(url,param,function(text){
if(text==1){
//alert("新增成功");
$("#AddDivForm").find("input").val("");
$("#addDiv").hide();
query(curPage);
}else{
alert("新增失败");
}
});
}

//删除方法
function del(eno){
var url="${pageContext.request.contextPath}/ajaxDelete";
var param="eno="+eno;
ajaxSend(url,param,function(text){
if(text==1){
//alert("删除成功");
query(curPage);
}else{
alert("删除失败");
}
});
}

//打开修改DIV
function openUpdate(eno, ename, sal){
$('#updateDiv').show();
$('#upate_eno').val(eno);
$('#upate_ename').val(ename);
$('#upate_sal').val(sal);
}

//修改方法
function toUpdate(){
var url="${pageContext.request.contextPath}/ajaxUpdate";
var param=$("#updateDivForm").serialize();

ajaxSend(url,param,function(text){
if(text==1){
//alert("修改成功");
$('#updateDiv').hide();
$("#updateDivForm").find("input").val("");
query(curPage);
}else{
alert("修改失败");
}
});
}

//ajax请求
function ajaxSend(url,param,callback){
$.ajax({
url:url+"?",//表示请求的路径
data:param,
dataType:"json",// 表示响应的数据类型 json的方式传参
success:callback,
/*异常的处理*/
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("调用服务器失败--"+XMLHttpRequest.status+"--"+errorThrown);
}
});
}
</script>
</head>
<body onload="query(1)">
<form>
<input type="text" id="qename" />
<input type="button" value="查询" onclick="query(curPage)" />
<input type='button' value='新增' onclick='$("#addDiv").show()'>
</form>
<table border="1" cellpadding="10" cellspacing="0">
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
<td>薪水</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tbody">

</tbody>
</table>

<input type="image" src="${pageContext.request.contextPath}/task/image/1.gif" onclick="query(1)"/>
<input type="image" src="${pageContext.request.contextPath}/task/image/2.gif" onclick="query(prevPage)"/>
当前<span id="curPage"></span>页 共<span id="pageCount"></span>页 共<span id="totalSize"></span>条
<input type="image" src="${pageContext.request.contextPath}/task/image/3.gif" onclick="query(nextPage)"/>
<input type="image" src="${pageContext.request.contextPath}/task/image/4.gif" onclick="query(pageCount)"/>


<!-- 修改的div -->
<div id="updateDiv" style="display:none;position: absolute;left: 35%;top: 40%;width: 300px;height:100px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
<form id="updateDivForm">
<input id="upate_eno" type='hidden' />
ENAME: <input id="upate_ename" name="name" type='text'><Br/>
SAL:       <input id="upate_sal" name="sal" type='text' name='sal'><Br/>
</form>
<input type='button' value='保存' onclick=toUpdate() />
<input type='button' value="关闭" onclick="document.getElementById('updateDiv').style.display='none'" />

</div>

<!-- 新增的div -->
<div id="addDiv" style="display:none;position: absolute;left: 35%;top: 40%;width: 300px;height:100px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
<form id="AddDivForm">
ENAME:<input type='text' id='addEname' name="ename"><br/>
SAL:      <input type='text' id='addSal' name="sal"><br/>
</form>
<button onclick="add()">增加</button>
<input type='button' value="关闭" onclick="document.getElementById('addDiv').style.display='none'">
</div>
</body>
<script type="text/javascript">

</script>
</html>


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MybatisLesson</groupId>
<artifactId>MybatisLesson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name/>
<description/>
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>

<!-- 自动加载mybatis架包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>

<!--
加载本地ojdbc6.jar
前面三个可以乱填,最好是配上,不然可能会报错
scope 系统
scope路径
-->
<dependency>
<groupId>oracle</groupId>
<artifactId>orcale</artifactId>
<version>3.2.8</version>
<scope>system</scope>
<systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
</dependency>

<!-- 加载log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.3</version>
</dependency>

<!-- 加载springmvc架包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<!-- mybatis集成spring的核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>

<!-- 新数据库连接池架包dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<!-- aspectjweaver 事务架包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.weaver</artifactId>
<version>1.6.8.RELEASE</version>
</dependency>

<!-- jackson架包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

<!-- 加载json架包 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>

</dependencies>
<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>${basedir}/WebRoot</webappDirectory>
<warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<!-- 加载spring.xml配置文件 -->
<!-- spring 要使用springmvc的标签和国际化必须加载spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--
设置一个字符集的过滤器,这个过滤器要在其它过滤器之前过滤,
不然其它过滤器先拦截同样会出现中文乱码问题
-->
<filter>
<filter-name>utf</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>utf</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>myencode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<!-- servlet一般是不支持delete和put 所以要配置一个过滤器 这个项目没有用请求限定-->
<filter>
<filter-name>hidden</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hidden</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置action -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="#####"/>
</webServices>
</scripting>
</system.web.extensions>

</web-app>