关闭

java开发中使用spring+hibernate+struts

标签: hibernatestrutsspringjavaimportstring
5465人阅读 评论(1) 收藏 举报

  sping是个好东西,可还是喜欢struts的MVC,一部分原因是以前做的项目都是struts的使用习惯了,第二目前从用户数来说还是struts的最多,但sping与struts也并不是相排斥的可以结合着使用,相比而言现在另一热门的框架技术Tapestry还真觉得有点排它性,Tapestry也是不错,但相比struts而言学习门槛要高得多,学习资料也少,目前使用Tapestry技术的公司也比较少。


  sping中整合了hibernate的事务管理,这个偶比较喜欢,可以少写好多代码耶。主要是用到以下这几个类吧:
   import org.springframework.orm.hibernate.HibernateCallback;
   import org.springframework.orm.hibernate.SessionFactoryUtils;
   import org.springframework.orm.hibernate.support.HibernateDaoSupport;
大家可以先写一个hibernate基础工具类,然后各使用到的类继承它,差不多实用的方法都在里面了。


import java.util.Collection;
import java.util.Iterator;
import java.util.List;


import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.type.Type;


import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.SessionFactoryUtils;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;


public class BaseDAO extends HibernateDaoSupport{
 
 public BaseDAO(){
  super();
 }
 
   public void clear() throws DataAccessException
   {
     getHibernateTemplate().clear();
   }


   public void flush() throws DataAccessException
   {
     getHibernateTemplate().flush();


   }


   public int countByNamedQuery(final String queryName, final Object[] values)
       throws DataAccessException
   {
     String str = getQueryString(queryName);
     str = getCountString(str);
     return ((Integer) getHibernateTemplate().find(str, values).get(0))
         .intValue();
   }


   public int countByNamedQueryAndNamedParam(final String queryName,
       final String[] paramNames, final Object[] values)
       throws DataAccessException
   {
     String str = getQueryString(queryName);
     str = getCountString(str);
     return ((Integer) getHibernateTemplate().findByNamedParam(str, paramNames,
         values).get(0)).intValue();
   }


   public int delete(String queryString, Object value, Type type)
       throws DataAccessException
   {
     return getHibernateTemplate().delete(queryString, value, type);
   }


   public void deleteAll(Collection entities) throws DataAccessException
   {
     getHibernateTemplate().deleteAll(entities);


   }


   public List find(String queryString, Object[] values)
       throws DataAccessException
   {
     return getHibernateTemplate().find(queryString, values);
   }


   public List findByNamedParam(String queryString, String[] paramNames,
       Object[] values) throws DataAccessException
   {
     return getHibernateTemplate().findByNamedParam(queryString, paramNames,
         values);
   }


   public List findByNamedQuery(String queryName, Object value)
       throws DataAccessException
   {
     return getHibernateTemplate().findByNamedQuery(queryName, value);
   }


   public List findByNamedQuery(String queryName, Object[] values)
       throws DataAccessException
   {
     return getHibernateTemplate().findByNamedQuery(queryName, values);
   }


   public List findByNamedQueryAndValueBean(String queryName, Object valueBean)
       throws DataAccessException
   {
     return getHibernateTemplate().findByNamedQueryAndValueBean(queryName,
         valueBean);
   }


   public List findPage(final String queryString, final Object value,
       final int firstResult, final int maxResults) throws DataAccessException
   {
     return getHibernateTemplate().executeFind(new HibernateCallback()
     {


       public Object doInHibernate(Session session) throws HibernateException
       {
         Query query = session.createQuery(queryString);
         query.setParameter(0, value);
         query.setFirstResult(firstResult);
         query.setMaxResults(maxResults);
         return query.list();


       }
     });
   }


   public List findPageByNamedQuery(final String queryName, final Object value,
       final int firstResult, final int maxResults) throws DataAccessException
   {
     return getHibernateTemplate().executeFind(new HibernateCallback()
     {


       public Object doInHibernate(Session session) throws HibernateException
       {
         Query query = session.getNamedQuery(queryName);
         query.setParameter(0, value);
         query.setFirstResult(firstResult);
         query.setMaxResults(maxResults);
         return query.list();


       }
     });
   }


   public Iterator iterate(String queryString) throws DataAccessException
   {
     return getHibernateTemplate().iterate(queryString);
   }


}
要使用sping中hibernate的事务管理还得要在applicationContext.xml这个配置文件中设置一下,举个简单的例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- $Id: uum-applicationContext.xml,v 1.2 2005/09/21 09:24:09 wj Exp $ -->
<beans>
  <!--
    Datasource that works in any application server
    You could easily use J2EE data source instead if this were
    running inside of a J2EE container.
  -->
  <!--bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:comp/env/jdbc/uumDataSource</value>
        </property>
  </bean-->
 
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
      <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
      <value>jdbc:oracle:thin:@127.0.0.1:1521:demo</value>
    </property>


    <property name="username">
      <value>spuser</value>
    </property>
    <property name="password">
      <value>123456</value>
    </property>
  </bean>
  <!-- Hibernate SessionFactory -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="dataSource">
      <ref local="dataSource"/>
    </property>
    <!-- Must references all OR mapping files. -->
    <property name="mappingResources">
      <list>
        <value>pojo/FujianInfo.hbm.xml</value>
  <value>pojo/SpInfo.hbm.xml</value>
  <value>pojo/User.hbm.xml</value>
      </list>
    </property>
    <!--
      Set the type of database; changing this one property will port this to Oracle,
      MS SQL etc.
    -->
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
        <prop key="hibernate.query.substitutions">true 1,false 0</prop>
        <prop key="hibernate.jdbc.fetch_size">50</prop>
        <prop key="hibernate.jdbc.batch_size">0</prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
  </bean>
  <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
  <bean id="iserviceTarget" class="service.spring.Service" singleton="false">
 <property name="userDAO">
      <ref local="userDAO"/>
    </property>
    <property name="spInfoDAO">
      <ref local="spInfoDAO"/>
    </property>
 <property name="fujianInfoDAO">
      <ref local="fujianInfoDAO"/>
    </property>
  </bean>
  <!-- Transactional proxy for the Petclinic primary business object -->
  <bean id="iservice" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
      <ref local="transactionManager"/>
    </property>
    <property name="target">
      <ref local="iserviceTarget"/>
    </property>
    <property name="transactionAttributes">
      <props>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="insert*">PROPAGATION_REQUIRED</prop>
        <prop key="store*">PROPAGATION_REQUIRED</prop>
        <prop key="appand*">PROPAGATION_REQUIRED</prop>
        <!--prop key="add*">PROPAGATION_REQUIRED</prop-->
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="remove*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="count*">PROPAGATION_REQUIRED</prop>
        <prop key="is*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
  <!-- Pass the session factory to our DAOs -->
  <bean id="userDAO" class="dao.hibernate.UserDAO">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
  <bean id="fujianInfoDAO" class="dao.hibernate.FujianInfoDAO">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
  <bean id="spInfoDAO" class="dao.hibernate.SpInfoDAO">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
</beans>
将要用到事务管理的持久类作为bean往
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">这一段里添加。
将这个文件放在class根目录下系统起动时会自动加载,也可以自己手动控制加载。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:151978次
    • 积分:2168
    • 等级:
    • 排名:第17434名
    • 原创:50篇
    • 转载:47篇
    • 译文:1篇
    • 评论:30条
    最新评论