基于J2EE的CRUD使用详解

原创|其它|编辑:郝浩|2009-10-15 09:43:27.000|阅读 583 次

概述:CRUD是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  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:message 
                key="go.add" /> </a> 
      </div> 
</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 
  {
  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>  
… 

  在本项目的开发过程中运用这样的技术方法大大提高了开发效率、增加了程序的可读性、减少了模块间的耦合性,这样使得系统更加容易更新和维护。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP