不一样的地方
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:p="http://www.springframework.org/schema/p"
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-3.0.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
">
<context:component-scan base-package="cn.et.day20170606">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 加载资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 定义 数据源 通过el表达式把值设置进去 加载资源文件 -->
<bean id="datasoure" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driverClassName}"></property>
<property name="username" value="${userAccount}"></property>
<property name="password" value="${password}"></property>
<property name="validationQuery" value="SELECT sysdate FROM DUAL"/>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasoure"></property>
<!-- 告诉映射文件位置 -->
<property name="mappingResources">
<array>
<value>cn/et/day20170606/entity/EmpEntity.hbm.xml</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<!-- 打印sql日志 -->
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<!-- 数据库的操作类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 声明事物管理器 数据库的事物控制类 用来控制事物 -->
<bean id="trancsaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--必须要加载这个属性 通过这个源把数据库的事物交给他处理 -->
<property name="dataSource" ref="datasoure"></property>
</bean>
<!-- 声明事物通知 -->
<tx:advice id="tran" transaction-manager="trancsaction">
<tx:attributes>
<!-- 告诉他拦截这个方法 -->
<tx:method name="insert*" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!-- *代表了除了 上面配置的方法都不使用事务 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 声明的事物通知方法 -->
<aop:config>
<!-- 配置一个切入点 -->
<aop:pointcut expression="execution(* cn.et.day20170606.service.*Service.*(..))" id="money"/>
<!-- 把通知和切入点引进来 -->
<aop:advisor advice-ref="tran" pointcut-ref="money"/>
</aop:config>
</beans>
springmvc.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:p="http://www.springframework.org/schema/p"
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-3.0.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
">
<!-- 配置扫描 -->
<context:component-scan base-package="cn.et.day20170606">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<!-- 配置json消息转换器-->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!--
设置响应的支持的响应类型
-->
<value>text/html</value>
<value>text/html;charset=utf-8</value>
<value>application/json;charset=utf8</value>
</list>
</property>
</bean>
<!-- 交给tomcat处理 才可以访问静态资源 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsps/" p:suffix=".jsp">
</bean>
--><!--
第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConvert"/>
</set>
</property>
</bean>
<!-- 第一步: 创建自定义日期转换规则 -->
<bean id="dateConvert" class="cn.**.utils.DateConvert"/>
</beans>
dao
@Repository
public class EmpJaxaDaoImpl implements EmpJaxaDao{
@Autowired
private HibernateTemplate hibernateTemplate;
/**
*查询数据库的数据方法
* @throws IOException
* @throws IOException
*/
public List<EmpEntity> queryEmp(String ename){
String sql="select empno,ename,job,mgr,to_char(hiredate,'yyyy-mm-dd') as hiredate,sal,comm from EmpEntity where nvl(ename,' ') like :ename";
Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
Query query=session.createQuery(sql);
query.setParameter("ename", "%"+ename+"%");
List<EmpEntity> list=query.list();
return list;
}
/**
*新增数据库的数据方法
* @throws IOException
*/
public void insertEmp(EmpEntity emp){
hibernateTemplate.save(emp);
}
/**
*更新数据库的数据方法
* @throws IOException
*/
public void updateEmp(EmpEntity emp){
hibernateTemplate.update(emp);
}
/**
* 删除数据库数据的方法
* @throws IOException
* @throws IOException
*/
public void deleteEmp(Integer empno){
EmpEntity emp=new EmpEntity();
emp.setEmpno(empno);
hibernateTemplate.delete(emp);
}
/**
*查询数据库的分页数据方法
*注意结束位置再前面 开始位置再后面
* @throws IOException
*/
public List<EmpEntity> queryPaginEmp(String ename, int start,
int end){
String sql="from EmpEntity where nvl(ename,' ') like :ename";
Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
Query query=session.createQuery(sql);
query.setParameter("ename", "%"+ename+"%");
//分页 开始的位置 从0开始
query.setFirstResult(start-1);
//查询的条数
query.setMaxResults(5);
List<EmpEntity> list=query.list();
return list;
}
/**
*查询数据库的总数方法
* @throws IOException
*/
public int queryTotalEmp(String ename){
String sql="select count(rowid) as CT from EmpEntity where nvl(ename,' ') like :ename";
Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
Query query=session.createQuery(sql);
query.setParameter("ename", "%"+ename+"%");
Long lon=(Long)query.uniqueResult();
int i=lon.intValue();
return i;
}
}
/**
* 数据库的数据类 (一个对象就是数据库的一行)
* @author Administrator
*
*/
public class EmpEntity {
private int empno;
private String ename;
private String job;
private String mgr;
private Date hiredate;
private String sal;
private String comm;
private String deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public String getComm() {
return comm;
}
public void setComm(String comm) {
this.comm = comm;
}
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.et.day20170606.entity.EmpEntity" table="EMP" schema="SCOTT">
<id name="empno" type="java.lang.Integer">
<column name="EMPNO" precision="4" scale="0" />
<generator class="native" />
</id>
<property name="ename" type="java.lang.String">
<column name="ENAME" length="10">
</column>
</property>
<property name="job" type="java.lang.String">
<column name="JOB" length="9">
</column>
</property>
<property name="mgr" type="java.lang.String">
<column name="MGR" precision="4" scale="0">
</column>
</property>
<property name="hiredate" type="java.util.Date">
<column name="HIREDATE" length="7">
</column>
</property>
<property name="sal" type="java.lang.String">
<column name="SAL" precision="7">
</column>
</property>
<property name="comm" type="java.lang.String">
<column name="COMM" precision="7">
</column>
</property>
<property name="deptno" type="java.lang.String">
<column name="DEPTNO" >
</column>
</property>
</class>
</hibernate-mapping>
这里是自己实现的string转date
import org.springframework.core.convert.converter.Converter;
/**
* 全局日期处理类
* Convert<T,S>
* 泛型T:代表客户端提交的参数 String
* 泛型S:通过convert转换的类型
*/
public class DateConvert implements Converter<String,Date>{
public Date convert(String stringDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var path="${pageContext.request.contextPath}/day0609";
//根据id获取对象
function $(id){
return document.getElementById(id);
}
//和div赋值
function toUpdate(empno,ename,job,mgr,hiredate,sal,comm,deptno){
$('updateDiv').style.display='block';
$('empNoId').value=empno;
$('updateEname').value=(ename=='null'?'':ename);
$('updateJob').value=(job=='null'?'':job);
$('updateMgr').value=(mgr=='null'?'':mgr);
$('updateHiredate').value=hiredate;
$('updateSal').value=(sal=='null'?'':sal);
$('updateComm').value=(comm=='null'?'':comm);
$('updateDeptno').value=(deptno=='null'?'':deptno);
}
/**
封装ajax
**/
function sendAjax(method,url,callback,param){
var xmlHttp=new XMLHttpRequest();
//向服务器发出请求
//规定请求的类型、URL 以及是否异步处理请求。
//method:请求的类型;GET 或 POST
//url:文件在服务器上的位置
//async:true(异步)或 false(同步)
xmlHttp.open(method,url,true);
//设置响应的回调 当后台的json返回后 自动调用xhr.onreadystatechange指定的函数
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
//成功后调用第三个函数
callback(xmlHttp.responseText);
}
}
//post 在send里面带参数 get直接带 post一定要设置setRequestHeader
if(method=="POST"){
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.send(param);
}else{
xmlHttp.send();
}
}
//和action交互的方法 用于更新
function toUpdate1(){
document.getElementById('updateDiv').style.display='none'
var empno=$('empNoId').value;
var ename=$('updateEname').value;
var job=$('updateJob').value;
var mgr=$('updateMgr').value;
var hiredate=$('updateHiredate').value;
var sal=$('updateSal').value;
var comm=$('updateComm').value;
var deptno=$('updateDeptno').value;
//参数
var param="ename="+ename+"&job="+job+"&mgr="+mgr+"&hiredate="+hiredate+"&sal="+sal+"&comm="+comm+"&deptno="+deptno+"&empno="+empno
//调用ajax好action交互
sendAjax("POST",path+"/updateEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//和action交互的方法 用于新增
function toAdd(){
document.getElementById('addDiv').style.display='none'
var xmlHttp=new XMLHttpRequest();
var ename=$('a1').value;
var job=$('a2').value;
var mgr=$('a3').value;
var hiredate=$('a4').value;
var sal=$('a5').value;
var comm=$('a6').value;
var deptno=$('a7').value;
var param="ename="+ename+"&job="+job+"&mgr="+mgr+"&hiredate="+hiredate+"&sal="+sal+"&comm="+comm+"&deptno="+deptno;
sendAjax("POST",path+"/insertEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//和action交互的方法 用于删除
function toDelete(empno){
var param="empno="+empno;
sendAjax("POST",path+"/deleteEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//分页的一些参数
var current=1;
var sum=0;
var trail=0;
var last=0;
var next=0;
和action交互的方法 用于查询
function toQuery(pagin){
var xmlHttp=new XMLHttpRequest();
var tb=$("tb");
var td=$("td");
var ename=$("ename").value;
xmlHttp.open("GET",path+"/queryPaginEmp?ename="+ename+"&pagin="+pagin,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//获取action响应的内容
var json=xmlHttp.responseText;
//把它转成json对象
var jsonall=JSON.parse(json);
//给分页的一些参数赋值
current=jsonall.current;
sum=jsonall.sum;
trail=jsonall.trail;
last=jsonall.last;
next=jsonall.next;
$("p1").innerHTML=current;
$("p2").innerHTML=trail;
$("p3").innerHTML=sum;
var jsonObj=jsonall.list;
//无刷新在此查询的时候把整个tbody删除
tb.removeChild(td);
var tdRow=document.createElement("tbody");
tdRow.setAttribute("id","td");
//把tbody加到table里面去
tb.appendChild(tdRow);
for ( var i = 0; i < jsonObj.length; i++) {
var el = jsonObj[i];
//创建节点
var trRow=document.createElement("tr");
var tdRow1=document.createElement("td");
var tdRow2=document.createElement("td");
var tdRow3=document.createElement("td");
var tdRow4=document.createElement("td");
var tdRow5=document.createElement("td");
var tdRow6=document.createElement("td");
var tdRow7=document.createElement("td");
var tdRow8=document.createElement("td");
var tdRow9=document.createElement("td");
tdRow1.innerHTML=el.empno;
tdRow2.innerHTML=el.ename;
tdRow3.innerText=el.job;
tdRow4.innerText=el.mgr;
var date=new Date();
var hiredate;
if(el.hiredate!=null){
date.setTime(el.hiredate);
hiredate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
}else{
hiredate="";
}
tdRow5.innerHTML=hiredate;
tdRow6.innerHTML=el.sal;
tdRow7.innerHTML=el.comm;
tdRow8.innerHTML=el.deptno;
tdRow9.innerHTML="<button οnclick='toDelete("+el.empno+")'>删除</button><button οnclick=toUpdate("+el.empno+",'"+el.ename+"','"+el.job+"','"+
el.mgr+"','"+hiredate+"','"+el.sal+"','"+el.comm+"','"+el.deptno+"')>更新</button>";
//把节点tdRow1加到trRow
trRow.appendChild(tdRow1);
trRow.appendChild(tdRow2);
trRow.appendChild(tdRow3);
trRow.appendChild(tdRow4);
trRow.appendChild(tdRow5);
trRow.appendChild(tdRow6);
trRow.appendChild(tdRow7);
trRow.appendChild(tdRow8);
trRow.appendChild(tdRow9);
tdRow.appendChild(trRow);
}
}
}
xmlHttp.send();
}
</script>
</head>
<body οnlοad="toQuery(1)">
<!-- http://localhost:8080/ssha/day20170609/emp.jsp -->
ename<input type="text" name="ename" id="ename"/>
<input type="button" value="查询" οnclick="toQuery(current)"/>
<input type="button" value="新增" οnclick="document.getElementById('addDiv').style.display='block'"/><br/>
<table id="tb">
<thead>
<tr>
<td>empno</td>
<td>ename</td>
<td>job</td>
<td>mgr</td>
<td>hiredate</td>
<td>sal</td>
<td>comm</td>
<td>deptno</td>
<td>操作</td>
</tr>
</thead >
<tbody id="td">
</tbody>
</table>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/18.gif" οnclick="toQuery(1)"/>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/20.gif" οnclick="toQuery(last)"/>
当前<span id="p1"></span>页 总<span id="p2"></span>页 总:<span id="p3"></span>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/16.gif" οnclick="toQuery(next)"/>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/14.gif" οnclick="toQuery(trail)"/>
<!-- 新增的div-->
<div id="addDiv" style="display:none;position: absolute;left: 65%;top: 14%;width: 300px;height:200px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
ENAME:<input type='text' name='ename' id='a1'>
<br/>
JOB:<input type='text' name='job' id='a2'>
<br/>
MGR:<input type='text' name='mgr' id='a3'>
<br/>
HIREDATE:<input type='text' name='hiredate' id='a4'>
<br/>
SAL:<input type='text' name='sal' id='a5'>
<br/>
COMM:<input type='text' name='comm' id='a6'>
<br/>
DEPTNO:<input type='text' name='deptno' id='a7'>
<br/>
<input type='button' value="新增" οnclick="toAdd()">
<input type='button' value="关闭" οnclick="document.getElementById('addDiv').style.display='none'">
</div>
<!-- 修改的div-->
<div id="updateDiv" style="display:none;position: absolute;left: 65%;top: 14%;width: 300px;height:200px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
<input type='hidden' name='empno' id='empNoId'>
ENAME: <input type='text' id="updateEname" name='ename'>
<br/>
JOB: <input type='text' id="updateJob" name='job'>
<br/>
MGR: <input type='text' id="updateMgr" name='mgr'>
<br/>
HIREDATE: <input type='text' id="updateHiredate" name='hiredate'>
<br/>
SAL: <input type='text' id="updateSal" name='sal'>
<br/>
COMM: <input type='text' id="updateComm" name='comm'>
<br/>
DEPTNO: <input type='text' id="updateDeptno" name='deptno'>
<br/>
<input type='button' value='修改' οnclick="toUpdate1()">
<input type='button' value="关闭" οnclick="document.getElementById('updateDiv').style.display='none'">
</div>
</body>
</html>