ssh三框架整合

来源:互联网 发布:北国知春物业 编辑:程序博客网 时间:2024/04/30 03:41
1. 加入 Spring
1). 加入 jar 包

2). 配置 web.xml 文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>

//*多个applicationContextXXXX.xml公用一个IOC容器
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3). 加入 Spring 的配置文件applicationContext.xml. 

<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

2. 加入 Hibernate
1). 同时建立持久化类, 和其对应的 .hbm.xml 文件, 生成对应的数据表

2). Spring 整合 Hibernate

①. 加入 jar 包
②. 在类路径下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性

<hibernate-configuration>
    <session-factory>
    <!-- 配置 hibernate 的基本属性 -->   
    <!-- 方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>    
    <!-- 是否显示及格式化 SQL -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>    
    <!-- 生成数据表的策略 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="current_session_context_class">thread</property>   
// 没有配置报no session异常

    </session-factory>   
</hibernate-configuration>
③. 建立持久化类, 和其对应的 .hbm.xml 文件
④. 和 Spring 进行整合applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/jerome/entities/*.hbm.xml"></property>
</bean>

⑤. 启动项目, 会看到生成对应的数据表

3. 加入 Struts2
1). 加入 jar 包: 若有重复的 jar 包, 则需要删除版本较低的. 
2). 在 web.xml 文件中配置 Struts2 的 Filter

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
3). 加入 Struts2 的配置文件

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
<!-- struts2的 包 -->
    <package name="default" namespace="/" extends="struts-default">
     <action name="emp-*" class="employeeAction"
       method="{1}">
      <result name="list">/WEB-INF/views/emp-list.jsp</result>
     </action>
    </package>
</struts>
4). 整合 Spring
①. 加入 Struts2 的 Spring 插件的 jar 包
②. 在 Spring 的配置文件中正常配置 Action, 注意 Action 的 scope 为 prototype
③. 在 Struts2 的配置文件中配置 Action 时, class 属性指向该 Action 在 IOC 中的 id

 <bean id="employeeAction" class="com.jerome.actions.EmployeeAction" 
    scope="prototype">
    <property name="employeeService" ref="employeeService"></property>
   </bean>

DAO:public Session getSession() {
return this.sessionFactory.openSession();//getCurrentSession()报异常。
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<Employee> getAllEmployee() {
String hql = "From Employeee LEFT OUTER JOIN FETCH e.department";
return getSession().createQuery(hql).list();
}

Services:public class EmployeeServices {
     private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;

public List<Employee> getAll(){
return employeeDao.getAllEmployee();
}
}

Actions:public String list(){
request.put("employee",employeeService.getAll());
    return "list";
     }
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}

0 0
原创粉丝点击