SSH框架&整合

1、SSH开发所需要的所有基本jar包

链接:https://pan.baidu.com/s/1yJEnP6LWJ4x7DtxbKHOENA
提取码:4msp

2、SSH开发所需要的配置文件

2.1 Struts的配置文件

  • web.xml中配置struts的核心过滤器
 <!-- Struts2的核心过滤器 -->
  <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>
  • struts.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的常量 -->
	<constant name="" value=""/>
		
	<package name="包名唯一" extends="struts-default" namespace="/">
		<!--配置拦截器-->
		<interceptors>
			<interceptor name="拦截器名称" class="拦截器全路径"></interceptor>
		</interceptors>
		
		<!--全局结果视图-->
		<global-results>
			<result name="名称">页面路径</result>
		</global-results>
	
		<!--配置action-->
		<action name="" class="" method="">
			<result name=""></result>
			
			<!--====如果使用到拦截器,则进行下面的配置======-->
			<interceptor-ref name="">
				<param name=""></param>	
			</interceptor-ref>
			<!--一旦使用自定义拦截器,就需要手动添加默认拦截器-->
			<interceptor-ref name="defaultStack"/>
			<!--====如果使用到拦截器,则进行的配置==========-->
			
		</action>	
	</package> 
</struts>

2.2 Hibernate的配置文件

  • hibernate.cfg.xml
    • 如果将hibernate的配置交给spring则不需要这个文件,换成一个属性文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//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:///....</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置================ -->
		<!-- 打印SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 配置C3P0连接池 -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!--在连接池中可用的数据库连接的最少数目 -->
		<property name="c3p0.min_size">5</property>
		<!--在连接池中所有数据库连接的最大数目  -->
		<property name="c3p0.max_size">20</property>
		<!--设定数据库连接的过期时间,以秒为单位,
		如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
		<property name="c3p0.timeout">120</property>
		 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
		<property name="c3p0.idle_test_period">3000</property>
		
		<!-- 引入映射 -->
		<mapping resource="映射文件的全路径"/>
	</session-factory>
</hibernate-configuration>
  • 这是属性文件,文件名为jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///....
jdbc.username=root
jdbc.password=root
  • 映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
    	<class name="" table="">
    		<id name="" column="">
    			<generator class=""></generator>
    		</id>
    		<property name="" column=""></property>    	
    	</class> 
    </hibernate-mapping>

2.3 spring的配置文件

  • web.xml中配置spring的监听器
<!-- 配置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>
  • 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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 开启属性注入的注解 -->
	<context:annotation-config/>
	
	<!--========================这部分的配置是针对将hibernate的配置交给spring管理=============================-->
	<!-- 引入外部属性文件=============================== -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置C3P0连接池=============================== -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 配置Hibernate的相关属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		
		<!-- 引入映射文件 -->
		<property name="mappingResources">
			<list>
				<value>映射文件的全路径</value>
			</list>
		</property>
	</bean>
	<!--========================这部分的配置是针对将hibernate的配置交给spring管理=============================-->
	
	<!-- 配置Action -->
	<bean id="...action" class="action的全路径" scope="prototype">
		<!--属性注入,注入Service,如果是注解方式的开发,则不需要接下来的配置-->
		<property name="userService" ref="userService"/>
	</bean>

	<!-- 配置Service -->
	<bean id="....Service" class="service的全路径">
		<!--属性注入,注入Dao,如果是注解方式的开发,则不需要接下来的配置-->
		<property name="userDao" ref="userDao"/>
	</bean>

	<!-- 配置DAO -->
	<bean id="...Dao" class="dao的全路径">
		<!--属性注入,注入事务管理,注解方式的开发,也需要配置,因为这个管理器的类并没有交由spring管理-->
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_43642812/article/details/84928718