日期:2014-05-20  浏览次数:20809 次

求一个关于hibernate的操作问题
请教关于hibernate插入数据库数据的问题,以下是主要的代码:
BaseDao.java
Java code

public void save(T entity);



BaseHibernateDao.java
Java code

public void save(T entity) {   
    try {   
        getHibernateTemplate().save(entity);  
    if (logger.isDebugEnabled()) {   
         logger.debug("保存实体类成功," + entity.getClass().getName()); 
    }   
    } catch (RuntimeException e) { 
        logger.error("保存实体异常," + entity.getClass().getName(), e);   
        throw e;   
    }   
}   



Service.java
Java code

void saveSvrplan(JySvrplan svrplan);


ServiseImpl.java
Java code

public void saveSvrplan(JySvrplan svrplan){
    try{
        svrplanDao.save(svrplan);
    }catch(Exception e){
        e.printStackTrace();
    }
}



Action.java
Java code

public String saveSvrplanInfo(){
    svrplan.setSvrtype((short)1);
        
    ActionContext ctx = ActionContext.getContext();        
    HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 

    svrplan.setSvrname(request.getParameter("dsname"));
    svrplan.setData(request.getParameter("dsdes"));
    svrplan.setOrg(Integer.parseInt(request.getParameter("dsage")));
    svrplan.setMin(Integer.parseInt(request.getParameter("dsm")));
    svrplan.setMax(Integer.parseInt(request.getParameter("ds")));
        
    svrplan.setDbl(request.getParameter("dsdbl"));
    svrplan.setDbt(Short.parseShort(request.getParameter("dsdbt")));
    svrplan.setDb(request.getParameter("dsdbip"));
    svrplan.setDbp(Integer.parseInt(request.getParameter("dsdb")));
    svrplan.setDbu(request.getParameter("dsdbur"));
    svrplan.setDbli(request.getParameter("dsdblik"));
    svrplan.setDbn(request.getParameter("dsdbne"));
    svrplan.setDbpwd(request.getParameter("dsdbd"));
        
    svrplan.setQybj(Short.parseShort(request.getParameter("isde")));*/
        
    [color=#FF0000]svrplanService.saveSvrplan(svrplan);[/color]
    return null;
}


applicationContext.xml
XML code

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="mappingResources">
        <list>
          <value>com/shinee/monitor/model/JySvrplan.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.connection.autocommit">true</prop>
        </props>
   </bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
      <property name="sessionFactory">   
        <ref bean="sessionFactory"/>   
      </property>   
   </bean>
  
   <!-- Hibernate Template定义 -->    
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">    
      <property name="sessionFactory">    
    <ref bean="sessionFactory" />    
    </property>      
   </bean>