SSH三大框架的整合

搭建项目:

           整合一下ssh,过程有点艰辛,报了很多的bug,但是只要坚持就可以解决,当我们遇到困难的时候如果解决不了,请一定要冷静而且可以暂且把问题放一放,可以玩玩游戏,看看电影什么的,但是请不要放弃解决困难的勇气,时间会见证一切。

1.导入三大框架所需要的jar包(注意struts和hibernate有一个包是冲突的javassits.jar只能保留一个)

SSH三大框架的整合SSH三大框架的整合

2.配置web.xml文件!(分两部分,第一部门是struts2的,第二部分是spring的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>xxxx</display-name>
  <!-- struts2的过滤 -->
  <filter>
  	<filter-name>Struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>Struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- spring的监听 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
</web-app>

3.创建struts2的xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!--struts2的其他代码-->
</struts>

4.创建hibernate的xml文件以及用hibernate反向生成实体类

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/指向的数据库</property>
    <property name="hibernate.connection.password">sql密码</property>
    <property name="hibernate.connection.username">sql账号</property>
    <!--数据库方言-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- <property name="hibernate.current_session_context_class">thread</property> -->
    <!-- <property name="hibernate.show_sql">true</property> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

5.(重点)创建spring的xml文件,命名为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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

<!-- 6、自动加载构建bean 表明com.svse包及其子包中,如果某个类的头上带有特定的注解@Component,@Repository,@Service,@Controller等,就会将这个对象作为Bean注入进spring容器-->
  <context:component-scan base-package="com.svse"></context:component-scan>

  <!-- 配置公共组件层 -->
  
  <!-- 第一配:配置数据源 -->
  <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
    <property name="user" value="root"></property>
    <property name="password" value="svse"></property>
  </bean>  
  
  <!-- 第二配:配置SessionFactory(ldcp) -->
  <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
    <property name="dataSource" ref="comboPooledDataSource"></property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <property name="packagesToScan">
      <list>
        <value>com.svse.entity</value>
      </list>
    </property>
  </bean>
  
  
  <!-- 第三配:配置事务管理器 -->
  <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  
  <!-- 第四配:配置事务传播特性 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="del*" propagation="REQUIRED"/>
      <tx:method name="*" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  
  
  <!-- 第五配:哪些包下的哪些类的哪些方法要参与事务 -->
  <aop:config>
    <aop:pointcut expression="execution(* com.svse.impl.*.*(..))" id="allmethods"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allmethods"/>
  </aop:config>
 

</beans>

创建用于数据层继承的SessionFactory的抽象类(必要)
 

public abstract class HibernateUtil  {

    @Autowired
    private SessionFactory sessionFactory;
    
    public Session getSession(){
    	return this.sessionFactory.getCurrentSession();
    }
    
}

到此差不多就搭建完了, 剩下的就是一些spring的@注解。

@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@Scope注解 作用域
@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件、 
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Scope用于指定scope作用域的(用在类上)

解释的有点不完整,但框架的整体大概就是这样,里面的细节还是要自己去克服一下。

有什么不全的大家多多指教,一起学习,共同进步!

有问题以及需要ssh项目搭建的源码可以私信!