基于J2EE的SSH
整合应用及操作示例二(CRUD操作及配置)
2009-10-08 09:25:22
CRUD
是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能。
In computing, CRUD is an acronym for create, retrieve, update, and delete. It is used to refer to the basic functions of a database or persistence layer in a software system.
C reate new records
R etrieve existing records
U pdate existing records
D elete existing records.
在响应新增部门请求中,通过导入Struts
的配置文件(struts-config.xml)
来完成业务流程的部署。它把depAdd.jsp 和depAdd.do
连接起来。depAdd.jsp上产生客户请求,depAdd.do
则对请求产生响应、处理jsp页面上的数据。当点击depAdd.jsp
上的保存按钮
( <div style="float: right; padding: 10px 25px 0 0;"><div
class="right_button"><a href="#" onclick="check();return false;"><bean:messagekey="go.add"
/> </a></div>
先检查页面数据的正确性,校验通过后将输入数据用Set方法存入到userinfoForm这个FormBean中。
function check(){var userName
= document.userinfoForm.userName.value;var password = document.userinfoForm.password.value;var
length = document.userinfoForm.password.value.length;var repassword = document.userinfoForm.repassword.value;var
tel = document.userinfoForm.tel.value;var department = document.userinfoForm.department.value;if(userName==""){alert('部门管理员姓名不能为空!')return
false;}
if(password==""){alert('密码不能为空!')return
false;}if(length<6||length>10){alert('密码必须大于6个字符小于10个字符!')return
false;}if(repassword==""){alert('重复密码不能为空!')return
false;}if(password!=repassword){alert('密码输入不一致!')return
false;}if(tel!=""){No
= "0123456789()+-"for(i=0; i<tel.length;
i++){var
Checkstr = tel.charAt(i);if (No.indexOf(Checkstr)== -1){alert("联系电话格式不正确!");return
false;}}}if(department==""){alert('部门管理员所属部门不能为空!')return
false;}
else{document.userinfoForm.submit();}
}
然后根据struts-config.xml调用depAdd.do
(这将在Spring配置文件中指定相应的Action)
进行业务处理。在depAdd.do中页面数据将从userinfoForm
中读取。depAdd.do
执行成功后将显示/ok.jsp页面。depAdd.do对应的Action
(DepAddAction)在Spring的配置文件(applicationContext.xml)
中指定。要把depAdd.do和DepAddAction对应起来,首先要在struts-config.xml
中配置Delegating RequestProcessor。其次,需要在application Context.xml中定义名字为“/depAdd”的受管JavaBean。每次对DepAddAction请求时,Delegating
Request Processor将充当代理。同时,DepAddAction
使用到受管Java Beansm Service。要使用UserinfoService,需要在DepAddAction中生成UserinfoService的get()、set()方法,并且application
Context.xml 中对“/depAdd”进行Dep Add Action的依赖注入。因为DepAddAction
并没有直接去操作数据访问Userinfo DAO。而是通过调用业务逻辑层UserinfoService
中的方法来实现业务逻辑的。DepAddAction中部分代码如下:
String userName = userinfoForm.getUserName();String pwd = userinfoForm.getPassword();String
rePwd = userinfoForm.getRepassword();String tel = userinfoForm.getTel();String
dep = userinfoForm.getDepartment();
// 生成userinfo对象Userinfo userinfo =
new Userinfo();
// 将从表单中获得的值赋给该对象userinfo.setUserName(userName);userinfo.setPassword(pwd);userinfo.setTel(tel);userinfo.setDepartment(dep);userinfo.setUserType("dep");//
所有新增用户类型一律为dep// 数据入库
userinfoService.save(userinfo);
如果depAdd.do要对应另一个Action,则只要修改applicationContext.xml
即可,这将有利于系统的更新。同样,如果另一个.do
要对应DepAddAction,也只要在applicationContext.xml中配置即可,这将有利于代码的重用。在本系统中,Hibernate
和Spring
共用一个配置文件applicationContext.xml。Hibernate
从applicationContext.xml
中读取和数据库有关的信息。数据库信息包括数据库连接、与数据库结构相对应的映射文件。在新增部门请求中,涉及到的数据库表为userinfo表,它所对应的映射文件为Userinfo.hbm.xml。为了访问数据库表userinfo,只有Userinfo.hbm.xml映射文件是不够的,还需要数据访问类UserinfoDAO、数据类AbstractUserinfo,Userinfo。数据类Userinfo的实现较为简单,它只是Java对象与数据库表之间的对应,主要用于在各应用层间传递数据,在它的基础上要实现的就是数据访问类UserinfoDAO。系统在生成UserinfoDAO
的同时,也将UserinfoDAO作为JavaBean配置到applicationContext.xml
中。UserinfoDAO中是对userinfo
表进行保存、查询、删除或修改等基本数据操作,在applicationContext.xml
中需要userinfoService
进行UserinfoDAO
及其代理的依赖注入。这样做,使得当UserinfoDAO
变化时,只需修改applicationContext.xml给userinfoService实现新的注入,指向新的实现就可以了,由此解除了数据访问层和业务层的紧密耦合。数据访问类UserinfoDAO
继承于辅助类Hibernate-DaoSupport,借助于getHibernateTemplate()
获得对Hibernate资源的操作,极大的方便了Hibernate框架的使用。在UserinfoDAO中定义了对数据库表userinfo的操作函数。如下面代码,即是UserinfoService
中调用的saveData
方法。
public
class UserinfoDAO
extends HibernateDaoSupport implements IUserinfoDAO {private
static final Log log = LogFactory.getLog(UserinfoDAO.class);
protected
void initDao() {// do nothing}
public
void save(Userinfo transientInstance) {log.debug("saving
Userinfo instance");try {getHibernateTemplate().save(transientInstance);log.debug("save
successful");}
catch (RuntimeException re) {log.error("save
failed", re);throw re;}
部门添加模块中applicationContext.xml中的代码如下:
<!-- 创建事务管理类 --><bean
id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property
name="sessionFactory"><ref
local="sessionFactory"
/></property></bean>
<!-- 创建用户事务代理类 --><bean
id="userinfoDAOProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property
name="transactionManager"><ref
bean="transactionManager"
/></property><property
name="target"><ref
local="UserinfoDAO"
/> </property><property
name="transactionAttributes"><props><prop
key="save*">PROPAGATION_REQUIRED</prop><prop
key="modify*">PROPAGATION_REQUIRED</prop><prop
key="delete*">PROPAGATION_REQUIRED</prop><prop
key="get">PROPAGATION_REQUIRED,readOnly</prop><prop
key="*">PROPAGATION_REQUIRED,readOnly</prop></props>
</property></bean>
…
<!--进行UserinfoDAO及其代理的依赖注入--><bean
id="UserinfoService"
class="com.dpt.ec.service.impl.UserinfoService">
<property
name="userinfoDAO"><ref
bean="userinfoDAOProxy"
/></property></bean>
…
<!--调度管理#新增部门管理员 --><bean
name="/depAdd" class="com.dpt.ec.web.action.admin.DepAddAction"singleton="false"><property
name="userinfoService"><ref
bean="UserinfoService"
/></property></bean>部门添加模块中struts-config.xml中的代码如下:<form-beans>
…
<form-bean
name="userinfoForm"
type="com.dpt.ec.web.form.UserinfoForm"
/></form-beans>…
<!-- 调度管理#新增加部门管理员--><action
path="/depAdd" name="userinfoForm"type="org.springframework.web.struts.DelegatingActionProxy" scope="request" validate="false"><forward
name="success"
path="/ok.jsp"
/><forward
name="warning"
path="error.pub"
/><forward
name="failure"
path="error.sys"
/></action>
在本项目的开发过程中运用这样的技术方法大大提高了开发效率、增加了程序的可读性、减少了模块间的耦合性,这样使得系统更加容易更新和维护。
分享到:
相关推荐
对于SSH整合的CRUD操作,掌握了这个就可以说掌握了三者的整合开发
使用ssh2实现的CRUD操作。好东东,初学者可以参考该文档完成三个框剪的整合
本项目采用Java作为主要开发语言,整合了CSS、JavaScript等前端技术,构建了一个基于SSM(Spring、SpringMVC、MyBatis)框架的CRUD(增删改查)操作示例。该示例共包含46个文件,其中: - Java源文件:14个,涵盖了...
通过大量代码实践,总结的一套高效实用的ssh模板,包含拦截器的编写及配置,内封装了一套分页工具类,及jsp相应模板,后台只用接受2个参数,即可完成列表展示,代码内有注释,可自行下载学习。
NULL 博文链接:https://liuzidong.iteye.com/blog/935493
整合了MySQL+Hibernate+Struts2+Spring的一个用户登录与注册系统,可以实现基本的单表CRUD。
SSH整合例子KTV简单实现crud功能 mysql数据库 包含创建数据库表sql语句 可方便初学者观摩
这里基于SSH框架,前面介绍了用户管理的CRUD,这里是在用户管理的CRUD基础上,添加课程管理的CRUD的示例,其它类似的功能可以采用同样的方法,具体教程可以参考...
SSH整合Oracle,CRUD实例(xml配置版)。包含了基本的增删改查。适合SSH初学者。 https://blog.csdn.net/qq_35206244/article/details/81367316
基于注解ssh的通用dao的crud,spring4+struts2+hibernate4
SSH框架CRUD小例子SSH框架CRUD小例子
SSH整合完成了Dept、Emp两张表的CRUD,有事务+日志记录异常。
这是我最近刚做好的SSH的整合CRUD的基本操作,附有工程和教学文档,初学者可以跟着一步步的学,已经学会了的可以给我指导下意见。使用前请先阅读说明.txt.
这里基于SSH框架,通过用户管理的业务背景,实现对数据的增删改查操作,即CRUD。
struts + hibernate + spring, 用户登录后,操作-CRUD (连数据库都给出来了!“傻瓜式导入运行”) SSH整合初学者适用,物有所值....
S2SH简单整合CRUD适合新手练手。jdk1.7,tomcat7.0。数据库Oracle。打包了简单的sql语句,包含了Oracle自增序列的建立。
伴随着Internet的迅速发展,计算机技术正在由基于C/S(client/ Server)模式的应用系统转变为基于B/S模式的应用系统。 过去,网络软件的开发都采用C/S(client)模式,在这种模式下,主要的业务逻辑都集中于客户端...
一些关于ssh(spring struts hibernate)的说明文档,和实用
基于SSH三大框架的增删改查 jar包齐全 直接配置数据库就能运行
1.使用MyEclipse提供的数据库连接工具,连接上数据库; 创建一个连接,名字叫mysql; 2.创建一个web工程;调整一下工程的jdk版本; 8.5只能支持到1.6; 3.加入spring框架; 4.加入hibernate; spring管理hibernate;...