springmvc3.2+spring+hibernate4全注解方式整合(一)
生活随笔
收集整理的這篇文章主要介紹了
springmvc3.2+spring+hibernate4全注解方式整合(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>json_test</display-name><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><!-- 加載所有的配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:com/config/spring/spring-*.xml</param-value></context-param><!-- 配置Spring監聽 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置SpringMVC --><servlet><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/SrpingMvc_servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!-- 配置字符集 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 配置Session --><filter><filter-name>openSession</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSession</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>
首先從我的web.xml文件大概能看出主要配置文件的存放路徑和hibernate的版本。
?
2、springmvc的配置文件
<?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:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 注解探測器 --><context:annotation-config /><!-- 添加注解驅動 --> <mvc:annotation-driven /><!-- 啟動掃描所有的controller 只掃描mvc,不掃描service --><context:component-scan base-package="com.fangjian.core.web"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan> <!-- 允許對靜態資源文件的訪問 --> <mvc:default-servlet-handler /> <mvc:resources mapping="/skin/**" location="/skin/" /> <!-- 定義跳轉的文件的前后綴 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean><!-- 上傳文件大小限制為31M,31*1024*1024 --><bean id="maxUploadSize" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="32505856" /> <property name="maxInMemorySize" value="4096" /> </bean> <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 annotation默認的辦法映射適配器 --> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /><!-- 避免IE在ajax請求時,返回json出現下載 --><bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean> </beans>
3、spring-jdbc.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 引入屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties" /><!-- Hibernate配置 --> <!-- 數據源配置,使用應用內的DBCP數據庫連接池 --><bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" id="dataSource"> <property name="driverClass" value="${jdbc.driver}"> </property> <property name="jdbcUrl" value="${jdbc.url}"> </property> <property name="user" value="${jdbc.username}"> </property> <property name="password" value="${jdbc.password}"> </property> <property name="autoCommitOnClose" value="true"> </property> <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"> </property> <property name="initialPoolSize" value="${cpool.minPoolSize}"> </property> <property name="minPoolSize" value="${cpool.minPoolSize}"> </property> <property name="maxPoolSize" value="${cpool.maxPoolSize}"> </property> <property name="maxIdleTime" value="${cpool.maxIdleTime}"> </property> <property name="acquireIncrement" value="${cpool.acquireIncrement}"> </property> <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"> </property> </bean> <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="packagesToScan" value="com.fangjian.core.platform.po"> </property> <property name="hibernateProperties"> <props> <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="javax.persistence.validation.mode">none</prop><!-- <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProviderr</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop> --> <prop key="current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> --> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="jdbc.use_scrollable_resultset">false</prop> <!-- <prop key="hibernate.transaction.auto_close_session">false</prop> --> </props> </property></bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > <property name="dataSource" ref="dataSource"></property></bean> <!-- 事務管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory"><ref local="sessionFactory"/></property></bean><!-- 采用聲明式容器管理事務一般只對service層進行處理 --> <aop:config expose-proxy="true"><!-- 只對業務邏輯層實施事務 --> <aop:pointcut id="txPointcut" expression="execution(* com.fangjian.core.platform.service.*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> <!-- proxy-target-class="true"<aop:advisor pointcut="execution(* com.fangjian.core.platform.service.*.*(..))" advice-ref="txAdvice"/><aop:advisor pointcut="execution(* com.fangjian.core.platform.service.*.*(..))" advice-ref="txAdvice"/>--></aop:config><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="merge*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必須配置為開啟事務 否則 getCurrentSession()獲取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" rollback-for="java.lang.Exception"/> </tx:attributes></tx:advice></beans>
4、spring自己的配置文件spring-common.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 自動掃描組件,這里要把controler下面的 controller去除,他們是在springMvc-servlet.xml中配置的,如果不去除會影響事務管理的。 --> <context:component-scan base-package="com.fangjian.core"><context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /></context:component-scan></beans>
?
?
轉載于:https://www.cnblogs.com/fangj/p/3820228.html
總結
以上是生活随笔為你收集整理的springmvc3.2+spring+hibernate4全注解方式整合(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《相和歌辞·王昭君二首》第四句是什么
- 下一篇: 经济学发展史