TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:1 f% t7 d, R w, K2 b; Q
) S6 I* N: j9 A6 Z- <?xml version="1.0" encoding="UTF-8"?>
6 H$ t3 Z, k6 y4 F9 L) ?( L6 b - <beans xmlns="http://www.springframework.org/schema/beans"
2 M9 }( s$ R% O: o& N- U) c3 S3 P - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ( J4 H K' {. s ]
- xmlns:p="http://www.springframework.org/schema/p" + B9 `+ J" m; [2 d! N7 |
- xmlns:aop="http://www.springframework.org/schema/aop" " B" F( y6 |' Q; n2 I% ~& d
- xmlns:context="http://www.springframework.org/schema/context"
( V( ~* M: C) S4 M: b - xmlns:jee="http://www.springframework.org/schema/jee" , f, e9 w5 y3 i, u
- xmlns:tx="http://www.springframework.org/schema/tx" , ^6 a5 [" o$ \/ w* R
- xsi:schemaLocation="
; _ e% v3 [6 [ - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
/ g' F4 G. x, Q$ o! M2 E6 O - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 0 \( [: f8 {6 l5 Z6 ]9 M4 [6 I
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
, \ U& J/ C# P5 ?: c - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd ! Q! j3 K3 W( E, j2 t
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
1 H/ v+ \1 K' i, G" X. j' ?) t1 h -
8 T% u" M& s) K; Z: s8 h - <!-- <mvc:annotation-driven /> -->
/ \2 f, e# {4 V/ g9 U9 q - <!-- 【配置视图解析器】 -->
$ v* Z' w2 S! h6 ] - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
+ n( ~1 i& R0 o: V$ H2 N - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> $ T T- }! m# p5 }% y; |
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> # ]1 c. }) C4 e1 d6 F4 D" p
- <property name="prefix" value="/WEB-INF/jsp/"/>
" I; U0 j7 e% q& n8 A( } - <property name="suffix" value=".jsp"/>& x+ K8 e3 s0 o: m$ I
- </bean> -->
5 ?7 B( K' Q! m4 Z5 e8 M - : T4 f" q( e0 h
- <!-- 导入springMvc.xml配置文件 -->& f h _ G+ s4 o% @4 r5 A2 V
- <import resource="classpath:config/spring-mvc.xml" />
; ~/ }* H9 e4 U( w# [ - <!-- 加载数据库配置 -->
$ d) f8 ^; m% k; X - <context:property-placeholder location="classpath:config/db.properties" />
7 V; N y5 W' x7 U( t - <!-- 加载数据源 -->
. X5 N: B# x3 u8 u w& {! ~" p) H7 X - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
* S; o1 M4 G/ {' _5 l1 H- { - <property name="driverClassName" value="${db.driverClassName}" />6 s% [; @8 q! P) j4 y, [0 V; b3 a
- <property name="url" value="${db.url}" />" U6 A" E) j& J6 @# L6 k
- <property name="username" value="${db.username}" />
/ a. W& R) \3 ^* F: \ - <property name="password" value="${db.password}" />
( z S; y+ ~; ~' |) c* z$ U/ c ? - <property name="maxActive" value="${db.maxActive}" />
3 R% C8 ]) U" b8 x - <property name="maxIdle" value="${db.maxIdle}" />
1 R! c/ x. @8 D - <property name="minIdle" value="${db.minIdle}" />
3 |5 c0 D F' u1 D- ~2 i0 V- [) z; U - <property name="maxWait" value="${db.maxWait}" />% b) n' k4 f/ ]& N
- </bean>0 ^ L; q9 h2 l( ]7 e5 F# J
-
% V; D: { I# i' J) P -
6 z/ o: W9 w9 n+ d- @" k - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 1 b7 L7 r' v% m; r. a
- <property name="dataSource" ref="dataSource" /> 3 s, q' O6 }9 P3 I- V
- <property name="mappingDirectoryLocations"> 3 H% j- [7 b* v4 \) R8 j/ Y
- <list>( L0 e3 T# }& o- O% J1 {
- <value>classpath:config/hbm/</value>) w; a" h3 f: F) {$ l# a7 ?7 I1 B! A
- </list>. O5 X$ b) ~9 C+ `8 `
- </property>
3 ]8 K: a; ]6 u$ o) P- B - <property name="hibernateProperties">
# ^; K( k+ g7 {- p3 ?" N - <props>
2 q5 n. Q. T1 [ v# B9 y - <prop key="hibernate.dialect">${hibernate.dialect}</prop>" e6 Z; z$ U! T
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
" q4 S! P/ l- H9 O. t; } - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
1 |9 c5 h3 e! _ - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->; M4 y1 g6 C" u& D6 c
- </props> 6 P6 @' `2 K4 _% ?- w3 s/ K, S
- </property>
' q: Z; s+ Z l% @+ k' f: @2 D( b - </bean>
y4 ]% E; Q7 Z) z+ d - * _2 t7 P5 X$ ^4 N/ U) P0 |
- <!-- 声明式事务 --> 5 B3 T, @$ Y3 u% f0 Y% N1 x
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 2 L3 j8 M4 M. X1 x5 e- U: z
- <property name="sessionFactory" ref="sessionFactory" />
" P- j/ v3 W; c9 c9 D1 E0 j3 P - </bean> 1 u& p1 E4 g1 D* m/ I! j% ? ~
-
1 e; p8 p4 N7 b& F$ `$ x - <aop:config> ' a" P; z8 V# v7 A
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" /> 7 t0 U( q/ g; ?: q4 s D
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> + r- E" H4 g# u. D* m' k; L
- </aop:config>
* \+ f( h: Z9 }7 _ - 5 D& h3 e* z3 V
- <tx:advice id="txAdvice" transaction-manager="transactionManager"> ) y1 D l) c6 \, ^1 }' H
- <tx:attributes> ) {9 x# F2 J1 K2 C' q, K) @
- <tx:method name="save*" propagation="REQUIRED" />5 @; M8 D/ \5 P+ Q8 J3 S( Z5 p0 f
- <tx:method name="add*" propagation="REQUIRED" />% m {7 U+ h/ v. h- w& ^! H: q
- <tx:method name="create*" propagation="REQUIRED" />2 z0 p1 Z/ a$ H2 c2 E+ J; u! l* C$ o" S
- <tx:method name="insert*" propagation="REQUIRED" />
1 W N; S! I4 t2 T - <tx:method name="update*" propagation="REQUIRED" />* S! O8 v! p2 j4 F* q8 R% Z c
- <tx:method name="merge*" propagation="REQUIRED" />, D/ K% K, @/ z$ x* j. d# y3 ~
- <tx:method name="del*" propagation="REQUIRED" />
" y( p: f: D- ^* i3 S - <tx:method name="remove*" propagation="REQUIRED" />; n% e) V3 \6 U5 }( o/ P" f
- <tx:method name="put*" propagation="REQUIRED" />) B0 Y5 N0 _; U2 {) ]
- <tx:method name="*" propagation="SUPPORTS" read-only="true" /> - q$ j& P1 [& |% b8 N
- </tx:attributes> 3 t3 G$ e3 ?) y* x- N
- </tx:advice> 0 S1 c! ^- h, w. e
- </beans>
复制代码 spring-mvc.xml:. S" b& g" `! z8 M
7 l; t% c) H/ U8 k3 w! O- <?xml version="1.0" encoding="UTF-8"?>! a- Y$ v; y! Q. C9 c+ E
- <beans xmlns="http://www.springframework.org/schema/beans"
$ q7 K& t* z; g1 [5 O - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"' ?7 W0 e9 G* p W0 y
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"/ M4 w( h( r, E- a- l( w0 }* ]
- xmlns:mvc="http://www.springframework.org/schema/mvc", t. c+ y/ @ ^9 v m$ \
- xsi:schemaLocation="http://www.springframework.org/schema/beans * Z0 I; T* D$ S" Q$ n" @
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
2 M6 d0 P% q* d3 {2 b! w% A - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ! T8 K- e0 t' z; D% w9 l
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
8 @) x/ w# E2 W4 | I - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd: o- a8 z3 }3 M3 U
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">+ a' {- g3 r& R
- <!-- 自动扫描的包注解 -->6 L( S4 S9 N2 @0 z7 c
- <context:component-scan base-package="com.hoodo.rci" />
* G4 L2 U9 T. H1 D$ _- h* o - <!-- 自动注入 -->
4 b# E7 W+ t! G9 Z6 ? - <mvc:annotation-driven />
6 [0 q; @. G% v u q0 ~ - <!-- 视图解析器 -->7 a+ f2 D$ Y4 Y
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">1 y0 P2 T4 J6 z% ?4 ^; o
- <property name="prefix" value="/WEB-INF/jsp/" />
$ |' {) H5 l0 o - <property name="suffix" value=".jsp" />/ W7 A1 x2 S2 q
- </bean>
/ H" ^; G1 O8 i4 J/ g - & Y9 o& [# }" I, b/ C' Q) j
- <!-- 异常解析器 -->! I7 x# K1 ~) q' ^2 f
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">: c/ c. X9 o) L6 i2 @
- <property name="defaultErrorView" value="common/error" />7 A+ i: C- f# } B! D: i
- </bean> -->6 H" ^8 J, ]/ N- x8 |( w
- 4 c& `# ^. N# X3 S$ g3 G
- <!-- 拦截器 -->5 \4 k1 [0 K/ I" d3 S
- <!-- <mvc:interceptors> S" `/ }- w) c; \% g
- <mvc:interceptor>* ?/ P; y4 t" u- e
- <mvc:mapping path="/admin/*/*" />6 g2 N( E0 a. F& S7 C4 } I" o3 H
- <bean class="com.filter.CommonInterceptor"></bean>9 K7 b Z1 J8 p, J. t3 K/ T$ ]
- </mvc:interceptor>2 S5 G7 a# P& W1 _: P" A4 z W
- </mvc:interceptors> -->
8 |; M# r9 a! Z6 e
7 V$ e1 u: Q7 _: X7 S1 d- <!-- 上传文件相关的配置 -->" J( `' n: c# t6 j6 t0 T$ k
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
1 ^4 i7 K# h/ z9 U' @; ?# w, ]* f - <property name="defaultEncoding" value="utf-8" />
6 `; ?* p) w$ N+ l% q - <property name="maxUploadSize" value="104857600" />
+ W0 X a" H) y% E" i m0 k - <property name="maxInMemorySize" value="4096" />
$ j4 y5 w9 X/ r& B( x, Y - </bean>
9 u2 N1 {, x9 x+ s! Q% ] - </beans>
复制代码
1 M$ L4 |1 z" U# H3 {6 i
# w: Q/ Q3 k" B+ Z
. a) H5 l9 O+ h# R ?SpringMVC+hibernate4.3+Spring4.1整合案例
# x9 @1 D( p. |$ x5 R5 b; }. z3 u0 p# n( [. @- @
|
|