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:, O* A; H$ {9 f4 m4 T; _
/ \% A% Z3 e T9 X
- <?xml version="1.0" encoding="UTF-8"?>
! ~4 M5 x6 v$ t% x7 U - <beans xmlns="http://www.springframework.org/schema/beans" ' S9 U9 z/ ]5 ?
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
, K0 | i. \1 [' t9 f# `, Z - xmlns:p="http://www.springframework.org/schema/p" $ N6 r& ]0 @0 a( F
- xmlns:aop="http://www.springframework.org/schema/aop"
4 S+ G7 N2 m3 O* g, \' T - xmlns:context="http://www.springframework.org/schema/context" 2 m: H) w# O( l o) w
- xmlns:jee="http://www.springframework.org/schema/jee"
" c! n0 B6 r! Q! e: K - xmlns:tx="http://www.springframework.org/schema/tx" 4 p* R# T7 m0 A$ l
- xsi:schemaLocation=" 2 d$ {" V6 s2 B2 R" e8 x
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
7 u# j: B3 l: O" p) Y8 G - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
3 Y u: r( ]) j4 A7 r - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 5 t' }9 J u8 T- R
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
3 I* m" _5 O. R( V+ r: T5 ^ - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> " {1 p1 a. G# ~* |3 c
- ! g. z/ g a( w. Q# L0 D
- <!-- <mvc:annotation-driven /> -->, E h7 ^/ Q, Y; T0 b
- <!-- 【配置视图解析器】 -->
& ]# O* _( f2 o, j8 K3 }. }5 P - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> * e- B2 [0 t( e" ~ k4 w4 z
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
% l3 s- ]5 T4 T" D$ t5 i - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
8 S5 b. e6 W& r; w; e$ Q; [ - <property name="prefix" value="/WEB-INF/jsp/"/> ! U( T3 h" @0 V M3 ^
- <property name="suffix" value=".jsp"/>
0 V! x6 r2 a" r& g4 d( _2 Z9 ~ - </bean> -->
% y5 W8 f& q3 Q7 F- K - : c& h, L s( N/ K( |
- <!-- 导入springMvc.xml配置文件 -->
8 A+ Y' p$ |. Q% [* Z* i - <import resource="classpath:config/spring-mvc.xml" />
: E' U4 y6 z: B% o- B0 D - <!-- 加载数据库配置 -->
6 u( A" i! R) A+ | - <context:property-placeholder location="classpath:config/db.properties" />
o& h; j: w; i8 ^ - <!-- 加载数据源 -->1 N* {( s) A, l8 y; |* S3 S9 F; G4 W
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">2 H1 M; x% h2 a, g6 u7 g+ Y6 k& `
- <property name="driverClassName" value="${db.driverClassName}" />& X# q. m0 r n$ y, d
- <property name="url" value="${db.url}" />! n; Y4 x% a/ \6 Y3 u
- <property name="username" value="${db.username}" />
- a+ I* c6 s5 t1 b! H$ W2 O - <property name="password" value="${db.password}" />
6 I# {1 L: K+ w; @ b0 l8 d. C - <property name="maxActive" value="${db.maxActive}" />+ s3 F0 j7 y' C7 u
- <property name="maxIdle" value="${db.maxIdle}" />
/ H* V6 p: Z( Y# A - <property name="minIdle" value="${db.minIdle}" />
2 W1 z0 L/ X P$ n' e3 f; k - <property name="maxWait" value="${db.maxWait}" />8 l6 x& b% i6 a* G9 u! N
- </bean>
" ~- T. [) T# N @5 j - . B* U! X- R6 i( ~" J
- + a/ e" t( O/ c) |7 L( ~
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
7 h _: V" h% ~ - <property name="dataSource" ref="dataSource" /> & o( `8 u8 ]# I) Z
- <property name="mappingDirectoryLocations"> 0 C6 f7 n e; p, W* Q4 @' |
- <list>! a* d' v7 i- q8 C) Y% F7 \
- <value>classpath:config/hbm/</value>
& s7 m: ~; k( o/ I! G - </list>
8 P( V6 [% ^& z4 A" E! B9 T - </property>
) {. o6 z0 K* ?6 r( \' P - <property name="hibernateProperties"> ( |. w1 H$ i5 ?+ r# {+ u& V) t
- <props>$ @$ T5 b% s; P" v# S4 t
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>9 `2 P$ G! M- d! {
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>& B! B; G7 o9 N- Y1 ?$ m
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
7 ?4 s3 P! R2 ^9 B - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->4 Q1 |! Y' z1 Z
- </props> 5 N3 q" U- L3 F6 ^3 w. |: O
- </property>
4 _, w$ G) ^0 n! Y. o# u2 a/ J - </bean>
+ [7 F$ a$ L8 Y -
- d+ |4 j9 ~6 D* [$ U& o - <!-- 声明式事务 -->
4 ~! b7 N. q4 m4 j% H# P/ p$ S% S - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 0 p0 f) q& H: l( r: R5 @
- <property name="sessionFactory" ref="sessionFactory" />
8 U/ X# E3 T) s- i - </bean> & Y/ K+ x! f1 z
- : Y; D( V+ i. ]7 _' C x
- <aop:config> * Y) Y- ? a7 R9 z W9 {
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
* ]7 R) ~# M1 {8 }; Z, G - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> ) [3 Z2 O2 j& r8 T% u) F5 c
- </aop:config> * @; o4 p0 S! a" u' I
-
2 L! ~2 }- H1 J+ R! Y' p/ K. q - <tx:advice id="txAdvice" transaction-manager="transactionManager">
1 [" r1 C- Q6 ^0 W+ c8 G1 U! W4 Z - <tx:attributes> 9 s9 V' i8 n2 }: |% r" l
- <tx:method name="save*" propagation="REQUIRED" />/ g2 x2 z; I% Z& ^, k8 Q
- <tx:method name="add*" propagation="REQUIRED" />
0 s6 Y- P& C- u0 g* S" ~' z) b - <tx:method name="create*" propagation="REQUIRED" /># E7 G" B$ x) l% H$ N
- <tx:method name="insert*" propagation="REQUIRED" />
+ @5 g5 e+ R6 m5 ~% u0 R; {7 w$ p- N - <tx:method name="update*" propagation="REQUIRED" />
- N: t7 j5 i- x0 D' ^ - <tx:method name="merge*" propagation="REQUIRED" />
* D. H% `' m5 h6 W - <tx:method name="del*" propagation="REQUIRED" />, h& d" q, k" R0 k7 k
- <tx:method name="remove*" propagation="REQUIRED" />
P( G# r2 m. ^) l( Y- O - <tx:method name="put*" propagation="REQUIRED" />7 i/ J$ I6 I7 e3 D
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
& O$ _) x2 ?& W- ` - </tx:attributes>
$ }: [4 o. `' ` - </tx:advice>
1 g( { B5 u& a# W5 S - </beans>
复制代码 spring-mvc.xml:/ E' Q" U5 |- @; q( P f: ]) t
' ]$ e, I9 b7 s7 E8 o4 U1 D$ A! Z
- <?xml version="1.0" encoding="UTF-8"?>
) f! Z1 c3 V$ d) ?1 y' H7 F - <beans xmlns="http://www.springframework.org/schema/beans"" Q6 f6 m1 L2 i2 `
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context", n5 W7 J E$ g( S- o/ P. v) e; d' g
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; N) Z* X5 _$ K# ~) Q; w! G
- xmlns:mvc="http://www.springframework.org/schema/mvc"
$ g6 K& U, S) |! h" |6 r - xsi:schemaLocation="http://www.springframework.org/schema/beans ) ~5 u0 w( k! j: U5 f
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
, E: p0 |3 ~! e0 p% T: y - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd * p& h; d: x8 H, |
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 8 v6 N/ m- o' }' y0 g$ p) {% S
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
. t) w) s, u- f. D, }7 o4 Y - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
" m2 |" h* g3 J. q: J2 ` - <!-- 自动扫描的包注解 -->8 n: l2 I2 U, N7 U& [, D/ K) W/ D
- <context:component-scan base-package="com.hoodo.rci" />7 \& {1 k8 A- r
- <!-- 自动注入 -->
0 `& s) E- A4 K, ^2 p) z# v - <mvc:annotation-driven />
) i* X, c X; Z7 ?4 C/ M - <!-- 视图解析器 -->
) |% s& z* z4 p+ V( P1 _$ n - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+ [& y3 L* h) y4 g$ w7 b3 K! ]- C$ X2 r - <property name="prefix" value="/WEB-INF/jsp/" />: E" N) X( ~9 C5 g
- <property name="suffix" value=".jsp" />
8 I, _. u; j f* M% B - </bean>
4 F& Q8 Y2 {( z7 K! ^ - J+ [# g6 L' q' w& F5 s* c/ K1 w
- <!-- 异常解析器 -->9 t- I5 k4 R4 x- M% w9 X
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">) j6 }# p# G6 |4 d
- <property name="defaultErrorView" value="common/error" />
) v. R7 g5 U. F9 r* r0 ]) ~3 f. ^ - </bean> -->
9 I1 Z, [" ^5 S! m+ f - ! ~7 B' O1 j! e" j2 |9 v, {
- <!-- 拦截器 -->
1 i+ n/ x1 e0 u/ s% k3 r& x - <!-- <mvc:interceptors>
! C6 M) w4 z1 C, X+ c& ~( w - <mvc:interceptor>( F0 a- u7 z; i( E
- <mvc:mapping path="/admin/*/*" />
1 h2 d0 l/ X8 R9 }; |) |# o- P! C - <bean class="com.filter.CommonInterceptor"></bean>
1 c) J0 o5 g9 c {5 j$ ? - </mvc:interceptor>
7 S: i v0 b7 u m& H9 b& X. O1 ? - </mvc:interceptors> -->
" A5 \+ X5 ]; ?% B( { - 7 Z: w* g" X5 _
- <!-- 上传文件相关的配置 -->
5 T8 ~4 [5 x- {/ C/ N0 y - <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
. _* P6 r$ a( s" m( i& C1 h - <property name="defaultEncoding" value="utf-8" />
7 w* H7 s* V* G4 Z - <property name="maxUploadSize" value="104857600" />0 _$ O. j# H- d; G1 M# R+ E. P) K
- <property name="maxInMemorySize" value="4096" />
- z* C+ r. C4 I5 [. o" Q; _. D - </bean>7 O5 I3 i- W' O7 ^0 N# j
- </beans>
复制代码 * s6 z! c! N8 H, N# U
1 W2 t; T6 e) u( B& y8 {! f# {3 o: p/ U8 n0 V
SpringMVC+hibernate4.3+Spring4.1整合案例
6 i: J9 n+ F6 l( e* @* [! [% a$ E: x2 O, N: w* I
|
|