ssh整合

370 阅读7分钟

[toc]

xml方式整合

web.xml

 <!-- 
  	配置spring的核心监听器,这个类实现了ServletContextListener接口。
  	在它的父类ContextLoader中有一个属性contextConfigLocation,
  	通过配置它指定applicationContext.xml文件的位置。
   -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 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>

applicationContext.xml

<!-- spring整合hibernate方式一 -->
	<!-- 
	org.springframework.orm.hibernate5.LocalSessionFactoryBean这个类是spring提供的专门用来配置hibernate的,
	(注意,选择的版本要和使用的hibernate的版本对应起来)
	我们可以使用它来加载配置文件,或者直接通过设置它的属性来配置配置hibernate。
	在这个类中,有一个"private Resource[] configLocations"属性,
	还有"setConfigLocation(Resource configLocation)和setConfigLocations(Resource... configLocations)"两个方法,
	这两个方法都是在给configLocations属性赋值。
	<property/>标签中name属性的取值其实就是按照类中的set方法来进行取值的。
	问题:如果有多个配置文件要加载怎么办?
	
	原本加载配置文件及获得相关对象的方法:
	// 1.创建Configuration来加载配置文件
	Configuration config = new Configuration().configure();
	// 2.得到SessionFactory
	SessionFactory sessionFactory = config.buildSessionFactory();
	// 3.得到session
	Session session = sessionFactory.openSession();
	 -->
	<!-- 
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
	</bean>
	 -->
	 
	 
	 
	 <!-- spring整合hibernate方式二:和上面使用的是同一个类,使用这种方式就不需要"hibernate.cfg.xml"文件了 -->
	 <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	 	
	 	<!-- 类中的这个属性"hibernateProperties"是在配置hibernate的相关配置 -->
	 	<property name="hibernateProperties">
			<!-- 以下属性在书写时不能省略hibernate -->
			<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.format_sql">true</prop> 
			</props>
			<!-- 上述的配置可以简写成以下 -->
			<!-- <value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.format_sql=true
			</value> -->
		</property>
		
		<!-- 加载连接池 -->
		<property name="dataSource" ref="dataSource" />
		
		<!-- 加载hibernate的Xxx.hbm.xml配置文件,类中有四个属性供选择 -->
		<!-- 它类似于我们之前<mapping resource=””> -->
		<!-- <property name="mappingResources">
			<list>
				<value>com/lbb/domain/User.hbm.xml</value>
			</list>
		</property> -->
		<!-- 它加载时是根据类路径加载 classpath:路径 -->
		<!-- <property name="mappingLocations">
			<list>
				<value>classpath:com/lbb/domain/User.hbm.xml</value> 
			</list>
		</property> -->
		<!-- mappingDirectoryLocations 它加载的目录 -->
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:com/lbb/domain</value> 
			</list>
		</property>
		<!-- 还有一个mappingJarLocations它会加载jar文件中的hbm.xml文件,不常用 -->
	 </bean>
	 
	 <!-- 加载properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	 <!-- 配置连接池,可以继续配置最大最小,空闲连接数及超时信息等 -->
	<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>
	
	<!-- 
	声明dao
	spring整合hiberante后,我们的dao只需要继承HibernateDaoSupport类,
	在HibernateDaoSupport中只需要注入SessionFactory就可以获得到HibernateTemplate。
	HibernateTemplate它是对hibernate操作的一个简单封装,可以让我们方便使用原来hibernate的操作。
	 -->
	<bean id="userDao" class="com.lbb.dao.UserDAOImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 声明service -->
	<bean id="userService" class="com.lbb.service.UserServiceImpl">
		<property name="userDao" ref="userDao"/>
	</bean>
	
	<!-- 声明式事务管理:spring使用的是传统的aop技术实现的事务管理 -->
	<!-- 
	事务管理器
	org.springframework.transaction.PlatformTransactionManager这是一个spring提供的事务管理器接口,
	可以来选择相关的平台(jdbc  hibernate  jpa…),在不同的持久化层解决技术它的事务代码不一样。
	spring同时提供了三个子类供我们选择,
	DataSourceTransactionManager 主要针对于JdbcTemplate开发  MyBatis开发
	HibernateTransactionManasger主要针对于Hibernate开发
	JpaTransactionManager 主要针对于JPA开发。
	 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="add"/>
			<tx:method name="update"/>
			<tx:method name="del"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.lbb.service.*..*(..))" id="mypointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
	</aop:config>
	
	
	
	
	<!-- spring整合struts2框架方式一 -->
	<!-- 配置action:struts2框架的action要求是多例的,而spring创建的bean默认是单例的,所以要配置scope="prototype" -->
	<!-- <bean id="userAction" class="com.lbb.action.UserAction" scope="prototype">
		<property name="userService" ref="userService"/>
	</bean> -->
	
	<!-- spring整合struts2框架方式二,就不需要再配置action了 -->

hibernate.cfg.xml

<hibernate-configuration>
	<session-factory>
		<!-- 配置关于数据库连接的四个项 driverClass url username password -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3366/sshtest</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123</property>

		<!-- 设置连接提供者 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- c3p0连接池的配置 -->
		<property name="hibernate.c3p0.max_size">20</property> <!-- 最大连接池 -->
		<property name="hibernate.c3p0.min_size">5</property> <!-- 最小连接数 -->
		<property name="hibernate.c3p0.timeout">120</property> <!-- 超时 -->
		<property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接 -->

		<!-- 可以将向数据库发送的sql显示出来 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->
		<property name="hibernate.format_sql">true</property>

		<!-- hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>

		<mapping resource="com/lbb/domain/User.hbm.xml"/>

	</session-factory>
</hibernate-configuration>

struts.xml

<!-- spring整合struts2框架方式一:class属性的取值是applicationContext.xml文件中配置的id -->
	<!-- <package name="default" namespace="/" extends="struts-default">
		<action name="user_*" class="userAction" method="{1}">
			<result name="success">/success.jsp</result>
		</action>
	</package> -->

	<!-- spring整合struts2框架方式二:action中自动注入service -->
	<!-- 
	在default.properties中有autoWires属性,
	注入的方式默认是按照name注入,我们可以修改注入方式按照type注入
	 -->
	<constant name="struts.objectFactory.spring.autoWire" value="type"/>
	
	<package name="default" namespace="/" extends="struts-default">
		<action name="user_*" class="com.lbb.action.UserAction" method="{1}">
			<result name="success">/success.jsp</result>
		</action>
	</package>

db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3366/sshtest
jdbc.username=root
jdbc.password=123

注解方式

web.xml

<context-param>
  	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  <!-- openSessionInViewFilter -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  <!-- 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>

applicationContext.xml

<!-- 开启spring的注解扫描  @Respostory  @Service  @Controller-->
	 <context:component-scan base-package="com.lbb"/>
	
	 <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	 	
	 	<!-- 类中的这个属性"hibernateProperties"是在配置hibernate的相关配置 -->
	 	<property name="hibernateProperties">
			<!-- 以下属性在书写时不能省略hibernate -->
			<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.format_sql">true</prop> 
			</props>
			<!-- 上述的配置可以简写成以下 -->
			<!-- <value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.format_sql=true
			</value> -->
		</property>
		
		<!-- 加载连接池 -->
		<property name="dataSource" ref="dataSource" />
		
		<!-- 加载hibernate注解类,扫描加载jpa的po类 -->
		<property name="packagesToScan">
			<list>
				<value>com.lbb.domain</value>
			</list>
		</property>
	 </bean>
	 
	 <!-- 加载properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	 <!-- 配置连接池,可以继续配置最大最小,空闲连接数及超时信息等 -->
	<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>
	
	
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- spring提供的事务注解驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>