本部分是整合S2SH+Freemarker,后台用Spring来管理各个bean,Hibernate来做数据库持久化,前端呈现用Freemarker。整合中对Struts2,Hibernate,Spring都是采用Annotation来进行注解类。
首先在ApplicationContext.xml中配置连接池,采用c3p0来配置。
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="user" value="root" />
<property name="password" value="zhangting" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 最小连接数 -->
<property name="minPoolSize" value="5" />
<!-- 验证等待请求是否超时-->
<property name="checkoutTimeout" value="120" />
</bean>
然后对hibernate的一些个相关properties做好相应的配置。
<!-- hibernateProperties -->
<bean id="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 开启使用二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 使用查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
对于Hibernate的一些相关配置差不多就配置好了。然后将这些东西都注入到sessionFactory中。采用Hibernate的Annotation来持久化对象,所以要将这些bean注入到AnnotationSessionFactoryBean这个类中。
<!-- hibernate-annotation sessionFactory 注入配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties" ref="hibernateProperties" />
<property name="packagesToScan">
<list>
<value>com.person.model</value>
</list>
</property>
</bean>
packagesToScan是扫描相应包下的持久化类,现在已经完成了一部分Spring与Hibernate的整合了。接下来做的就是编写一个DAO层。
import java.util.List;
/**
*
* @author 张廷 2011-3-24下午01:43:36
*
*/
public interface BaseDao {
public void insert(Object stu);
public void update(Object stu);
public List findAll(String hql);
public Object findById(Class clazz, Integer id);
public void delete(Object stu);
}
写好接口后,接下来就要写一个对这个接口的实现类,在这之中我都是采用Spring的Annotation来对每个bean进行声明。在DAO层中使用Spring包中HibernateTemplate来操作数据库,毕竟人家都已经写好了,不用白不用。既然使用了Spring,我们当然还是选择Spring来帮我们管理类,接着配置HibernateTemplate。
<!-- hibernateTemplate实现 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
接下来写DAO实现类
@Component
public class BaseDaoImplHibernate implements BaseDao {
@Autowired
private HibernateTemplate hibernateTemplate;
public void delete(Object stu) {
hibernateTemplate.delete(stu);
}
public List findAll(String hql) {
hibernateTemplate.setCacheQueries(true);
return hibernateTemplate.find(hql);
}
public Object findById(Class clazz, Integer id) {
// Object obj = hibernateTemplate.load(clazz, id);
return hibernateTemplate.get(clazz, id);
}
public void insert(Object stu) {
hibernateTemplate.save(stu);
}
public void update(Object stu) {
hibernateTemplate.update(stu);
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
既然使用了Spring的Annotation来声明bean,别忘了配置component-scan。
<context:annotation-config />
<context:component-scan base-package="com" />
接下来写Hibernate持久化类,使用Hibernate的Annotation来声明持久化类。
@Entity
@Table(name = "Student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer stuId;
private String stuNumber;
private String stuName;
private int age;
private String birth;
/*省略getter与setter*/
}
别忘了还要声明事务,让Spring来帮我们管理事务,事务的配置可以选择XML或者Annotation,由于XML来配置事务更加的方便,而且更加的简便,所以我们采用XML的方式来声明事务。
<!-- 事务 begin -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="businessService"
expression="execution(public * com.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 事务 end -->
Hibernate+Spring的整合已经完成了,用junit写个测试类来测试一下
public class HibernateTemplateTest {
private static ApplicationContext ctx;
static{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testDelStu() {
BaseDao dao = (BaseDao) ctx.getBean("baseDaoImplHibernate");
List<Student> list = dao.findAll("from Student");
for(Student stu : list)
System.out.println(stu.getStuName());
}
}
下面是结果:
测试已经通过了,在
下一篇中会介绍Spring+hibernate+struts2+freemarker的整合