TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring MVC+hibernate 4.3+Spring4.1整合案例spring.xml:
6 z3 }9 S z7 L8 j- @% M& g
6 ]* f8 A* g0 z1 m <?xml version="1.0" encoding="UTF-8"?> ; L1 Y/ b. P U) P3 @2 a# O
<beans xmlns="http://www.springframework.org/schema/beans"
. v4 g$ Q7 S, F, }! W) Y xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ! R! ?+ c7 K, f' ~9 a
xmlns:p="http://www.springframework.org/schema/p" 6 E! W2 B9 a( I3 i
xmlns:aop="http://www.springframework.org/schema/aop" 6 |2 o! n% Y( E9 O1 Q/ L/ ^
xmlns:context="http://www.springframework.org/schema/context" . L3 v1 Z7 c0 J* J9 u5 `
xmlns:jee="http://www.springframework.org/schema/jee" , l; i" g8 g4 c2 V8 o e
xmlns:tx="http://www.springframework.org/schema/tx" 8 V, h6 f* M5 q$ ]. ?
xsi:schemaLocation=" i, d3 R3 x k0 Z8 ?, V
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 0 I) ^8 x/ x7 n7 W0 U- _! j7 u0 Z
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ' p2 |/ }) D7 m/ f7 e. L
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ' f$ X) W) @1 } z2 D
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
+ d3 q# c, T6 a$ v* Q- {: Q1 o, Z" ~ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 5 \: H3 J* R6 i
) s5 Z. z C0 n m5 `% _/ { <!-- <mvc:annotation-driven /> -->! f' G' u+ y7 R- ~+ P3 X
<!-- 【配置视图解析器】 --> , C6 ^( e' `. g3 g1 a
<!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
3 ~) [: A& }7 h7 m/ E; o <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
. f' C9 m" h- H <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> " T! y4 N8 L+ Q8 P2 ?0 G: ^
<property name="prefix" value="/WEB-INF/jsp/"/> , g$ ^: K3 A$ {& Y' [. d4 y
<property name="suffix" value=".jsp"/>9 t( @7 w! `. u% K; ], ]
</bean> -->) L0 ?/ m1 v, {
$ |. Y! b( r/ v1 A( G <!-- 导入springMvc.xml配置文件 --># y8 {( G2 H7 F Z& J
<import resource="classpath:config/spring-mvc.xml" />
B6 m/ W0 ]( ?3 n; }8 @ <!-- 加载数据库配置 -->
: m& N; H4 R; ^& U! ~/ o <context:property-placeholder location="classpath:config/db.properties" />
9 P* D! T- j5 e6 `* Y <!-- 加载数据源 -->
1 h, h, `, D" \ <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
9 E. ~( ]& D; | <property name="driverClassName" value="${db.driverClassName}" />
. `, J. t' m1 W$ l; s$ U' G <property name="url" value="${db.url}" />* M/ {( \8 N% j( c1 f. V& A) W
<property name="username" value="${db.username}" />
6 i* i9 c. j- e9 J/ O <property name="password" value="${db.password}" />! ~- G: Q7 r% B' V" o# @4 H
<property name="maxActive" value="${db.maxActive}" />
' T" Y8 B k) @3 {- w' f4 @& S# v; T <property name="maxIdle" value="${db.maxIdle}" />6 f E# l. J) z
<property name="minIdle" value="${db.minIdle}" />/ F6 X' @) Y [0 f. L8 J* t& z
<property name="maxWait" value="${db.maxWait}" /> s+ ^6 J' _$ v' x+ @
</bean>9 h9 D" l" n2 H9 W6 y
2 A R- P" S u, h! `4 w, r 2 ?$ p6 T9 ^! R2 T
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 1 t( s1 ?& Z" c) ~/ U
<property name="dataSource" ref="dataSource" />
, `$ ^- _5 |) w D+ h* S <property name="mappingDirectoryLocations"> ( }$ ` q4 T; I9 o; v1 Z% B+ u
<list>
1 v! ?- C+ { ]+ }) P% V) C. b: ~0 U <value>classpath:config/hbm/</value>
: Z7 Q1 ^- V/ _ </list>' b% \: q: u$ j
</property>
7 y$ |0 U, v6 E- b/ t <property name="hibernateProperties">
: B2 I* x. r- u6 q/ s. ?6 u <props>; d5 Q+ i$ e; f/ c% c( {
<prop key="hibernate.dialect">${hibernate.dialect}</prop>/ l8 r0 Q4 s" \ b+ l
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
* x R+ S) L1 r <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
( S) _1 k" d# `' `3 e. k9 O5 W0 { <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
2 X; o: J* X D) j4 z </props>
3 W9 P1 f5 @/ M6 e: | </property> 8 B6 `& u$ d: s5 Z' F3 _3 n
</bean>
3 N" W2 w# C1 P : [2 ]; [5 s5 U% R5 Z- \8 }
<!-- 声明式事务 --> 2 i( T' M3 }( H9 k/ @' P! B
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
4 l- `( \& y r$ L8 Z <property name="sessionFactory" ref="sessionFactory" /> + Y, y9 p& R+ u) h
</bean> 8 L$ ?; T- H* }+ J% k
: b# L% x* Q8 |- p; d
<aop:config>
; }2 Z8 z, S9 |# i <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
- S; V" _2 W+ S. { <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> / h7 K/ N4 }" P# @" W
</aop:config>
# S6 f4 p+ @: |' M9 W( `% ]5 S
( C5 J: `5 Y1 E5 x% Z <tx:advice id="txAdvice" transaction-manager="transactionManager"> : ~9 y' h7 r% p) `
<tx:attributes> " x5 `# Z+ z1 p. c3 Y+ }5 T
<tx:method name="save*" propagation="REQUIRED" />6 T4 s# x' f- S
<tx:method name="add*" propagation="REQUIRED" />% X! H, ~% n0 Y' d
<tx:method name="create*" propagation="REQUIRED" />
$ {6 A E$ k7 T! U- h+ d8 X <tx:method name="insert*" propagation="REQUIRED" />; y$ O7 P- X, i8 j$ u9 |1 |& M
<tx:method name="update*" propagation="REQUIRED" />
1 S- ^+ ~5 V$ R <tx:method name="merge*" propagation="REQUIRED" />; Y* C" ?% A4 h. _+ M* t5 O
<tx:method name="del*" propagation="REQUIRED" />
9 e; j5 J0 x1 ~- f% X2 G5 C <tx:method name="remove*" propagation="REQUIRED" />) [) c3 S: Q" G. J8 R3 ^8 {6 x
<tx:method name="put*" propagation="REQUIRED" />, e2 o' K4 P% ^' @; n
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
3 ^6 Z& m' C% ~* D e2 \3 p6 G </tx:attributes>
5 I: B9 ]- o6 \8 Y </tx:advice> % T8 h' I5 G* }5 G0 f
</beans> 复制代码 spring-mvc.xml:
8 c5 ]; t; J; C: v+ J% h! }. E
* b3 w- |/ d4 [ <?xml version="1.0" encoding="UTF-8"?>
' B9 x, V- W9 ~$ {# | v <beans xmlns="http://www.springframework.org/schema/beans"8 d! r7 h. d; }9 B9 P# w
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"4 q w3 W* q/ Y
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"* a0 t- N8 l) L1 T, c, a
xmlns:mvc="http://www.springframework.org/schema/mvc"4 d* @* p! z- c% r) t2 E5 E
xsi:schemaLocation="http://www.springframework.org/schema/beans
4 l$ }8 @7 z ~9 p http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
$ l0 M5 w; I7 |9 |% t" M* _ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
4 ?3 r( z1 F2 M http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 1 V6 p$ H0 o& q5 T2 @ h% P! f- \
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd$ N$ |3 S5 W+ Y0 u
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
# v- }; e/ S7 x; ^) G <!-- 自动扫描的包注解 -->
* E T3 T1 m' R( `( e1 O4 h( p; j1 J <context:component-scan base-package="com.hoodo.rci" />5 `& E. _8 I# ^6 {6 D
<!-- 自动注入 -->
& q8 B1 C- ?7 `0 D <mvc:annotation-driven />. ^/ g. ]' F$ K. j3 g* J5 j
<!-- 视图解析器 -->
& Q4 c# B( R \0 h* w" F1 M6 y <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">( J$ W; ?% \ A7 C5 d
<property name="prefix" value="/WEB-INF/jsp/" />8 ]# d, o/ Z2 I8 x
<property name="suffix" value=".jsp" />: o$ _& B: J8 m, {, o
</bean>
, X3 C" x1 v# J $ g4 i/ }( ^' ~; f
<!-- 异常解析器 -->2 V0 K/ Y3 V+ \: W% u6 L) Y& u9 y' K' L
<!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
& X$ j1 F: T$ a% r# t3 P' g <property name="defaultErrorView" value="common/error" />
$ B2 `5 d" z4 C% W </bean> -->
$ S4 p5 b# ]3 A1 M 9 ~2 U4 ^5 W# A( ~, L+ S' ~" q
<!-- 拦截器 -->
7 h6 ^7 U' k" Y+ x7 W* z <!-- <mvc:interceptors>
6 m( Y1 m K0 d <mvc:interceptor># C& V9 |6 _" U: |2 m, R |
<mvc:mapping path="/admin/*/*" />
4 V6 c: N& [+ \& Q! | <bean class="com.filter.CommonInterceptor"></bean>
) Q4 | ?. _+ B4 p( r$ e! |& m. ? </mvc:interceptor>
$ j% u& w: J, h, o& I </mvc:interceptors> -->4 p. @ O8 B. E: e$ Z
, x! s) S4 K9 s5 t
<!-- 上传文件相关的配置 -->" [, A9 c/ L, ^# d/ }" G/ s% a
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">: ~& e. x+ j2 _% L% ]' X
<property name="defaultEncoding" value="utf-8" />
- @: x% U$ }( e <property name="maxUploadSize" value="104857600" />
* k3 M' g6 Z# ?) H <property name="maxInMemorySize" value="4096" />
: _+ j+ ~( ^ A </bean>
% e Y' N8 m2 b </beans> 复制代码
- l0 r" t, S. ~* @ Q: i2 N
2 s T) t: U* O
' f4 K0 b5 X9 q4 ~# o1 e1 g: n" L SpringMVC+hibernate4.3+Spring4.1整合案例 * S# p/ q: c5 o
$ w4 b V+ L; \- U& r0 B) F2 `
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :