Spring+hibernate+Ajax(dwr)增加、删除、修改、查询

1.emp.hbm.xml:映射文件

<?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="com.emp.hib.entity.Emp" table="emp" schema="dbo" catalog="emp">
        <id name="tid" type="java.lang.Long">
            <column name="tid" precision="18" scale="0" />
            <generator class="assigned" />
        </id>
       
        <property name="tmc" type="java.lang.String">
            <column name="tmc" length="50" not-null="true" unique="true" />
        </property>
        <property name="tsj" type="java.sql.Date">
            <column name="tsj" length="23" not-null="true" />
        </property>
        <property name="tnr" type="java.lang.String">
            <column name="tnr" length="8000" />
        </property>
        <property name="tgx" type="java.lang.Integer">
            <column name="tgx" not-null="true" />
        </property>
      
    </class>
</hibernate-mapping>


2.emp.java

package com.emp.hib.entity;

import java.sql.Date;

/**
* Emp entity.
*
* @author MyEclipse Persistence Tools
*/

public class Emp implements java.io.Serializable {

// Fields

private Long tid;
private String tmc;
private Date tsj;
private String tnr;
private Integer tgx;


public Long getTid() {
   return this.tid;
}

public void setTid(Long tid) {
   this.tid = tid;
}

public String getTmc() {
   return this.tmc;
}

public void setTmc(String tmc) {
   this.tmc = tmc;
}

public Date getTsj() {
   return this.tsj;
}

public void setTsj(Date tsj) {
   this.tsj = tsj;
}

public String getTnr() {
   return this.tnr;
}

public void setTnr(String tnr) {
   this.tnr = tnr;
}

public Integer getTgx() {
   return this.tgx;
}

public void setTgx(Integer tgx) {
   this.tgx = tgx;
}

}

3.接口IEmpService :

package com.emp.web.service;

import java.util.List;

import com.emp.hib.entity.Emp;


public interface IEmpService {

//添加
public void addEmp(Emp emp);

//修改
public void updateEmp(Emp emp);

//根据Id得到员工
public Emp getEmp(Long id);

//删除指定Id的员工
public void delEmp(Long id);

//得到所有员工
public List<Emp> getEmps(String hql);


}
4.实现类EmpService

package com.emp.web.service;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.emp.hib.entity.Emp;

public class EmpService extends HibernateDaoSupport implements IEmpService {

// 添加

public void addEmp(Emp emp) {
   try {

    super.getHibernateTemplate().save(emp);

   } catch (Exception ex) {
    ex.printStackTrace();
   }
}

// 修改
public void updateEmp(Emp emp) {
   try {
    super.getHibernateTemplate().update(emp);
   } catch (Exception ex) {
    ex.printStackTrace();
   }
}

// 根据Id得到员工
public Emp getEmp(Long id) {
   try {
    return (Emp) super.getHibernateTemplate().get(Emp.class, id);

   } catch (Exception ex) {
    ex.printStackTrace();
    return null;
   }
}

// 删除指定Id的员工
public void delEmp(Long id) {
   try {
    super.getHibernateTemplate().delete(this.getEmp(id));
   } catch (Exception ex) {
    ex.printStackTrace();
   }
}

// 得到所有员工
public List<Emp> getEmps(String hql) {
  
   try {
    return super.getHibernateTemplate().find(hql);
   } catch (Exception ex) {
    ex.printStackTrace();
    return null;
   }
}

}

5.dwr调用类

package com.emp.web.dwr;

import java.util.List;
import com.emp.web.service.IEmpService;
import com.emp.hib.entity.Emp;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

public class EmpDwr {

private IEmpService service;

public void addEmp(Emp emp) {
   this.getService().addEmp(emp);
}

public Emp getEmp(Long tid)
{
  
   return this.getService().getEmp(tid);
}
public List<Emp> findEmp(String keyword,String condition)
    {
   
   
    String hql="From Emp where 1=1 ";
       if(condition.equals("等于"))
       {
        hql+=" and tmc ='"+keyword+"'";
       }
       else if(condition.equals("包含"))
       {
          hql+=" and tmc like '%"+keyword+"%'";
       }
    return this.getService().getEmps(hql);
    }
   
    public String getPar()
    {
    WebContext con = WebContextFactory.get();
    System.out.print(con.getHttpServletRequest().getParameter("id"));
    return con.getHttpServletRequest().getParameter("id");
   
    }
   
    public void updateEmp(Emp emp)
    {
   
    this.getService().updateEmp(emp);
   
    }
   
    public void delEmp(Long id)
    {
   
    this.getService().delEmp(id);
    }

   
   
public IEmpService getService() {
   return service;
}

public void setService(IEmpService service) {
   this.service = service;
}

}

6.spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="
http://www.springframework.org/schema/beans"
xmlns:dwr="
http://www.directwebremoting.org/schema/spring-dwr"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">


<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName"
    value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
   </property>
   <property name="url"
    value="jdbc:sqlserver://localhost:1433;databaseName=emp">
   </property>
   <property name="username" value="ss"></property>
   <property name="password" value="ssa"></property>
</bean>
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
    <ref bean="dataSource" />
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">
      org.hibernate.dialect.SQLServerDialect
     </prop>
     <prop key="hibernate.show_sql">true</prop>
    </props>
   </property>
   <property name="mappingResources">
    <list>
     <value>com/emp/hib/entity/Emp.hbm.xml</value></list>
   </property>
   </bean>
  
     <!-- service -->
   <bean id="service" class="com.emp.web.service.EmpService">
     <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
  
     <!-- dwr -->
     <bean id="empDwr" class="com.emp.web.dwr.EmpDwr">
       
    <property name="service" ref="service"/>

     </bean>
  
  
   </beans>

7.dwr配置文件 dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<dwr>

<allow>
   <create creator="spring" javascript="OpDwr">
     <param name="beanName" value="empDwr"/>
     <param name="location" value="classpath:applicationContext.xml"/>
   </create>
   <convert converter="bean" match="com.emp.hib.entity.Emp"/>
</allow>
</dwr>

9.显示页面 display.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">

   <title>My JSP 'display.jsp' starting page</title>

</head>
<script type='text/javascript' src='/EmpTest/dwr/interface/OpDwr.js'></script>
<script type='text/javascript' src='/EmpTest/dwr/engine.js'></script>
<script type='text/javascript' src='/EmpTest/dwr/util.js'></script>
<script>
   
   
   
   
    /* 查找 */
     function search()
     {
       
        OpDwr.findEmp($("keyword").value,$("condition").value,display);
     }
    
     /*显示*/
     function display(items)
     {
    
        alert(items.length);
        /*for(var i=0;i<items.length;i++)
        {
          alert(items[i].tmc);
        }*/
         dwr.util.removeAllRows("rst");
         var tdata=[function (emp){return emp.tid;},
         function (emp){return emp.tmc;},
         function (emp){return emp.tsj;},
         function (emp){return emp.tnr;},
         function (emp){return emp.tgx;},
         function (emp){
         var btnDel = document.createElement
        ("<input type='button' value='删除' οnclick=' del("+ emp.tid +")'>");
        return btnDel; },
        function (emp){
         var btnEdit = document.createElement
        ("<input type='button' value='修改' οnclick=' edit("+ emp.tid +")'>");
        return btnEdit; }
        ];
       
       
     
        dwr.util.addRows("rst", items,tdata);
      
        dwr.util.addOptions("opts",items,"tid","tmc");
     }
     /*删除*/
     function del(tid)
     {
        OpDwr.delEmp(tid);
        search();
     }
     /*更新显示*/
     function edit(tid)
     {
        $("updateDiv").style.display="block";
      
    
        OpDwr.getEmp(tid,upShow);
     }
     /*更新层显示数据*/
     function upShow(data)
     {
        
         var emp = {tid:data.tid,tmc:data.tmc,tsj:data.tsj,
         tnr:data.tnr,tgx:data.tgx};
         dwr.util.setValues(emp);
        
     }
     /*更新*/
     function upEmp()
     {
    
         var empp={tid:$("tid").value,tmc:$("tmc").value,tsj:$("tsj").value,
         tnr:$("tnr").value,tgx:$("tgx").value};
        
         OpDwr.updateEmp(empp);
        
        $("updateDiv").style.display="none";
         search();
     }
     /*显示查询条件*/
     function showCondition(conName)
     {
      
        $("condition").length=0;
       
        if(conName !="")
        {
          var conditions =["请选择","等于","包含"];
          dwr.util.addOptions("condition",conditions);
        }
        else
        {
         
          dwr.util.addOptions("condition",["请选择"]);
        }
     }
    

</script>
<body>
   <form>
    <div style="border: 1px solid green; width: 80%;">
    
     <select id="conName" οnchange="showCondition(this.value)">
     <option value="">请选择</option>
     <option value="用户名">用户名</option>
     <option value="姓名">姓名</option>
     </select>
     &nbsp;
    
    
     <select id="condition" >
      <option value="">请选择</option>
     </select>
     &nbsp;
     <input type="text" id="keyword">
     <input type="button" value="search" οnclick="search()">
    </div>
    <br />

    <div style="border: 1px solid green; width: 80%;">
     <select id="opts">
     </select>
     <br />
     <table id="ta">

      <thead>
       <tr>
        <th>
         tid
        </th>
        <th>
         tmc
        </th>
        <th>
         tsj
        </th>
        <th>
         tnr
        </th>
        <th>
         tgx
        </th>
        <th></th>
       </tr>
      </thead>
      <tbody id="rst">

      </tbody>

     </table>
    </div>

    <div id="updateDiv" style="display:none;">
     <table>
      <tr>
       <td>
        tid
       </td>
       <td>
        <input type="text" id="tid">
       </td>
      </tr>

      <tr>
       <td>
        tmc
       </td>
       <td>
        <input type="text" id="tmc">
       </td>
      </tr>

      <tr>
       <td>
        tsj(时间)
       </td>
       <td>
        <input type="text" id="tsj">
       </td>
      </tr>


      <tr>
       <td>
        tnr(内容)
       </td>
       <td>
        <input type="text" id="tnr">
       </td>
      </tr>

      <tr>
       <td>
        tgx(是否更新)
       </td>
       <td>
        <input type="text" id="tgx">
       </td>
      </tr>

      <tr>
       <td>

       </td>
       <td><input type="button" value="提交" οnclick="upEmp()">
       </td>
      </tr>
     </table>
    </div>

   </form>
</body>
</html>

10.添加页面 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
   
    <title>添加</title>


</head>

<script type='text/javascript' src='/EmpTest/dwr/interface/OpDwr.js'></script>
<script type='text/javascript' src='/EmpTest/dwr/engine.js'></script>

<script type='text/javascript' src='/EmpTest/dwr/util.js'></script>


<script>

     function show()
     {
       var emp = {tid:"",tmc:"",tsj:"",tnr:"",tgx:""};
       dwr.util.setValues(emp);
     }
     function showResult()
     {

      var empp={tid:$("tid").value,tmc:$("tmc").value,tsj:$("tsj").value,
      tnr:$("tnr").value,tgx:$("tgx").value};
   
     /* alert(dwr.util.getValues(document.forms[0]));*/
      OpDwr.addEmp(empp);
      window.location.href="display.jsp";
     }
</script>
<body οnlοad="show()">
    <form >
    <table>
    <tr>
     <td>
     tid
     </td>
     <td>
     <input type="text" id="tid">
     </td>
    </tr>
   
    <tr>
     <td>
     tmc
     </td>
     <td>
     <input type="text" id="tmc">
     </td>
    </tr>
   
    <tr>
     <td>
     tsj(时间)
     </td>
     <td>
     <input type="text" id="tsj">
     </td>
    </tr>
   

    <tr>
     <td>
     tnr(内容)
     </td>
     <td>
     <input type="text" id="tnr">
     </td>
    </tr>
   
    <tr>
     <td>
     tgx(是否更新)
     </td>
     <td>
     <input type="text" id="tgx">
     </td>
    </tr>
   
      <tr>
     <td>
   
     </td>
     <td>
     <input type="button" value="提交" οnclick="showResult()">
     </td>
    </tr>
    </table>
    </form>
</body>
</html>
11.web 配置文件 web.xml:

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

<!-- filter -->
<filter>
    <filter-name>EmpFilter</filter-name>
    <filter-class>com.emp.filter.EmpFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>EmpFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- dwr -->
<servlet>
    <servlet-name>dwr</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dwr</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
12.过滤器:EmpFilter

package com.emp.filter;

import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EmpFilter implements Filter {

private FilterConfig config ;

public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
   // TODO Auto-generated method stub
       try
       {
        request.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
        chain.doFilter(request, response);
       
       }catch(Exception ex)
       {
        ex.printStackTrace();
       }
}

public void init(FilterConfig filterConfig) throws ServletException {
   // TODO Auto-generated method stub
       this.config = filterConfig;
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
  
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值