spring3,springMVC,Hibernate4整合開發

spring.xml文件的主要配置(包括了與hibernate4整合的):

<?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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 自動掃描與裝配bean -->
	<context:component-scan base-package="com.jian.luntan"></context:component-scan>
	
	<!-- 導入外部的properties文件 -->
	<context:property-placeholder location="classpath:config.properties"/>
	<!-- 配置數據源 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 數據連接信息 -->
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<!-- 其他配置 -->
		<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
		<property name="initialPoolSize" value="3"></property>
		<!--連接池中保留的最小連接數。Default: 3 -->
		<property name="minPoolSize" value="3"></property>
		<!--連接池中保留的最大連接數。Default: 15 -->
		<property name="maxPoolSize" value="5"></property>
		<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
		<property name="acquireIncrement" value="3"></property>
		<!-- 控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
		<property name="maxStatements" value="8"></property>
		<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
		<property name="maxStatementsPerConnection" value="5"></property>
		<!--最大空閒時間,1800秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
		<property name="maxIdleTime" value="1800"></property>
	</bean>
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 指定hibernate的配置文件位置 -->
		<!-- 配置c3p0數據庫連接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<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>
			</props>
		</property>
		<!-- 自動掃描註解方式配置的hibernate類文件 -->
		<property name="packagesToScan">
			<list>
				<value>com.jian.luntan.domain</value>
			</list>
		</property>
		<!-- 自動掃描hbm方式配置的hibernate文件和.hbm文件 -->
		<!-- 
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:sy/hbm</value>
			</list>
		</property>
		 -->
	</bean>
	<!-- 配置聲明式事務管理(採用註解的方式) -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
	
</beans>


springMVC.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/mvc  
	       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
	<!-- 1、掃描註解 -->
	<context:component-scan base-package="net.seehope.action"></context:component-scan>
	<!-- 2、配置action映射和action的方法映射 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!--使用默認方式排除攔截靜態資源 -->
	<mvc:default-servlet-handler/>
	
	<!-- 3、配置返回視圖格式-->
	<bean 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	
	
	
</beans>


log4j.properties主要配置:

log4j.rootLogger=INFO,A1,R

log4j.appender.A1=org.apache.log4j.ConsoleAppender
#配置日誌信息打印到控制檯
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%c]%m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender 
#輸出信息到文件的文件名稱,默認在webRoot目錄下
log4j.appender.R.File=zhkunion.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.Threshold=ALL
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#[%p]即DEBUG,INFO,WARN,ERROR,FATAL
#[%d]輸出日誌時間點的日期或時間
log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

web.xml主要配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  
   <!-- 引入Spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/spring.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- mvc控制中心 -->
	<servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-mvc.xml</param-value>
		</init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

至此,配置文件就配置完了,接下來就引入相關的jar包了,包括spring、hibernate等相關jar包,下圖列出一部分:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章