我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > SpringMVC+hibernate4.3+Spring4.1整合案例
总共48086条微博

动态微博

查看: 8319|回复: 10

SpringMVC+hibernate4.3+Spring4.1整合案例

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2015-04-15 20:59:36 |只看该作者 |正序浏览
    springMVC+hibernate4.3+Spring4.1整合案例spring.xml:% b) I9 Q/ W) ^: e3 s0 G; h
    5 o6 {7 E' u- M. B
    1. <?xml version="1.0" encoding="UTF-8"?>  2 l  f. a" w' }3 B; _& V/ b& ^3 q
    2. <beans xmlns="http://www.springframework.org/schema/beans"    - G7 ~9 i( H/ ^6 i) x2 o# Z
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      7 S) K- i3 X5 u$ f& {/ E. ]% a' n. ~
    4.     xmlns:p="http://www.springframework.org/schema/p"  4 V$ z4 u- E: U
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   & ]0 ^( W! p4 m! h1 E
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      0 V# p0 M% |+ F: |' t/ `: t
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      3 [/ H2 W# a/ Z7 T% d: @. @: |, K& i
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  4 k1 \8 w' l. K, G5 M2 a
    9.     xsi:schemaLocation="    & B/ x2 G, t) m- \7 ^+ T" p$ n1 U
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      1 q- S" l: k' R
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  8 a- \/ U. Q) N( R6 z5 O+ g
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      % d; V# M0 C/ X. P3 @
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  3 P  h& B- q8 F# W& w) P; G
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> , l+ T: H, [1 }: a* N4 n
    15.    
      ; t$ d6 J# J! P' `3 N
    16.     <!-- <mvc:annotation-driven /> -->
      : y' W2 \6 X' Z' |4 k
    17.     <!-- 【配置视图解析器】 -->  
      # t4 j  }8 `7 j  F. {, e) ?  H: I
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
        k5 l  }7 V) A7 W
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      # n# t+ U* o' R* j$ G% Z1 y
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  + v0 }! C/ N6 e7 [
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      - Z4 f8 H* a$ T+ @  ^
    22.         <property name="suffix" value=".jsp"/>
      ( l6 `& z. n, t
    23.     </bean> -->
      2 {: N/ O/ ?, j6 m6 U
    24.    
      & v# @2 Z( [) X6 A/ X
    25.     <!--  导入springMvc.xml配置文件 -->; F$ U8 U8 ^$ X- x6 a% `! Z( b
    26.     <import resource="classpath:config/spring-mvc.xml" />
      ' B+ B% w( _+ [# d* u
    27.     <!-- 加载数据库配置 -->+ d4 u) A. l3 e+ ?
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      / K4 n& X' l7 d
    29.         <!-- 加载数据源 -->  q  l# D" D, o7 X' x, J+ \1 C
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  q: Z% q2 f6 l9 b/ m
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      ; o4 Y& a* {  K$ A6 Q) s
    32.                 <property name="url" value="${db.url}" />
      ! ?6 r" A+ T2 M
    33.                 <property name="username" value="${db.username}" />
      0 V/ B0 {( r0 _) B/ L3 k0 N2 E, D
    34.                 <property name="password" value="${db.password}" />
        j- X5 [2 K5 u# b: J3 C
    35.                 <property name="maxActive" value="${db.maxActive}" />
      & Q2 }) G8 U$ R& C( H. L$ i) \6 z
    36.                 <property name="maxIdle" value="${db.maxIdle}" />
      9 y, |3 n& K/ b; V
    37.                 <property name="minIdle" value="${db.minIdle}" />
      ' @' l' B  f# z+ c
    38.                 <property name="maxWait" value="${db.maxWait}" />  F, c$ P; r7 r8 p7 J& y, N
    39.         </bean>( J+ j9 ]1 ]' R7 L
    40.     5 K! X$ t% M, Z& y4 L2 t, n
    41.     & \7 t" N+ s8 z: T& h+ c+ C4 [
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  # D7 B- `5 J5 P8 t% I% V# H
    43.         <property name="dataSource" ref="dataSource" />  ) I3 S9 M) a0 [( v: j5 [  ?
    44.         <property name="mappingDirectoryLocations">  % W& q9 d% \! `6 o/ s
    45.                   <list>. W7 u% V7 f8 {! ]6 R
    46.                                 <value>classpath:config/hbm/</value>/ c! B2 y9 v1 d$ [& H% e- g  e; v
    47.                         </list>: p; S+ r, r& r  F5 c8 L, \
    48.         </property>  ) U4 U( T6 P; {: W. k, S2 F1 T: j; B
    49.         <property name="hibernateProperties">  , G! ~) `; P, ~! C
    50.             <props>
      2 r7 A" J2 n9 G: H2 Z4 ~
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>( I* @2 H- `; z
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      # R; L( ^( b0 A& c1 [
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      ! S5 }# m+ L6 |
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>--># G8 C, w- ~1 \' @# Y. A
    55.                                 </props>
      " i" X. C# e  w: g3 W) s6 l
    56.         </property>  - M# }1 ]. k$ y3 J! J* l, c! D/ i
    57.     </bean>. f2 r- c$ d7 R% I* i
    58.    
      ; |+ U1 T7 }6 p4 ?
    59.     <!-- 声明式事务 -->  
      3 }# c# \: E4 z+ o" S
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  5 e; o6 m% l! G/ B2 o
    61.         <property name="sessionFactory" ref="sessionFactory" />  3 z8 |- |1 ~" M6 S
    62.     </bean>    L5 S# S9 n7 f8 Y. N
    63.          
      1 T# Q  V, c1 |/ o( d
    64.     <aop:config>  % C# K1 u: v% C4 u8 s$ R
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      $ L3 W5 q. F7 \! C; k
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
      2 m5 T8 V7 N4 x
    67.     </aop:config>  
      4 x- p# M8 G( Q' {) J" q- V3 o
    68.       ( E0 x) z# V% d- l
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  # J+ C7 ~" B& \
    70.         <tx:attributes>  
      ) q) O/ I: c7 d" B4 u5 [, b  L! Y
    71.                 <tx:method name="save*"   propagation="REQUIRED" />* P, f9 a, g) o
    72.                         <tx:method name="add*"    propagation="REQUIRED" />' J$ [: Q& S# B, [  l  _1 Z/ g0 r
    73.                         <tx:method name="create*" propagation="REQUIRED" />+ o- A* x; b$ x/ A8 y
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      , d" U7 o* }5 i) }* \& p: H
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      ; K  H9 {$ C, l( S# F! G
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />  r3 ]' N) M3 T! d# B, |
    77.                         <tx:method name="del*"    propagation="REQUIRED" />
      * f( d4 _0 K1 w  Z
    78.                         <tx:method name="remove*" propagation="REQUIRED" />
      % Y$ C6 l) k1 @0 D& z) F/ Y: A
    79.                         <tx:method name="put*"    propagation="REQUIRED" />$ {- g# n) d# p, s+ x/ c& X
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  % j/ Y% E% r& v8 R
    81.         </tx:attributes>  
      " P  y2 ?9 [  p! |4 O9 A
    82.     </tx:advice> , D7 q- B+ a) U' d, x, r
    83. </beans>  
    复制代码
    spring-mvc.xml:
    5 H6 W4 u7 g2 N8 B0 g, K  N' U, l! f4 y+ n; p7 g" Q! L1 R0 e
    1. <?xml version="1.0" encoding="UTF-8"?>
      9 w6 `9 w" h& Q2 f& ]
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      ) K9 n8 |$ N- _+ T" `
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      % H( M) C( N4 ]' Z  r% ~+ C
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx") x, N' ]! t* z5 Y
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"/ C& a; ~& H4 z3 X
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      & m2 ]* d6 Z! B# ?  n( F1 M- z
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  8 c. h* g0 u- u. }- k4 A: a0 c
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      * ]( @/ |0 a8 r  S' D
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  5 z! b7 K) C* W& l
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd2 k4 D, ~- D1 z1 _
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">; o( Z+ h1 O) B2 k
    12.     <!-- 自动扫描的包注解  -->
      ( {6 s  d7 b2 l6 T5 T8 s  J3 ~+ R9 e
    13.         <context:component-scan base-package="com.hoodo.rci" />
      - h" g8 Z& C8 f* w- y
    14.         <!-- 自动注入  -->
      ) S) |3 U: \* ^) I- U4 }9 _
    15.         <mvc:annotation-driven />
      ) R) \0 B/ l; `) }5 C
    16.     <!-- 视图解析器 -->4 `  ]4 }1 _- C2 M+ B
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      ! F! Q1 T! l* v: t% a
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />
      ' m' W2 h/ n4 p, F( x
    19.                 <property name="suffix" value=".jsp" />* j: h4 m9 ^. o3 {7 B1 A& m4 O$ \
    20.         </bean>
      ( s. s' x( n: a# @" [/ i$ O) I
    21.         
      # Q0 a. b- E9 R3 p& d
    22.      <!-- 异常解析器 -->7 [' s  D# J8 V6 z8 X4 b# o
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">$ M  _7 E) r4 u3 c: z
    24.         <property name="defaultErrorView" value="common/error" />* i0 ^6 p; V* _
    25.    </bean> -->, `+ H8 n! ?  [6 u1 u6 M
    26.      
      0 s7 F- P9 h) Z& r& g& P
    27.      <!-- 拦截器  -->
      & X5 f; b2 i# j1 C
    28.         <!-- <mvc:interceptors>
      ; \: L" Q3 a. }1 j4 e
    29.             <mvc:interceptor>
      6 H4 {2 R1 y% u8 i! J
    30.                <mvc:mapping path="/admin/*/*" />* `' ]) G8 u3 E2 ]
    31.                <bean class="com.filter.CommonInterceptor"></bean>% ~7 Z* r, k/ R% {4 M: l+ a4 i* ]
    32.             </mvc:interceptor>, x' _+ J; X! F
    33.         </mvc:interceptors> -->0 j- p8 R6 n1 c5 d' ]' B& F% n# i

    34. ( k  [. G% W; c) n* v
    35.         <!-- 上传文件相关的配置 -->
      % W, O: {. L  N1 Y  ]
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">9 _" N( a( ~* n- L9 v
    37.         <property name="defaultEncoding" value="utf-8" />; N4 T4 Z$ \+ A6 o) J' R6 J& X
    38.                 <property name="maxUploadSize" value="104857600" />- l2 m5 x! d( z% W1 @2 ?5 x
    39.                 <property name="maxInMemorySize" value="4096" />
      ( H0 M% A7 {' F5 c0 E% y1 I
    40.         </bean>* x  u, A6 L6 n) l
    41. </beans>
    复制代码

    9 U7 t- ~& e8 H
    - o4 S& r  Q% G) P' @/ ^! C) K- m# u2 F; z! V, R6 g
    SpringMVC+hibernate4.3+Spring4.1整合案例
    9 ]- g+ a9 W# R0 w

    - W3 C3 U+ b8 P  }

    我的日常 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与我的日常享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和我的日常的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、我的日常管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    4

    主题

    0

    听众

    81

    金钱

    三袋弟子

    该用户从未签到

    11#
    发表于 2017-10-22 20:29:57 |只看该作者
    ( C8 s1 a/ r0 j. n9 l, t% p, f; }
    看看。学习学习。谢谢楼主
    回复

    使用道具 举报

    5

    主题

    0

    听众

    316

    金钱

    四袋长老

    该用户从未签到

    10#
    发表于 2017-08-14 08:49:01 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

    46

    主题

    1

    听众

    584

    金钱

    三袋弟子

    该用户从未签到

    80后

    9#
    发表于 2016-10-30 11:15:32 |只看该作者
    SpringMVC+hibernate4.3+Spring4.1整合案例 [复制链接]
    回复

    使用道具 举报

    4

    主题

    0

    听众

    302

    金钱

    四袋长老

    该用户从未签到

    8#
    发表于 2016-09-26 16:41:42 |只看该作者
    看看。学习学习。谢谢楼主
    回复

    使用道具 举报

    1

    主题

    0

    听众

    80

    金钱

    三袋弟子

    该用户从未签到

    7#
    发表于 2016-06-22 09:26:53 |只看该作者
    好资料,可以好好学习下
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    6#
    发表于 2016-05-05 16:00:44 |只看该作者
    感谢分享   ,
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    5#
    发表于 2016-05-05 16:00:11 |只看该作者
    下来,学习一下。谢谢
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    地板
    发表于 2016-05-05 16:00:00 |只看该作者
    下来,学习一下。谢谢
    回复

    使用道具 举报

    woniu 实名认证   

    2

    主题

    0

    听众

    330

    金钱

    四袋长老

    该用户从未签到

    板凳
    发表于 2016-04-12 11:49:21 |只看该作者
    这个项目太棒勒!下下来学习下!
    回复

    使用道具 举报

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    关闭

    站长推荐上一条 /1 下一条

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表