Using Spring+hibernate+struts in Java development

Source: Internet
Author: User
Tags flush

Sping is a good thing, but still like the MVC of struts, in part because the previous projects are used by struts, the second is currently from the number of users or struts, but sping and struts are not mutually exclusive can be combined with the use of Now another popular framework technology tapestry really feel a little bit of it, tapestry is also good, but compared to struts learning threshold is much higher, learning materials are less, the current use of tapestry technology companies are relatively few.


Sping integration of the Hibernate transaction management, this I prefer, can write a lot less code yes. The main use of the following categories:
Import Org.springframework.orm.hibernate.HibernateCallback;
Import Org.springframework.orm.hibernate.SessionFactoryUtils;
Import Org.springframework.orm.hibernate.support.HibernateDaoSupport;
You can write a Hibernate basic tool class, and then each use of the class to inherit it, almost practical methods are inside.


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 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 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);
}


}
The hibernate transaction management in Sping has to be set up in Applicationcontext.xml's configuration file for a simple example:
<?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 so works in any application server
You could easily use Java EE data source instead if this were
Running inside of a-Java 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 would 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-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 sessions 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>
The persisted class that will be used for transaction management as a bean to
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate.HibernateTransactionManager" added in this section.
Put this file in the class root when the system is started, it will automatically load, or you can manually control the load.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.