传智播erp项目学习,Struts1+Spring2+Hibernate3的配置

来源:互联网 发布:macbook下载什么软件 编辑:程序博客网 时间:2024/06/05 18:40

   今天开始学习erp项目,赖家才老师讲的,赖老师讲课风趣幽默,听起来特别提神。学了Spring好长时间了,还没有在项目中用过,这次就用ssh做erp项目。赖老师今天主要讲了Struts1+Spring2+Hibernate3的配置,确实要比Struts1+Hibernate3的配置复杂,赖老师按步骤都讲的非常透彻,学起来也就不难了。
       下面总结一下Struts1+Spring2+Hibernate3的配置
  基本步骤:
1、 新建一个web工程:(b/s)
2、 导入所需要的所有jar包,包括ssh的包和数据库驱动,可以在bulid path中建一个library,把这些包加进来,以后再使用时直接添加library就行了
3、 加入spring特性:
4、 加入hibernate特性:
5、 实现spring+hibernate
6.   加入struts
具体操作,
1,在web工程上选择myeclipse-->add spring capabilitise ,创建一个applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
2, 右击该工程 myeclipse-->add hibrenate capabilitise 并按向导构建数据源  和配置sessionFactory,注意(1)在步骤define hibernate configuration要选择 spring configuration file ,不要选择hibernate configuration file(hibernate.cfg.xml),因为要在applicationContext.xml中构建数据源,以整合Spring和hibernate。
(2)最后不用 选择create sessionfactory class,而是使用Spring提供的org.springframework.orm.hibernate3.LocalSessionFactoryBean。
导构建数据源
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&amp;characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>

sessionFactory对Spring和hibernate的整合提供支持
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>

3.打开DB Browse 就会显示在上一步中建立的连接mysqldriver,点击它和数据库建立连接,就会看到说创建的所有数据库,在数据库ssh中建立user表,右击它选择Hibernate mapping and application generation. 创建pojo和它的.hbm.xml文件。
要选中update hibernate configuration with mapping file location,在applicationContext.xml文件中建立它的映射。
生成的User.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.future.User" table="user" catalog="ssh">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>
</class>
</hibernate-mapping>

 applicationContext.xml文件中在bean  sessionFactory中增加了一个属性,
<property name="mappingResources">
<list>
<value>com/future/User.hbm.xml</value>
</list>
</property>
4.创建dao,让它继承HibernateDaoSupport和配置dao。
<bean id="userDao" class="com.future.UserDAO">
    <property name="sessionFactory">
      <ref local="sessionFactory" />
   </property>
</bean>
5 测试dao,向数据库中添加一行数据
public class UserDAO extends HibernateDaoSupport{
  public void addUser(User user){
    this.getHibernateTemplate().save(user);
  }
  public void deleteUser(Integer id){
    User user = new User();
    user.setId(id);
    this.getHibernateTemplate().delete(user);
}
//1.test spring + hibernate dao.
public static void main(String[] args) {
   ApplicationContext applicationContext = new ClassPathXmlApplicationContext   ("applicationContext.xml");
   UserDAO dao= (UserDAO)applicationContext.getBean("userDao");
   User user = new User("user","123456");
   dao.addUser(user);
  }
}

6、 业务逻辑处理
(1) 新建业务逻辑处理模块UserService,使用dao模块
(2) 在applicationContext文件中添加配置:使用声明式事务处理,即使用Spring提供的org.springframework.orm.hibernate3.HibernateTransactionManager
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Spring 事务管理器代理 -->
<bean id="transactionProxyFactory" abstract="true" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="count*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 用户定义的service方法 -->
<bean id="userService" parent="transactionProxyFactory">
<property name="target">
<bean class="com.future.UserService">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
</property>
</bean>
7.加入struts特性:add struts capabilitise
8、 配置struts:
修改struts-config.xml配置文件
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
9、 构建显示层form和action和页面:
在action中调用业务逻辑层UserService
public class RegisterAction extends Action {
private UserService userService = null;
10、 在struts中配置action和form
<action name="registerForm"
path="/register"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="false">
<forward name="registerResult" path="/RegisterResult.jsp" />
</action>
11、 在spring配置文件中注入action
<bean name="/register" class="com.future.RegisterAction">
<property name="userService">
<ref local="userService" />
</property>
</bean>

原创粉丝点击