[java]代码库
package com.hpsvse.ssh.dao;
import java.util.List;
import com.hpsvse.ssh.entity.Dept;
public interface IDeptDAO {
public List<Dept> queryAll();
}
package com.hpsvse.ssh.dao;
import java.util.List;
import com.hpsvse.ssh.entity.Emp;
/**
* 定义对Emp的全部可能的操作,不考虑业务
* @author Administrator
*
*/
public interface IEmpDAO {
/**
* 添加员工
* @param emp
*/
public void save(Emp emp);
/**
* 删除员工
* @param emp
*/
public void delete(Emp emp);
/**
* 修改员工
* @param emp
*/
public void update(Emp emp);
/**
* 根据ID查询员工
* @param emp
* @return
*/
public Emp queryById(Integer empno);
/**
* 查询全部员工
* @return
*/
public List<Emp> queryAll();
}
package com.hpsvse.ssh.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hpsvse.ssh.dao.IDeptDAO;
import com.hpsvse.ssh.entity.Dept;
public class DeptDAOImpl extends HibernateDaoSupport implements IDeptDAO{
public List<Dept> queryAll() {
return this.getHibernateTemplate().find("FROM Dept");
}
}
package com.hpsvse.ssh.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hpsvse.ssh.dao.IEmpDAO;
import com.hpsvse.ssh.entity.Emp;
//继承HibernateDaoSupport可以为我们提供sessionFactory,获得session。
//同时spring提供了HibernateTemplate帮助我们简化hibernate访问。
public class EmpDAOImpl extends HibernateDaoSupport implements IEmpDAO{
public void delete(Emp emp) {
this.getHibernateTemplate().delete(this.queryById(emp.getEmpno()));
}
public List<Emp> queryAll() {
return this.getHibernateTemplate().find("FROM Emp");
}
public Emp queryById(Integer empno) {
return this.getHibernateTemplate().get(Emp.class, empno);
}
public void save(Emp emp) {
this.getHibernateTemplate().save(emp);
}
public void update(Emp emp) {
this.getHibernateTemplate().merge(emp);
}
}
package com.hpsvse.ssh.service;
import java.util.List;
import com.hpsvse.ssh.entity.Dept;
public interface IDeptService {
public List<Dept> queryAll();
}
package com.hpsvse.ssh.service;
import java.util.List;
import com.hpsvse.ssh.entity.Emp;
/**
* 封闭emp的功能
* @author Administrator
*
*/
public interface IEmpService {
public List<Emp> queryAllEmps();
public void add(Emp emp);
public void update(Emp emp);
public void del(Emp emp);
}
package com.hpsvse.ssh.service.impl;
import java.util.List;
import com.hpsvse.ssh.dao.IDeptDAO;
import com.hpsvse.ssh.entity.Dept;
import com.hpsvse.ssh.service.IDeptService;
public class DeptServiceImpl implements IDeptService{
private IDeptDAO deptDao;
public void setDeptDao(IDeptDAO deptDao) {
this.deptDao = deptDao;
}
public List<Dept> queryAll() {
return deptDao.queryAll();
}
}
package com.hpsvse.ssh.service.impl;
import java.util.List;
import com.hpsvse.ssh.dao.IEmpDAO;
import com.hpsvse.ssh.entity.Emp;
import com.hpsvse.ssh.service.IEmpService;
public class EmpSerivceImpl implements IEmpService{
private IEmpDAO empDao;
public void setEmpDao(IEmpDAO empDao) {
this.empDao = empDao;
}
public List<Emp> queryAllEmps() {
return empDao.queryAll();
}
public void add(Emp emp) {
empDao.save(emp);
}
public void update(Emp emp) {
//empDao.queryById(emp.getEmpno());
empDao.update(emp);
}
public void del(Emp emp) {
empDao.delete(emp);
}
}
package com.hpsvse.ssh.web.action;
import java.util.List;
import com.hpsvse.ssh.entity.Dept;
import com.hpsvse.ssh.service.IDeptService;
public class DeptAction {
private List<Dept> depts;
private IDeptService deptSerivce;
public void setDeptSerivce(IDeptService deptSerivce) {
this.deptSerivce = deptSerivce;
}
public List<Dept> getDepts() {
return depts;
}
public void setDepts(List<Dept> depts) {
this.depts = depts;
}
public String queryAllDeptAjax(){
depts = deptSerivce.queryAll();
return "toalldeptajax";
}
}
package com.hpsvse.ssh.web.action;
import java.util.List;
import com.hpsvse.ssh.entity.Emp;
import com.hpsvse.ssh.service.IEmpService;
public class EmpAction {
private Emp emp;
private IEmpService empSerivce;
//注入service
public void setEmpSerivce(IEmpService empSerivce) {
this.empSerivce = empSerivce;
}
private List<Emp> emps;
public String queryall(){
emps = empSerivce.queryAllEmps();
return "success";
}
public String addEmpAjax(){
empSerivce.add(emp);
return null;
}
public String updateEmpAjax(){
empSerivce.update(emp);
return null;
}
public String delEmpAjax(){
empSerivce.del(emp);
return null;
}
public String queryallAjax(){
emps = empSerivce.queryAllEmps();
return "toqueryallajax";
}
public List<Emp> getEmps() {
return emps;
}
public void setEmps(List<Emp> emps) {
this.emps = emps;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
}
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name ="*"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="service" expression="execution(public * com.hpsvse.ssh.service.impl..*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>
<bean id="ds"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@10.10.24.210:1521:orcl">
</property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="ds" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/hpsvse/ssh/entity/Emp.hbm.xml</value>
<value>com/hpsvse/ssh/entity/Dept.hbm.xml</value>
</list>
</property>
</bean>
<bean id="empdao" class="com.hpsvse.ssh.dao.impl.EmpDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="deptdao" class="com.hpsvse.ssh.dao.impl.DeptDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="empservice" class="com.hpsvse.ssh.service.impl.EmpSerivceImpl">
<property name="empDao">
<ref local="empdao"/>
</property>
</bean>
<bean id="deptservice" class="com.hpsvse.ssh.service.impl.DeptServiceImpl">
<property name="deptDao">
<ref local="deptdao"/>
</property>
</bean>
<bean id="deptaction" class="com.hpsvse.ssh.web.action.DeptAction">
<property name="deptSerivce">
<ref local="deptservice"/>
</property>
</bean>
<bean id="empaction" class="com.hpsvse.ssh.web.action.EmpAction">
<property name="empSerivce">
<ref local="empservice"/>
</property>
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="emp" namespace="/emp" extends="json-default">
<action name="*" class="empaction" method="{1}">
<result>
/list.jsp
</result>
<result name="toqueryallajax" type="json">
<param name="root">emps</param>
</result>
</action>
</package>
<package name="dept" namespace="/dept" extends="json-default">
<action name="*" class="deptaction" method="{1}">
<result name="toalldeptajax" type="json">
<param name="root">depts</param>
</result>
</action>
</package>
</struts>
all.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'all.jsp' starting page</title>
<style type="text/css">
.high{
background-color: gray;
}
</style>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
//加载全部员工
loadEmps();
selectEmp();
loadDept();
$("#btnAdd").click(function(){
var param = $("#mform").serialize();
$.ajax({
url:"${pageContext.request.contextPath}/emp/addEmpAjax",
type:"post",
data:param,
success:function(d){
loadEmps();
}
})
});
$("#btnUpdate").click(function(){
var param = $("#mform").serialize();
$.ajax({
url:"${pageContext.request.contextPath}/emp/updateEmpAjax",
type:"post",
data:param,
success:function(d){
loadEmps();
}
})
});
$("#btnDel").click(function(){
$.ajax({
url:"${pageContext.request.contextPath}/emp/delEmpAjax",
type:"post",
data:{'emp.empno' : $("#empno").val()},
success:function(d){
loadEmps();
}
})
});
})
function loadDept(){
var deptSelect = $("#dept");
$.ajax({
url :"${pageContext.request.contextPath}/dept/queryAllDeptAjax",
dataType:"json",
type:"post",
success:function(data){
deptSelect.html("");
$.each(data, function (i,d){
var opElement = $("<option></option>");
opElement.val(d.deptno);
opElement.text(d.dname);
deptSelect.append(opElement);
})
}
});
}
function selectEmp(){
$("#tb>tr").live("click",function(){
$(this).addClass("high").siblings().removeClass("high");
var ename = $(this).children("td").eq(1).text();
var job = $(this).children("td").eq(2).text();
var sal = $(this).children("td").eq(3).text();
var dept = $(this).children("td").eq(4).attr("deptno");
var empno= $(this).children("td").eq(1).attr("empno");;
$("#ename").val(ename);
$("#empno").val(empno);
$("#job").val(job);
$("#sal").val(sal);
$("#dept").val(dept);
})
}
function loadEmps(){
var contentDiv = $("#contentDiv");
var tableElement = $("<table border='1'><tr><th>序号</th><th>员工姓名</th><th>工作</th><th>工资</th><th>部门</th></tr></table>");
var tbodyElement = $("<tbody id='tb'></tbody>");
$.ajax({
url : "${pageContext.request.contextPath}/emp/queryallAjax",
dataType:"json",
type:"post",
success:function(data){
//alert(data);
$.each(data, function (i,e){
var trElement = $("<tr></tr>");
trElement.append("<td>"+ (i+1)+"</td>");
trElement.append("<td empno='"+e.empno+"'>"+ e.ename+"</td>");
trElement.append("<td>"+ e.job+"</td>");
trElement.append("<td>"+ e.sal+"</td>");
trElement.append("<td deptno='"+e.dept.deptno+"'>"+ e.dept.dname+"</td>");
//alert(e.ename);
tbodyElement.append(trElement);
})
tableElement.append(tbodyElement);
contentDiv.html(tableElement);
}
});
}
</script>
</head>
<body>
<div align="center">
<input type="button" value="刷新" onclick="loadEmps()"/>
<br/>
<div id="contentDiv">
</div>
<div>
<form id="mform">
<input type="hidden" name="emp.empno" id="empno" value=""/>
员工姓名:<input type="text" name="emp.ename" value="" id="ename"/><br/>
员工工作:<input type="text" name="emp.job" value="" id="job"/><br/>
员工工资:<input type="text" name="emp.sal" value="" id="sal"/><br/>
员工部门:<select id="dept" name="emp.dept.deptno"></select><br/>
<input type="button" id="btnAdd" value="添加"/>
<input type="button" id="btnDel" value="删除"/>
<input type="button" id="btnUpdate" value="修改"/>
</form>
</div>
</div>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.sendRedirect(path + "/emp/queryall");
%>
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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>
<title>My JSP 'list.jsp' starting page</title>
</head>
<body>
<div align="center">
<table border=1>
<tr>
<th>序号</th>
<th>员工姓名</th>
<th>员工工作</th>
<th>员工部门</th>
<th>员工工资</th>
</tr>
<s:iterator value="emps" var="e" status="s">
<tr>
<td><s:property value="#s.index"/></td>
<td><s:property value="#e.ename"/></td>
<td><s:property value="#e.job"/></td>
<td><s:property value="#e.dept.dname"/></td>
<td><s:property value="#e.sal"/></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
[代码运行效果截图]
[源代码打包下载]