TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:
9 S1 E i0 A% N8 ~& H/ a( x$ h/ G! `- ~ D( u: l
- <?xml version="1.0" encoding="UTF-8"?>
2 R: |/ I3 f |; ?! g - <beans xmlns="http://www.springframework.org/schema/beans"
% Z9 T/ j. ]5 M2 f, @ - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
# z, J' e3 c5 c5 m7 |7 Q - xmlns:p="http://www.springframework.org/schema/p"
& [0 a+ N7 E: A- `: t5 J. J1 Y6 ?4 s - xmlns:aop="http://www.springframework.org/schema/aop" / |! M r/ M U: U$ x3 ]
- xmlns:context="http://www.springframework.org/schema/context" g; d/ n1 r+ G% B/ T# s) C
- xmlns:jee="http://www.springframework.org/schema/jee" 6 M8 H4 ?! C5 R6 n1 S: r) Z
- xmlns:tx="http://www.springframework.org/schema/tx"
$ @0 y6 T2 E5 p( ^* d2 W; s - xsi:schemaLocation=" 7 U" C/ `2 d+ k1 |9 Z
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
( [8 H h2 @( N2 w0 ?! E$ p M" b. N - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd # I9 Q# x! J$ }; G1 g$ |2 t/ V
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
. u$ D/ M; K: r9 g - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
% r5 L1 F; `0 Y5 x- a' D- S9 z4 k5 n - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ' [- ]1 `" d0 j. V6 \" c. t
-
% K. U o( g" N/ e - <!-- <mvc:annotation-driven /> -->
6 |0 K& K0 \7 p0 [ - <!-- 【配置视图解析器】 --> 6 P9 u8 u/ ?. T x* }
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> 1 s$ w/ J K- h
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
3 a7 {$ K; f, j4 t, m5 C - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
: I3 n8 ?& }% l& Q. M9 j$ j+ M - <property name="prefix" value="/WEB-INF/jsp/"/>
. A: c8 X, K$ T$ W - <property name="suffix" value=".jsp"/>
, D5 _# A7 `$ Y2 J0 l {. [$ P" B2 C - </bean> -->/ H2 L- D7 O+ @% u2 f0 s
- + u/ t+ G7 o3 l! |
- <!-- 导入springMvc.xml配置文件 -->
) A( y/ l6 s5 f1 a" t: n - <import resource="classpath:config/spring-mvc.xml" />( Y4 \ ?% h1 L
- <!-- 加载数据库配置 --># p. _) S3 ?4 i
- <context:property-placeholder location="classpath:config/db.properties" />: J' l8 f' [: [5 k+ ]* x
- <!-- 加载数据源 -->
4 ~# ^: h8 F* Z6 S( y! v - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">" N, I: p2 W: |+ x5 Z
- <property name="driverClassName" value="${db.driverClassName}" />8 s+ l" @: t; `3 Q: }/ _ a
- <property name="url" value="${db.url}" />; Y. p* L) H( [/ ^
- <property name="username" value="${db.username}" />
+ W! ~( g# y: y: E* }" C - <property name="password" value="${db.password}" />
* g, F" _1 t- F/ J - <property name="maxActive" value="${db.maxActive}" />
: e, Z/ @4 W7 b - <property name="maxIdle" value="${db.maxIdle}" />& p* Y6 Y/ q+ \, O% Q6 I2 q1 O
- <property name="minIdle" value="${db.minIdle}" />& I, `- u( p6 ~- @* c
- <property name="maxWait" value="${db.maxWait}" />
& ]# Q% `9 C% w5 p( `, k8 ] - </bean>
$ A6 r3 g3 t8 j- R( |# u6 { - 4 B; X. g% s D. ^
- ) a6 F& T3 M1 y' b7 ]" v
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ( w' [- k5 C/ B7 E: y
- <property name="dataSource" ref="dataSource" /> : j- h. D- C* }5 E; X
- <property name="mappingDirectoryLocations">
3 X5 @# a0 m" [% X - <list>
: q& {' e: G: N, C; j - <value>classpath:config/hbm/</value>6 y c& [2 @1 x6 ]7 o1 F# b
- </list>, ~- m; I2 W/ }2 L" b( U+ l3 l
- </property>
( T X6 r+ d S6 E! s) C+ R - <property name="hibernateProperties"> - i9 r" x2 l! X# j* @: q$ @* Q4 U: q
- <props>
A6 w9 x( I0 Y - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
; P: p6 W+ l% c# ]8 \4 ` - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>! f5 Y" @1 G* U) D l, }9 F
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>7 d' D4 U/ e+ F
- <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>--># T* @8 \) s1 s2 q) L) r
- </props> . E* q2 Z" \0 W" I
- </property> ' H; O w" g2 p
- </bean>
$ ~6 r+ w% c8 B4 q1 F -
\1 `, U* U" w2 k - <!-- 声明式事务 --> & h; {; M. U. C R
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
& N, S+ w8 D8 h3 d' ~7 C - <property name="sessionFactory" ref="sessionFactory" /> : R! }8 r: Y3 |$ _* d6 Q' Q4 P
- </bean>
( U6 N7 A, S3 S - % v- A0 W* V. Y+ R' ?
- <aop:config> / B; R; ]2 d% b/ i3 T
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
5 ? ^1 X4 O# w4 N$ l8 F7 R - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
2 s8 V/ c% p& |% h - </aop:config> + j+ J! m0 T1 q% x) p0 v2 n
-
6 C3 u1 C9 }# W* Y; \ { - <tx:advice id="txAdvice" transaction-manager="transactionManager">
! b2 _3 V5 r) ^3 O* \/ ~0 a! [ - <tx:attributes>
( y2 L/ \2 v/ O/ o& E - <tx:method name="save*" propagation="REQUIRED" />0 k# B! ]; I( O2 O/ i$ m2 H+ c9 S
- <tx:method name="add*" propagation="REQUIRED" />. I, J+ Q% ^7 c! V2 _$ R& l
- <tx:method name="create*" propagation="REQUIRED" />5 W" @3 v4 L. @* _8 \2 }
- <tx:method name="insert*" propagation="REQUIRED" />! ]4 x$ v, U) l) u% u
- <tx:method name="update*" propagation="REQUIRED" />
& o+ V) {- |; t/ y$ F8 @5 i - <tx:method name="merge*" propagation="REQUIRED" />, c3 [- E4 G) }
- <tx:method name="del*" propagation="REQUIRED" />" Y( r+ B( P$ ?- M" u
- <tx:method name="remove*" propagation="REQUIRED" />
% ]4 }! Y4 x2 t/ X% \ U2 n - <tx:method name="put*" propagation="REQUIRED" />- I. y0 |; T( E2 ]! ]
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
' E$ k- E ~, K - </tx:attributes> 8 \; W8 Q# n, w5 R2 R7 F
- </tx:advice>
0 _" k; s; ?8 w' o - </beans>
复制代码 spring-mvc.xml:/ g- j" y7 E# f# D
6 s6 C- h! s1 J1 j
- <?xml version="1.0" encoding="UTF-8"?>5 N# F4 S3 D' z# Y! b x& V( E$ ^
- <beans xmlns="http://www.springframework.org/schema/beans"" |% [- i) o A# ^
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"7 S9 J3 t0 v: c% w. R+ ~
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
2 b/ }- [$ b0 R. B; n+ s - xmlns:mvc="http://www.springframework.org/schema/mvc"" v+ S# r" c- _, A
- xsi:schemaLocation="http://www.springframework.org/schema/beans
! H$ Y+ }- U3 x& n. j6 ` - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
! L* p- k* H) L* Y/ r7 }+ D; u - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ' ~+ o8 l2 L4 M) I& f: M3 S% d, {
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
1 B* t* ]. J( d B% i2 ? - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd* u, P- x: s2 O/ V9 y
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- ^- `7 u7 e% O7 I: \3 \ - <!-- 自动扫描的包注解 -->
3 j0 `6 B0 T# x+ J - <context:component-scan base-package="com.hoodo.rci" />7 x. C% @0 [) ?! x1 }, `) O
- <!-- 自动注入 -->
$ A& X2 D( O' n( Q - <mvc:annotation-driven /> [3 {9 Q$ S- j9 t
- <!-- 视图解析器 -->. g) S. v% B+ X, q. c
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
9 o) d9 |. v; t- g - <property name="prefix" value="/WEB-INF/jsp/" />& N0 X+ r. ~$ Z9 Z6 ~: y& R
- <property name="suffix" value=".jsp" />, j s! ]/ \, D: S7 l8 X
- </bean>
/ |0 ?6 {% n7 S8 d3 o* v* S6 M - % W4 X4 N _& H/ x; \
- <!-- 异常解析器 -->7 d8 H" `! X, c, B& D. V1 a4 F- `
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">( G T: I/ |! C8 O: Q; S {. L+ C2 z6 Y
- <property name="defaultErrorView" value="common/error" />+ O2 W% v* p& S: |% k& I: `
- </bean> -->1 V9 M8 g" C3 m$ A4 m. h) t) I
- ! I( P- B; h7 N9 N+ ]
- <!-- 拦截器 -->6 G2 i0 b' |( \9 H' `4 v/ ]( H
- <!-- <mvc:interceptors>2 b- g7 X* v2 l5 @+ l% z
- <mvc:interceptor>0 _ M6 m7 [# s2 w
- <mvc:mapping path="/admin/*/*" />) D2 l" S5 k) ^% u, ^$ o% A( {
- <bean class="com.filter.CommonInterceptor"></bean>. H; H( B2 c% K, O
- </mvc:interceptor>
' d( b. c' e: K6 \; P0 p - </mvc:interceptors> -->. w6 I) n& ?# x& Q% I' ? O
6 T; h. S4 d$ l- h- <!-- 上传文件相关的配置 -->5 w4 h; P; h5 T# L: b
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
* \3 S( J2 j$ Q w C4 ^) J - <property name="defaultEncoding" value="utf-8" />
$ z- U& N ]6 Z5 F$ J! p5 O - <property name="maxUploadSize" value="104857600" />9 w7 O( E4 |5 R7 M
- <property name="maxInMemorySize" value="4096" />6 _: U# h0 G, h, W5 q ?0 r1 [( m$ T
- </bean>! Z k2 z6 o, m. R7 }+ `: J
- </beans>
复制代码
& g8 Y$ z1 l7 R
8 K; s+ c* M- `6 ~- o) A; k
/ T1 x' f* m* d' K' H7 eSpringMVC+hibernate4.3+Spring4.1整合案例5 Y; c) z3 p7 I7 T, X l
1 ~7 e# L4 A9 Y# F4 Q |
|