<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config />
<context:component-scan base-package="com.li.service.product.imple"></context:component-scan>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:250/cabaretmanage" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean> <bean id="SessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<list>
<value>com.li.entity.product</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.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="txManager"/>   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
           <property name="sessionFactory" ref="SessionFactory"></property>
   </bean></beans>
package com.li.service.dao;import javax.annotation.Resource;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;@Transactional
public abstract class DAOSupport<T> implements DAO<T> {
protected SessionFactory sf; public SessionFactory getHiber() {
return sf;
} @Resource(name="SessionFactory")
public void setHiber(SessionFactory sf) {
this.sf = sf;
} public void del(T obj) {
Session s = sf.getCurrentSession();
s.delete(obj);
} public void dels(T[] objs) {
Session s = sf.getCurrentSession();
for(Object obj:objs){
 s.delete(obj);
}
} @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
public T find(Class clazz,int primary) {

Session s = sf.getCurrentSession();
T t =(T) s.get(clazz, primary);
return t;
} public void save(T entity) {
Session s = sf.getCurrentSession();
s.save(entity);
} public void update(T entity) {
Session s = sf.getCurrentSession();
s.update(entity);
}
     
}package com.li.service.product.imple;import org.springframework.stereotype.Component;import com.li.service.dao.DAOSupport;
import com.li.service.product.ProductTypeService;@Component("productType")
public class ProductTypeServiceEntity<T> extends DAOSupport<T> implements ProductTypeService<T> {}
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
ProductTypeServiceEntity<ProductType> prot =(ProductTypeServiceEntity<ProductType>)context.getBean("productType");