TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:, f" }' w* `: K4 S* [0 ~
2 W) p, I4 e2 h2 b0 G- ^- <?xml version="1.0" encoding="UTF-8"?>
! ^! c% }" g* s+ _/ Z2 Z - <beans xmlns="http://www.springframework.org/schema/beans"
1 r) `5 ?1 V' |7 ~3 i! v - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + ?/ A. T' z) p! N* b5 a& ^
- xmlns:p="http://www.springframework.org/schema/p" ) [3 P. x; s9 f7 k# W& y& c
- xmlns:aop="http://www.springframework.org/schema/aop"
3 c# ?& @7 y% t0 Q4 k' Q - xmlns:context="http://www.springframework.org/schema/context" - C: s) P1 X7 s V# ^7 F
- xmlns:jee="http://www.springframework.org/schema/jee"
- g5 F" V) \3 e& n2 k' T - xmlns:tx="http://www.springframework.org/schema/tx" & `, S8 ?9 d' ? F* m j$ E7 u
- xsi:schemaLocation="
9 q3 E! m e+ m7 x/ Q% Q - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 7 m5 R$ e. g/ [' }( c. U8 V" e* C
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
; q1 n4 K! s" |' y - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
% m% d) k$ J) _ - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
. N: {1 t: D; F. m( @" ~7 d - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- J2 Z" `" u+ f; @% g -
6 l; E n5 X; r' R( \/ h2 I) e - <!-- <mvc:annotation-driven /> -->3 o- g6 c7 c5 b P. i' o
- <!-- 【配置视图解析器】 -->
- s/ f, b8 z! F% i( O- F- S0 R - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> . U; d, m( ~, c3 z
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
! f" g8 r' @+ S0 E: v - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
0 ]9 c! h |6 N1 } I# N% ^" w - <property name="prefix" value="/WEB-INF/jsp/"/>
6 P% A' B3 r; ~% o& o1 O/ x. ? - <property name="suffix" value=".jsp"/># O/ Z* _4 a, b" w. b) J- X
- </bean> -->& P; h/ T; ~1 Y( d4 b
- ( ~! M8 f: u' O( s6 e" ]4 ?9 x
- <!-- 导入springMvc.xml配置文件 -->
7 Z9 s ~1 s6 P& h - <import resource="classpath:config/spring-mvc.xml" />
p/ x' O1 f Q& W$ r1 ?8 a; m - <!-- 加载数据库配置 -->2 C4 t+ Q; H; d1 b1 g8 k
- <context:property-placeholder location="classpath:config/db.properties" />
3 a; v2 D' D h4 m4 c1 q - <!-- 加载数据源 -->
: Q# f; [- k2 t' M2 z2 p" l& J' W - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">; c5 s# K. x4 p+ z" P" E+ Y
- <property name="driverClassName" value="${db.driverClassName}" />
' u4 f" B7 }( G& Q - <property name="url" value="${db.url}" />
; V% r9 b9 \* B9 [, K) H - <property name="username" value="${db.username}" />4 j6 c9 O- E6 L5 [
- <property name="password" value="${db.password}" />7 N: F% R3 ?+ {" V2 N
- <property name="maxActive" value="${db.maxActive}" />' ^/ [& ^1 f5 @5 {+ Y/ ^( r
- <property name="maxIdle" value="${db.maxIdle}" />
$ j8 Z& W) r8 E- F# C4 c8 Z - <property name="minIdle" value="${db.minIdle}" />9 R5 q0 m( O" R
- <property name="maxWait" value="${db.maxWait}" />* t; m# e( C( {% [, E
- </bean>
7 w* ^; f1 w4 Y8 W \( c* { - , m# v5 D% b1 j: }
- ) u! b8 I { F* Y4 W: I
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> % p2 D8 q- p$ ?) ?( Q. [
- <property name="dataSource" ref="dataSource" /> : N1 o4 l8 }3 u3 @2 I( N
- <property name="mappingDirectoryLocations">
5 M6 l( {. F) a- Y, C2 q - <list> G* `% g+ a- w( f( u) p. D
- <value>classpath:config/hbm/</value>
/ y; R9 E) }4 e6 ^, g9 Z2 h - </list>
. V* Z; x4 X# m! l9 K1 I) R: B - </property>
3 _, l0 J& j- v! C/ p7 ?# V* ? - <property name="hibernateProperties"> ; w5 S. \/ T( H2 V( V& _2 w; `
- <props># [- x3 p, u9 e. q$ W6 \
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>& m; W. q4 m- l1 I
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
' f9 Q* E5 T6 m - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>1 _# E5 S% Z, R# o; N/ Q/ n2 X
- <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
- Z( {/ @* M$ @. a" k - </props>
% F. @0 m' [" V" c' m M9 h - </property> 2 o) l8 O( _) k( k+ ^3 |0 _
- </bean>
7 f# x3 H+ x+ _6 e$ l6 I" Q -
, m$ _$ {4 X( r+ k( n% }1 O - <!-- 声明式事务 -->
0 a$ n, B. I+ j1 l/ z& {9 A - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 3 B, g& W7 E3 y
- <property name="sessionFactory" ref="sessionFactory" /> : T5 a4 ]3 q+ L( Z- X- x; a( l
- </bean> : ~( R" \; t' Y$ h. ^
-
: r @* M# h* k. @6 c: p4 q8 F& s - <aop:config>
' r7 j5 k# Y. T r3 h - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
- z3 l5 }; w5 @4 @& \6 K- K8 |( ~: d - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
, j: ?0 w2 j7 _) t4 q# L* O - </aop:config>
6 V3 L! ?5 u" I* r* w -
' L3 \- t' _+ q) C - <tx:advice id="txAdvice" transaction-manager="transactionManager"> & l) l+ n! K! h7 w& V
- <tx:attributes> / W. w; D8 d: s0 u" S* j8 Q
- <tx:method name="save*" propagation="REQUIRED" />
/ e* p* o) y2 m) R - <tx:method name="add*" propagation="REQUIRED" />
; e8 Y6 w, B4 a( A" d - <tx:method name="create*" propagation="REQUIRED" /> ?2 r. m) ~# @/ Y2 P1 E( m2 L
- <tx:method name="insert*" propagation="REQUIRED" />
+ _8 y( H" Z$ Z6 x - <tx:method name="update*" propagation="REQUIRED" />1 |. E A% [# @& d
- <tx:method name="merge*" propagation="REQUIRED" />' T. X. I. p% Z; H, M5 |
- <tx:method name="del*" propagation="REQUIRED" />
I5 b/ b' P" O0 l& [ - <tx:method name="remove*" propagation="REQUIRED" />+ s# t, j9 }3 P: f7 H$ d
- <tx:method name="put*" propagation="REQUIRED" />* ]( I* r; O# h4 D: x/ r
- <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 0 I9 h6 }3 D5 G2 X$ `' \% u; Y/ h* z
- </tx:attributes> + _/ r; v4 E3 h1 }& R. t# `" Y
- </tx:advice>
3 C/ H6 k- M8 H5 z A3 s) X - </beans>
复制代码 spring-mvc.xml:
# e8 _+ d. x' Y9 G( Z1 S5 d+ _* ?, t
- <?xml version="1.0" encoding="UTF-8"?>
/ x( q3 N f3 b9 ], K - <beans xmlns="http://www.springframework.org/schema/beans"
3 f- P8 u( v! M" E& s9 z5 ^ - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"4 |& e. q% T5 I* Z
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
4 N H* b$ m- m9 x/ ~ - xmlns:mvc="http://www.springframework.org/schema/mvc": J+ A3 e/ ^: d- D8 c; \9 p2 E
- xsi:schemaLocation="http://www.springframework.org/schema/beans
, i+ l( i; D3 C, e& s2 s" r - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
! t: H2 _% ~7 f& w2 E. @2 {+ O - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd $ J8 ?. G' a9 X# S& c
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd , ~9 Q M) p9 w- h+ S8 ]
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd5 k! B6 w! x& I% g
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">9 j$ a. v5 ?; ~
- <!-- 自动扫描的包注解 -->
, F: o( S K9 p+ |' V - <context:component-scan base-package="com.hoodo.rci" /># j5 K2 B4 g7 e# p- J2 y6 @' E
- <!-- 自动注入 -->
( O; e* X' _. E+ C3 U - <mvc:annotation-driven />
' n$ p) H6 Y! S9 x; E - <!-- 视图解析器 -->, d8 ], k0 T! F; V7 ]
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
o. j" t N* C - <property name="prefix" value="/WEB-INF/jsp/" />/ n1 T% H. A% N1 c+ X
- <property name="suffix" value=".jsp" />
' H8 h; ]$ K( E* n: e- U' D - </bean>
3 i0 b7 Z" B E* N0 N -
1 h: M* |7 A8 _& _+ f1 O - <!-- 异常解析器 -->8 g5 ?1 I) Y& ^/ d$ Z& Y1 B
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
M. u% \0 X7 Z- z9 p - <property name="defaultErrorView" value="common/error" />% d3 d7 j% K! S0 D/ V' X" v2 }
- </bean> -->+ \8 a' \: ^# [5 z# P+ Y; g
- 1 R$ ~4 j r7 [4 F Y
- <!-- 拦截器 -->
: P A' W! b& ` - <!-- <mvc:interceptors>
: F1 u5 d& s5 N' n5 J - <mvc:interceptor>
( ~6 B7 }" `( |. t - <mvc:mapping path="/admin/*/*" />
5 F: c! B* E4 J# K - <bean class="com.filter.CommonInterceptor"></bean>0 J4 q" D& R& _$ P5 i$ ~, V/ }3 \
- </mvc:interceptor> `/ X. e1 I# m6 r6 A6 X8 X5 I
- </mvc:interceptors> -->
5 a% _8 Z! E8 [) A/ `9 @ - ( y% V5 q. Z' e+ P2 K3 g* `
- <!-- 上传文件相关的配置 -->: d( w2 E3 I* W. R5 p3 x+ e( p
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">" e/ `/ E4 Z) j# Y
- <property name="defaultEncoding" value="utf-8" />
, x, {/ ~6 {: i1 ?4 p, X - <property name="maxUploadSize" value="104857600" />
( N- n! |% X. K$ b( p w: w - <property name="maxInMemorySize" value="4096" />
7 ]+ b( v. k- |$ t8 t% Y% D- f, o- T - </bean>
' G% s; \( Q: c% I2 |. t - </beans>
复制代码
% f( U0 a. y+ O0 F0 j% H4 ^3 a8 p( x. J2 f" H: Y
8 j& k- B7 a8 A" m KSpringMVC+hibernate4.3+Spring4.1整合案例
1 J) s% W6 G3 Q2 M0 W+ h
5 h6 R$ d# w* f9 a# f& b: y2 U o |
|