Struts2+Spring2.5.6+Hibernate3+Freemarker整合(一)

本文介绍了一个基于S2SH(Struts2、Spring、Hibernate)和Freemarker的技术栈整合案例,详细展示了如何配置c3p0连接池、Hibernate属性、SessionFactory,并通过Spring管理Bean及HibernateTemplate来实现DAO层操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本部分是整合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());
}
}

下面是结果:
[img]http://dl.iteye.com/upload/attachment/447020/20d0ca8c-c314-3bc6-84ac-b3813002457a.jpg[/img]
测试已经通过了,在[url=http://z276356445t.iteye.com/blog/977695]下一篇[/url]中会介绍Spring+hibernate+struts2+freemarker的整合
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值