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:# t2 U: o5 j, v% a+ O, e, q$ k
- `( @6 J# ]5 t2 q' [- <?xml version="1.0" encoding="UTF-8"?> 9 |% E# u# C6 e# F% p- j
- <beans xmlns="http://www.springframework.org/schema/beans" 6 r" e& c5 }7 N) |& W
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - }$ F. r5 j6 W$ \6 N
- xmlns:p="http://www.springframework.org/schema/p" 7 F9 f" e+ u4 H3 p5 n- L
- xmlns:aop="http://www.springframework.org/schema/aop"
9 F! @9 H3 [2 C& s) S - xmlns:context="http://www.springframework.org/schema/context" 6 p1 r: T2 E$ J4 `8 l
- xmlns:jee="http://www.springframework.org/schema/jee" ; Y# o* w" I7 {! t! Z
- xmlns:tx="http://www.springframework.org/schema/tx" 5 ]# P P% E$ A7 V$ x" N
- xsi:schemaLocation=" 1 \) M4 m y5 F! D4 b) o% O3 ~# T, s
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd $ i! t3 {1 h$ D2 A7 I
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 0 T$ Q: ~- T) D/ t
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
. A( ?8 g1 @) u) C% W3 e$ h- s - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
6 a- k9 G, S' b! R' ~1 H. x - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 9 @" g/ ?+ _; T) n5 Y
-
% V0 }) [8 r0 c0 b. z - <!-- <mvc:annotation-driven /> -->
8 A' a2 G/ R K" {( s# M - <!-- 【配置视图解析器】 -->
. v: \- B. T" H% ]! B - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
- m6 `2 f o" J& W F! w8 o" E% ^" } - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> 5 j/ x, U; i' l3 ?2 S
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> : O8 x% x/ \/ Y7 b% G' L# H Z- _
- <property name="prefix" value="/WEB-INF/jsp/"/>
+ A7 h. ~4 K; |& a& y3 s9 ` - <property name="suffix" value=".jsp"/>
: S u2 A7 i; V, l8 L3 A - </bean> -->* n5 ~7 t3 ~( k* J
- # S, I$ q2 B' } ^" M+ U
- <!-- 导入springMvc.xml配置文件 -->
2 S1 y0 [+ Z; w8 {0 r - <import resource="classpath:config/spring-mvc.xml" />
, j/ _" n6 r# \2 x! b( q$ T6 k; b - <!-- 加载数据库配置 -->& }% D) s. U1 k; e' ^+ Y
- <context:property-placeholder location="classpath:config/db.properties" />5 m; X( m, V7 d s
- <!-- 加载数据源 -->2 \ z! ?8 A" X- @1 x v6 \
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">6 V" r- f+ _( s" J# N0 |
- <property name="driverClassName" value="${db.driverClassName}" />
2 z+ o4 D% C6 A$ n7 x: F" w - <property name="url" value="${db.url}" />
! L y [, L3 Z+ a. L1 H, J! D - <property name="username" value="${db.username}" />
% n$ C1 I+ `& ]& H* }/ p - <property name="password" value="${db.password}" />
+ L9 v4 e! H0 p: `/ i1 S4 Y - <property name="maxActive" value="${db.maxActive}" />
! W/ k7 C2 S0 Z# T - <property name="maxIdle" value="${db.maxIdle}" />0 S4 b5 ]# n: j; M0 h% G. f8 H
- <property name="minIdle" value="${db.minIdle}" />% t) Z. s: M4 Q6 M3 i
- <property name="maxWait" value="${db.maxWait}" />4 A& a; k" z6 y( I" O q7 X
- </bean>/ h2 }0 H- d' U9 Q2 o" r2 N
- ; ^& H& W0 ~5 O# \- |9 C+ P% f' X
-
G( I6 v. k8 }: L" C - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> % X- A' u; S7 v$ k' S l
- <property name="dataSource" ref="dataSource" />
* w* n5 O! y* w* Q - <property name="mappingDirectoryLocations">
2 |7 x9 G/ `' F* Y& O# Z% Q - <list>0 x; w( Q' ^2 d1 M% t
- <value>classpath:config/hbm/</value>; n) H0 O3 D/ r5 ]
- </list>
2 \$ ?+ p. I6 X' I# y& Q - </property> ) ~; w- e' O% v1 t4 h' H& Q% k$ E* V
- <property name="hibernateProperties"> # z+ r" R( a: ^. E% d
- <props>
, O) @! C* I/ x, b7 O4 w. n1 n6 V, x - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
) F. |( @9 n5 b6 P - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>1 F4 M) k) A O* ]4 ~' w
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>, _. E# Z# A1 J, i
- <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
0 n9 v; }7 m/ X/ ~ - </props> 2 A: W* D. W# `2 _2 T
- </property>
- N$ g2 h* }; w8 a - </bean>. _( l' N7 o& v9 H( n
- ) ?+ [* W6 J8 Z/ J; n+ v
- <!-- 声明式事务 --> : g; q* o8 X6 K. W i
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
8 }8 t- {: q6 q - <property name="sessionFactory" ref="sessionFactory" />
, ^1 D3 h% ?+ d) D6 F+ k - </bean> * q, s& m* R: f! e, N4 m! S/ H
-
( ]( e( u1 Y: } - <aop:config>
4 }, l6 I+ w# d# _ - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
/ l; O1 x% |0 X [0 M - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> ! v) ?% }) H, n- c# z, {
- </aop:config>
2 J* D) p5 d5 D5 d) r3 ^ -
: S2 I0 L0 _) n( x* ?' ? - <tx:advice id="txAdvice" transaction-manager="transactionManager"> " M- {+ e; R# `% {4 }4 Y
- <tx:attributes>
# J' V8 r; B1 V8 N; ^ - <tx:method name="save*" propagation="REQUIRED" />( O5 h, N9 f8 G5 l9 A
- <tx:method name="add*" propagation="REQUIRED" />
. Q: [6 h5 o" b - <tx:method name="create*" propagation="REQUIRED" />* \- L# B; G. S9 t5 Y+ B' A* f0 X
- <tx:method name="insert*" propagation="REQUIRED" />
: u6 Y M& \" F( E. `7 K - <tx:method name="update*" propagation="REQUIRED" />
% |$ ]" ?% p ^! V0 d - <tx:method name="merge*" propagation="REQUIRED" />( s1 h# ^6 G2 r/ A
- <tx:method name="del*" propagation="REQUIRED" />
5 D+ U$ `% U* e3 @. o9 V - <tx:method name="remove*" propagation="REQUIRED" />, k6 T+ M( v& f! K& D$ V* G7 U( {9 u
- <tx:method name="put*" propagation="REQUIRED" />
3 Q" O# m3 R p. I" l - <tx:method name="*" propagation="SUPPORTS" read-only="true" />
J6 {; b" i6 l5 N `6 s/ { - </tx:attributes> s3 |. `2 g4 z1 b
- </tx:advice> : b8 `3 _3 C0 [ f/ j2 D5 a: f& Y% |
- </beans>
复制代码 spring-mvc.xml:
! g) g4 C: g% N) E. F- n" B. e0 w$ W8 G) W. F
- <?xml version="1.0" encoding="UTF-8"?>
p) l) x: \) A. z5 D8 d* D( K - <beans xmlns="http://www.springframework.org/schema/beans"
' a7 Q; N; ~4 f U0 s4 s; m$ D D - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context") C. q3 d; j; H/ q" D0 X
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
6 F0 h" u$ |( X3 Z% M - xmlns:mvc="http://www.springframework.org/schema/mvc"7 y2 |. i) K/ X3 p$ }1 z
- xsi:schemaLocation="http://www.springframework.org/schema/beans
! M- g O9 _9 G+ T- V) _7 d. W - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
, x3 E8 g/ G0 @7 G) p1 P D - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
0 z! N7 H9 U! j - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd / R% \; O, @+ J2 A# z. Z
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd) o' v5 N0 x: J* u; o. X# J
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">4 g4 _7 o' x# M( E7 T* }
- <!-- 自动扫描的包注解 -->! `; u, F6 O1 J. V2 `
- <context:component-scan base-package="com.hoodo.rci" />& o8 G$ i' k7 C+ J; `* w8 b
- <!-- 自动注入 -->
" c( j, [0 }' e( D1 \ - <mvc:annotation-driven />4 V: ~( `+ [7 ?$ v3 v- J' }* s
- <!-- 视图解析器 -->: E. x' D% p' Z/ R& j4 J) W. C4 ^( S
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">) V; }4 H! x0 A5 ?7 n. Z2 L$ D9 \
- <property name="prefix" value="/WEB-INF/jsp/" />4 O6 r+ _% d: `' r" _, H
- <property name="suffix" value=".jsp" />5 t5 }0 b( J3 q. V5 `
- </bean>
* q/ c1 L8 l7 [5 ?7 A) n* G - ' `7 L E. @( I
- <!-- 异常解析器 -->2 P) P1 H) I* {$ V8 v# _) Q, Q
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
% L0 i# g( s+ s - <property name="defaultErrorView" value="common/error" />% z- n5 D0 t; V! ^2 V
- </bean> -->9 m2 F7 N8 d; {( }, F% [- m2 J
-
3 `- `; ^/ h2 a( |% n& { - <!-- 拦截器 -->
% U1 N: p1 X6 r! b - <!-- <mvc:interceptors>+ ?4 Z% p% Y% S$ ^$ S# B, ?$ ]
- <mvc:interceptor>: A4 ^! E* N" C/ z9 G
- <mvc:mapping path="/admin/*/*" />7 Z T7 M! u* M2 b! u) n
- <bean class="com.filter.CommonInterceptor"></bean>9 Z/ S* t* X1 @1 a/ W+ R
- </mvc:interceptor># K4 ?- H$ P; Q+ q8 ?6 y% k
- </mvc:interceptors> -->4 w- s" l! E; e
/ U/ H# z- t: J: S, g7 D- <!-- 上传文件相关的配置 -->
6 _$ Q5 ?& q7 v* q* ~# |% p - <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">( U& K" _' Y& g/ ?1 g O! Y
- <property name="defaultEncoding" value="utf-8" />
, I0 X0 A& q3 l5 c3 ~5 z - <property name="maxUploadSize" value="104857600" />
/ N, p9 \5 A8 m: y4 ~5 ]+ d - <property name="maxInMemorySize" value="4096" />
$ _( B0 I$ ^2 l* C( ]" d2 n5 T - </bean>
$ {9 O+ X6 d. j+ q/ I; r/ Z - </beans>
复制代码
. c7 g( _* C+ j3 ]. M4 k+ n8 p- v4 A
7 W* B6 d L2 p) @8 x3 t6 B# s/ |5 }! R) p
SpringMVC+hibernate4.3+Spring4.1整合案例
* O; z* C7 B# Q9 ?
- B/ s* ~( k" R% y. `1 [4 a |
|