TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:
. @0 M A; F9 g& S1 Z0 K, ^) X% \% J5 M
- <?xml version="1.0" encoding="UTF-8"?> 8 ?& I$ G) G, u
- <beans xmlns="http://www.springframework.org/schema/beans" % U+ i; B2 d2 D
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 e! [4 p: Q0 A" X! Y6 z2 i - xmlns:p="http://www.springframework.org/schema/p" 7 b6 J/ V4 x4 z) L/ R# J+ V0 v( g( g
- xmlns:aop="http://www.springframework.org/schema/aop"
) s5 y1 ]8 q# G. Y - xmlns:context="http://www.springframework.org/schema/context" x) M+ k" I0 x8 y3 X4 L6 y! |
- xmlns:jee="http://www.springframework.org/schema/jee" Y. Z2 ^/ j6 u3 B6 v8 @' Y
- xmlns:tx="http://www.springframework.org/schema/tx"
+ ~0 k$ l) c' F2 {1 y - xsi:schemaLocation="
. P7 m0 S7 d4 Z/ ?, H+ l. U: P - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
4 I9 M9 N% j# z# X3 \: ^ - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ; E$ k: p- q; g/ C# o" U' Y `! M
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ) E8 t0 x1 e. G4 y
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
& N/ x k/ D' Q/ o3 _) \ - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> " p. r G7 l4 g: a/ u0 ]
-
* H5 R7 m: m7 X+ h q& d - <!-- <mvc:annotation-driven /> -->
4 e6 d8 w9 i2 r- `, @& | - <!-- 【配置视图解析器】 -->
; S! |. _; h, S* v5 o2 C( { L8 w8 z - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
( e3 _1 P( r1 L$ q1 D) W - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> 8 v6 b3 s7 K: \7 {$ p, j
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
n! g7 U) V7 i* _ - <property name="prefix" value="/WEB-INF/jsp/"/> " K8 X! l4 P5 H; z6 \! F7 R
- <property name="suffix" value=".jsp"/>
& i# g) N% d0 ]# b! f" X - </bean> -->% Z' y7 I# I! D/ [
- s$ |# m+ k- o2 v+ Z( J! O' l. d
- <!-- 导入springMvc.xml配置文件 --># N/ X# A' I4 H3 N( \
- <import resource="classpath:config/spring-mvc.xml" />; \" w2 B& d H! ~
- <!-- 加载数据库配置 -->/ e2 L% U* G3 L6 d6 E' N- J' a' ]
- <context:property-placeholder location="classpath:config/db.properties" />. H% A/ n3 f! q; F4 @
- <!-- 加载数据源 -->$ A r9 i* z4 c* Y
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
0 t3 O0 ?% s# R. j4 g N1 l - <property name="driverClassName" value="${db.driverClassName}" />
/ a- O! O5 \* t# M. r S B1 Y - <property name="url" value="${db.url}" />
- p8 w' F0 c0 w& D1 R1 ]/ u - <property name="username" value="${db.username}" /># W: ]1 D3 m# d Y; ~3 p7 f2 V/ w
- <property name="password" value="${db.password}" />7 s3 s1 P; K" H1 \: R) Z
- <property name="maxActive" value="${db.maxActive}" />& x/ t5 n# o( R; Q, d, v
- <property name="maxIdle" value="${db.maxIdle}" />
6 h8 } w1 g y9 V8 d - <property name="minIdle" value="${db.minIdle}" />
7 z$ A! f3 Y% P1 Y - <property name="maxWait" value="${db.maxWait}" />+ Z. _* G3 u% `
- </bean>
. Q" [- n4 v* P F/ V3 f -
3 u" T' L8 N; t' }8 e -
/ M" K2 f m$ {( Y) T - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> " M1 {9 g1 C* a' D* n) U" T" Z
- <property name="dataSource" ref="dataSource" /> 5 r6 J+ F+ C4 i6 E
- <property name="mappingDirectoryLocations">
9 d+ x: u0 Q1 I: E/ F - <list>) T+ ?7 h. J* S& P0 d" J9 \* F
- <value>classpath:config/hbm/</value>
/ C$ S2 g& @- }$ ]- d1 f2 q2 g - </list>
3 s( T% k- d8 }; T# _, y - </property>
( j* y5 s0 p' k* ]$ A* \! g- E - <property name="hibernateProperties"> - M: W3 N* U- `9 b) X: ]
- <props>
5 w7 ?+ L' a/ {) \" G - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
8 S @, c* E6 J2 I, r& q1 b+ u - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>: H0 @/ L f7 T( U/ W
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
/ x; o& l( g+ H% F9 q# o% \ - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->' z, g3 y7 z& Q( s% H
- </props>
' A* w! D7 }) R' m+ r" g - </property>
9 t/ y5 ?+ H! Q2 S/ ] - </bean>7 }' Y1 n: v+ F
-
. Z/ n- b# H4 t4 v/ N" k0 {; | - <!-- 声明式事务 -->
8 u8 A( x7 ?, r& _4 l0 \ - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> $ D/ X2 e7 J6 z; A
- <property name="sessionFactory" ref="sessionFactory" /> 1 e* F$ c: N' W% I! T
- </bean> 4 d9 H; d3 a, }$ V7 x& }
-
# f- x5 y/ I# i1 X3 Z - <aop:config> ; L( F+ X0 Q4 e) A. l! w6 J
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
) Z$ c0 F+ J9 ~5 r2 M& e5 v6 \ - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
. ?6 w( E- V) U( g" j( h, s& j - </aop:config>
9 S4 B }3 T5 ? - + W# y, g+ D& X8 d- C/ `' l. C
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
1 c9 l1 K! L; k - <tx:attributes> # R% u! m# n/ H9 l1 S, _
- <tx:method name="save*" propagation="REQUIRED" />
6 {/ @6 s8 f+ G9 I# Y' z - <tx:method name="add*" propagation="REQUIRED" />: e7 F ], e3 r. O6 Y
- <tx:method name="create*" propagation="REQUIRED" /># B& V8 k2 F& F9 b5 |
- <tx:method name="insert*" propagation="REQUIRED" />
* v/ N B3 m3 o/ \! I; ?+ R O - <tx:method name="update*" propagation="REQUIRED" />% U& g$ q" N4 \4 I: F r# n
- <tx:method name="merge*" propagation="REQUIRED" />' ]7 Z& u. Q/ y3 v& r9 A) F
- <tx:method name="del*" propagation="REQUIRED" />
, o0 o1 y0 J) ~ - <tx:method name="remove*" propagation="REQUIRED" />, p8 A; K8 S7 B6 o8 [
- <tx:method name="put*" propagation="REQUIRED" />7 t7 h, b- c. m% [' r* f
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
8 R7 M1 u( n5 |7 |( u$ ~% Q - </tx:attributes> P/ i: c3 m5 U6 d0 _7 Q4 C6 j+ X
- </tx:advice> [2 T6 X0 M& ]% Q5 y4 P
- </beans>
复制代码 spring-mvc.xml:
' X: l- J5 V7 f0 P& r
) \3 n& V: _( f. {5 k$ t# j0 W4 I9 G- <?xml version="1.0" encoding="UTF-8"?>
3 w$ v+ l# ?, s- f9 D0 `8 G - <beans xmlns="http://www.springframework.org/schema/beans"
- F% u. }) K* a, M8 _ V8 `; m - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"3 q; T9 X o6 u1 _7 K: m' X, k- ^" J
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
. c) Z( z! U+ q& V) s9 d0 C - xmlns:mvc="http://www.springframework.org/schema/mvc"
2 m) f7 y. p5 ?! a* { O+ V - xsi:schemaLocation="http://www.springframework.org/schema/beans
' `7 _( G5 Y$ }/ X; ~" k- z - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd / j& f( \1 b4 s! q4 C9 P7 U1 }: |2 e
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 5 ~- {, J) a e6 B4 L
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
% _4 G7 Y% [' @1 G, w0 q3 ?! [ - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
. c. r4 H e- X8 h% S* q - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
+ R1 B% R: N- g4 h. D: s3 f/ e - <!-- 自动扫描的包注解 -->
4 H w# C4 x+ _% d( n j- J - <context:component-scan base-package="com.hoodo.rci" />
" ^+ G! u+ Z- ]0 R- s+ y# z# u - <!-- 自动注入 -->
8 ^4 {4 \/ `) e. Y: m7 k - <mvc:annotation-driven />
3 j$ W5 q3 S! I4 m - <!-- 视图解析器 -->
7 F* U5 i I k* ? - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
1 m: ?# P) y$ \ - <property name="prefix" value="/WEB-INF/jsp/" /># ?4 C8 E% g" i: O
- <property name="suffix" value=".jsp" />, L2 j \- c( h1 T3 S* g7 s
- </bean>/ x9 r' w! v/ X5 F; ?
- - {1 n( V! Q3 {
- <!-- 异常解析器 -->
. B# j4 P: `9 { - <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"># Z4 N7 Q; `, O# u+ L+ [9 ^# [" d
- <property name="defaultErrorView" value="common/error" />. i7 ?6 O9 f1 w, L1 Q' o( p
- </bean> --> L0 @; ?* R$ J
- 4 i- h9 H9 z: z
- <!-- 拦截器 -->
) _) [4 t3 U5 i7 }" _& ? - <!-- <mvc:interceptors>! X4 j$ {: @+ D9 ~( H( ~! a9 g
- <mvc:interceptor>
8 v6 K) A, f- w; k& K' P6 _ - <mvc:mapping path="/admin/*/*" />
( n; b- N( w& V* n; @ - <bean class="com.filter.CommonInterceptor"></bean>; }4 Z& C& i0 Y% `5 b5 A
- </mvc:interceptor>
; d6 z1 ^6 B l; a$ X: W! j - </mvc:interceptors> -->
* P8 h! c( c6 g5 |+ I" ^
0 i3 r5 ^. t5 ?8 W0 {- <!-- 上传文件相关的配置 -->( L( h/ i7 s1 b
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">+ H; C$ l# ~" @! V4 N, c
- <property name="defaultEncoding" value="utf-8" />1 }6 T# W! M6 |5 @1 A* o' W( l6 d9 h
- <property name="maxUploadSize" value="104857600" />' N" l3 r6 K, T. i, n: e
- <property name="maxInMemorySize" value="4096" />$ V$ ]0 @) b- q q* |) t9 @" c
- </bean>
3 o9 H. ]' }8 y! J' h+ i4 z - </beans>
复制代码 0 z9 H" t8 r8 Z2 l3 ~$ H" d
/ P; ?1 I1 F* e+ t. S$ f; Y1 o2 y9 J8 y6 V7 z2 p0 r! C
SpringMVC+hibernate4.3+Spring4.1整合案例
# P8 W% M& ~/ c6 d" [8 e1 y; \; a
( Q8 w3 s; J) q1 k( `, q! s' u |
|