struts、hibernate與spring整合

框架的搭建 java

1搭建Spring3 web

1.1 導入springjar spring

1.2 spring.xml放入src/main/resources sql

<?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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	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.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- 引入項目配置文件 -->
	<!-- <context:property-placeholder location="classpath:config.properties" /> -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:config.properties</value>
			</list>
		</property>
	</bean>

	<!-- 自動掃描dao和service包(自動注入) -->
	<context:component-scan base-package="sy.dao,sy.service" />
</beans>

1.3 web.xml中配置spring監聽器和配置文件位置   數據庫

<!-- spring配置文件位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param>
<!--spring監聽器-->
<listener>
   <listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>


1.4 使用annotation方式把類加入spring容器中: express

若是是爲service包中類添加annotation apache

@Service("userService")
public class UserServiceImpl implements UserServiceI {}
若是是爲 dao 包中類添加 annotation     
@Repository("userDao")
public class UserDaoImpl implements UserDaoI {}

1.5 創建測試類測試spring 瀏覽器

public class TestSpring {
    @Test
    public void test() {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
        UserServiceI userService = (UserServiceI) ac.getBean("userService");
        userService.test();
    }
}
2  搭建 Struts2 並整合 spring3 

2.1 導入struts2jar spring-mvc

2.2 struts.xml放入src/main/resources 緩存

<?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>

	<!-- 指定由spring負責action對象的建立 -->
	<constant name="struts.objectFactory" value="spring" />
	<!-- 全部匹配*.action的請求都由struts2處理 -->
	<constant name="struts.action.extension" value="action" />
	<!-- 是否啓用開發模式 -->
	<constant name="struts.devMode" value="true" />
	<!-- struts配置文件改動後,是否從新加載 -->
	<constant name="struts.configuration.xml.reload" value="false" />
	<!-- 設置瀏覽器是否緩存靜態內容 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 請求參數的編碼方式 -->
	<constant name="struts.i18n.encoding" value="utf-8" />
	<!-- 每次HTTP請求系統都從新加載資源文件,有助於開發 -->
	<constant name="struts.i18n.reload" value="false" />
	<!-- 文件上傳最大值 -->
	<constant name="struts.multipart.maxSize" value="104857600" />
	<!-- 讓struts2支持動態方法調用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- Action名稱中是否可以使用斜線 -->
	<constant name="struts.enable.SlashesInActionNames" value="false" />
	<!-- 容許標籤中使用表達式語法 -->
	<constant name="struts.tag.altSyntax" value="true" />
	<!-- 對於WebLogic,Orion,OC4J此屬性應該設置成true -->
	<constant name="struts.dispatcher.parametersWorkaround" value="false" />

	<package name="basePackage" extends="struts-default">
		
	</package>

</struts>
 

2.3 web.xml中配置struts2filter

<!-- 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>

2.4 使用註解的方式配置action

@ParentPackage("basePackage")
@Action(「userAction」)
public class UserAction {
    public void test() {
        System.out.println("struts successful");
    }
}

3 搭建Hibernate4並整合spring3 
3.1導入Hibernate4jar包

3.2spring-hibernate.xml放入src/main/resources

<?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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	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.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<!-- JNDI方式配置數據源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

	<!-- 配置數據源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />

		<!-- 初始化鏈接大小 -->
		<property name="initialSize" value="0" />
		<!-- 鏈接池最大使用鏈接數量 -->
		<property name="maxActive" value="20" />
		<!-- 鏈接池最小空閒 -->
		<property name="minIdle" value="0" />
		<!-- 獲取鏈接最大等待時間 -->
		<property name="maxWait" value="60000" />

		<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />

		<!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<!-- 打開removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分鐘 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 關閉abanded鏈接時輸出錯誤日誌 -->
		<property name="logAbandoned" value="true" />

		<!-- 監控數據庫 -->
		<!-- <property name="filters" value="mergeStat" /> -->
		<property name="filters" value="stat" />
	</bean>

	<!-- 配置hibernate session工廠 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	   <property name="dataSource" ref="dataSource" />
	      <property name="hibernateProperties">
		 <props>
		   <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
		   <prop key="hibernate.dialect">${hibernate.dialect}</prop>
		   <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
		   <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
		   <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
		 </props>
	      </property>

	      <!-- 自動掃描註解方式配置的hibernate類文件 -->
	      <property name="packagesToScan">
		 <list>
		    <value>zhisuo.model</value>
		 </list>
	      </property>

	      <!-- 自動掃描hbm方式配置的hibernate文件和.hbm文件 -->
	      <!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:sy/hbm</value> </list> </property> -->
	</bean>

	<!-- 配置事務管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	     <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 註解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

	<!-- 攔截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="saveOrUpdate*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="grant*" propagation="REQUIRED" />

			<tx:method name="init*" propagation="REQUIRED" />
			
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />

			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
	<!-- 第一個*表明全部的返回值類型;第二個*表明全部的類;第三個*表明類全部方法;..表明子或者孫子包;最後一個..表明全部的參數 -->
	    <aop:pointcut id="transactionPointcut" expression="(execution(* zhisuo.service..*Impl.*(..)))" />
	  <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>

</beans> 

3.3     web.xml中添加Hibernate的配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring.xml,classpath:spring-hibernate.xml
    </param-value>
</context-param>
<!-- openSessionInView配置 -->
<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
    </filter-class>
	<init-param>
	    <param-name>singleSession</param-name>
	    <param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.4 UserDao中使用SessionFactory來操做數據庫

@Repository("userDao")
public class UserDaoImpl implements UserDaoI {
	
    private SessionFactory sessionFactory;
	
    public SessionFactory getSessionFactory() {
	return sessionFactory;
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
	this.sessionFactory = sessionFactory;
    }
    @Override
    public Serializable save(Tuser tuser) {
	return this.sessionFactory.getCurrentSession().save(tuser);
    }
}
相關文章
相關標籤/搜索