我的日常

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

动态微博

查看: 8317|回复: 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:6 c8 t4 V9 G% _& P% x* X# H
      Q( u- q& D& Q0 {
    1. <?xml version="1.0" encoding="UTF-8"?>  
      % l$ F+ z! b; [6 L( L  C
    2. <beans xmlns="http://www.springframework.org/schema/beans"    ) V3 X% N  O6 c  \& V) a6 m  ?
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   2 Q1 |/ D% k$ k: \1 T* K5 J9 S
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      2 B1 @3 z; `4 i5 H# L
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   / P! G- v! g: H0 U" f
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      1 c2 w, @0 I  G8 A
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  " C" f- @; G9 Q6 Z$ ^( X
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  + f5 m" y! j4 ]6 G: [
    9.     xsi:schemaLocation="    ' v' \; o; B! B, k- g5 J/ ~
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  + N6 L4 r; C& H' J; J0 h
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      + }% V( ^2 M7 j  H
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  9 X" y. N2 A* Y
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  / g& W# {9 @$ P# c& S% D% K) P2 [
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 1 \  B  |1 y# d$ ~
    15.    
      . @8 L. W+ e  |, R0 I5 a
    16.     <!-- <mvc:annotation-driven /> -->
      3 D1 P3 F* }$ r7 W' b2 q
    17.     <!-- 【配置视图解析器】 -->  & X5 h/ r0 x. U. J
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
      1 C- q* I( q7 W9 c8 D* \5 v3 E2 f
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      7 R( }- s+ f; q' C$ B
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      5 F* ?; `: |5 V# P
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      . p( e2 @  v9 _. l3 R
    22.         <property name="suffix" value=".jsp"/>4 r2 z( K! p: R" K0 `- t8 V
    23.     </bean> -->
      " u; p% }3 _' o9 p0 t$ w* O
    24.    
      * C$ b7 n, d1 R1 x+ l/ V
    25.     <!--  导入springMvc.xml配置文件 -->
      % v/ v" f! T3 @$ n
    26.     <import resource="classpath:config/spring-mvc.xml" />* X, K' ?) v2 s1 m
    27.     <!-- 加载数据库配置 -->
      " C$ I0 r" t5 b$ l. B& h% j& Y
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      6 `" [- L7 v8 A0 |
    29.         <!-- 加载数据源 -->9 a; L9 M/ Y) `8 z* H
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">% _/ `( r" `& s+ b4 J# F: Y
    31.                 <property name="driverClassName" value="${db.driverClassName}" />$ e* u; a2 W1 a$ L2 U) x
    32.                 <property name="url" value="${db.url}" />
      : a+ |; B) \; E: S
    33.                 <property name="username" value="${db.username}" />
      ; `6 q! n/ d* p) f! M
    34.                 <property name="password" value="${db.password}" />" l% l1 D. U. T- H0 j
    35.                 <property name="maxActive" value="${db.maxActive}" />( l- t7 X/ y$ @# c# n
    36.                 <property name="maxIdle" value="${db.maxIdle}" />! `! _+ N4 O/ n# p( n* F
    37.                 <property name="minIdle" value="${db.minIdle}" />7 U6 s& L: q# K$ K( d$ U
    38.                 <property name="maxWait" value="${db.maxWait}" />! K/ A& Z0 h( K
    39.         </bean>
      & c" {/ R4 D4 _6 ]6 J( d
    40.    
      # s7 L+ |5 x' P
    41.    
      ' j3 l: v. B/ j( Y* B3 a
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  $ k! Q1 n( [+ W, \! ?
    43.         <property name="dataSource" ref="dataSource" />  % d! @# h4 M$ L1 U& @! |+ B. @+ p
    44.         <property name="mappingDirectoryLocations">  9 k0 b5 z9 H( B9 u( I( p3 H# r
    45.                   <list>
        C1 |: W% d( i4 ]2 i/ _3 m* C
    46.                                 <value>classpath:config/hbm/</value>
      ! O# `# A1 |  K+ A$ N) c7 [3 _! s
    47.                         </list>1 e& `% M) |) P& U5 l" n
    48.         </property>  
        {( s) A6 h2 K: F1 c
    49.         <property name="hibernateProperties">  
      $ |, ^" M5 _  Z5 v" N
    50.             <props>5 o3 g  P5 k* p, c% h& M
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>, W5 ^  V& B- Y* U0 T
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      0 S3 c1 Y# d  i/ o' b2 F
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      ! [0 n# [9 b" ^4 c
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->" n# I6 m. \3 }7 _, l4 w" h( j
    55.                                 </props>
      ! N9 V4 a/ K* w- ^3 B  a
    56.         </property>  
      % q3 A- S: J# e9 o4 l/ _
    57.     </bean>; {8 ^( [2 E" N* q) M+ o0 R
    58.    
      2 G! K4 D" ?" a$ b4 q; g; L
    59.     <!-- 声明式事务 -->  
      . H1 c% h# ]  u! i/ P9 b! k; Y6 T7 |! ]
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      ; A, Y, ~6 f$ }+ e/ N: f
    61.         <property name="sessionFactory" ref="sessionFactory" />  6 D/ g& G, E  I. X
    62.     </bean>  ( G5 z& {( G- h6 v: O
    63.          
      % i: i  V: i' ^5 J
    64.     <aop:config>  
      : e) ?/ _- z2 L  `
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  ' e& y) f8 h0 a; E
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
      * g  C  }& W, ^0 E+ q* |
    67.     </aop:config>  + W) ~8 B& v! q8 \, W
    68.         n6 D# h6 A# ^2 B5 T
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
      ( [4 i) K& D8 R4 |& A
    70.         <tx:attributes>  
      8 k) k) U; V3 c( @4 D1 l
    71.                 <tx:method name="save*"   propagation="REQUIRED" />
      % B7 V; Z1 ~8 k3 e0 Z, Z
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      : s' c# w; A. U/ ]" Q6 F) N
    73.                         <tx:method name="create*" propagation="REQUIRED" />& b5 K$ [0 q! @1 G0 I
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      5 }$ M9 A+ ^7 {* G: y  s. l
    75.                         <tx:method name="update*" propagation="REQUIRED" />' ]" a, G, ^+ H9 [, p( F8 R. Y
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />) b6 h2 P. c2 |7 q2 e8 J* n
    77.                         <tx:method name="del*"    propagation="REQUIRED" />. k0 @5 `$ K0 b9 \* d4 _, x3 A
    78.                         <tx:method name="remove*" propagation="REQUIRED" />  L7 z+ G9 ~$ b$ J6 }% w- x
    79.                         <tx:method name="put*"    propagation="REQUIRED" />
      8 l7 k: l/ C, g" w
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      . x% Y+ U, d: t* W( @
    81.         </tx:attributes>  ) K2 D( U& A1 T/ Y5 z& `' f' o7 o  I
    82.     </tx:advice> # |5 g- N5 J6 {- j5 S
    83. </beans>  
    复制代码
    spring-mvc.xml:
    3 [! E" M9 j) t9 r' S; A, ]; ]6 ]
    2 T7 Z5 [* Q* C- D. P
    1. <?xml version="1.0" encoding="UTF-8"?>
      : ?$ c( n- ~2 Z9 o
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      7 M5 l( m* U8 @' O
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"7 |# w3 R& J1 x) c8 S
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"0 \4 B- m9 y8 q1 p& h
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc": h; H! ~: k1 U% t4 R0 q- C. u, R
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      0 M' n7 T( [' K: R* A8 W
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  / ]/ c" Y/ J5 k5 Y& D! |
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  2 S& r1 ~2 `0 ]( R6 F
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  / Y4 f6 L" ^6 N: u, q, M
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      3 p9 M& M2 W; f. ^4 t5 X& z9 G9 J( R
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      ' Q( g) w) B& ?1 t4 y1 I
    12.     <!-- 自动扫描的包注解  -->
      - I- R, G: ^/ z
    13.         <context:component-scan base-package="com.hoodo.rci" />+ G/ _, P+ |, f* s4 h
    14.         <!-- 自动注入  -->
      & g1 e" D: d% p3 c' v
    15.         <mvc:annotation-driven />
      3 f5 z" |! c! o2 w. O
    16.     <!-- 视图解析器 -->
      - u9 \& `' V/ i) x7 e4 K, \
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      ' S% m* ]+ f) ^2 H1 s8 T* U
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />* F: B/ c) v, ~7 N1 N7 N% f
    19.                 <property name="suffix" value=".jsp" />
      3 N. }7 W/ H1 q, \( j% j
    20.         </bean>& U; p1 l& H) Q/ m
    21.         ( h& n6 d5 f: \% v
    22.      <!-- 异常解析器 -->
      8 K" y& |) L# l$ }
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      0 u2 G" @; o  Z2 u) x( |  {, h
    24.         <property name="defaultErrorView" value="common/error" />9 m2 N! k  S8 r* r4 ?  L
    25.    </bean> -->1 d6 Z+ O4 i$ w1 V
    26.      
      & k$ P6 A. V5 o- C7 k. C
    27.      <!-- 拦截器  -->
      . P7 c7 i4 U, ?) |4 a% i
    28.         <!-- <mvc:interceptors>
      $ G4 G3 a& y  W" E- Q
    29.             <mvc:interceptor>
      - f. G  `6 U) [% X
    30.                <mvc:mapping path="/admin/*/*" />
      * p  v% k. ]1 K
    31.                <bean class="com.filter.CommonInterceptor"></bean>1 K! Y1 V7 E" C0 c3 h# B
    32.             </mvc:interceptor>7 Q5 D% ?# Y5 I+ M0 v3 G- P2 K0 A* [
    33.         </mvc:interceptors> -->. B+ W3 `& N. N7 a: N

    34. % Z! j* i, L% v. _3 c! L4 }3 m( H
    35.         <!-- 上传文件相关的配置 -->
      7 k) n! B; j! e/ W
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">* Y0 J2 H  c, X: h0 y. ?
    37.         <property name="defaultEncoding" value="utf-8" />
      7 K/ B7 n! s" ^) _" P" L
    38.                 <property name="maxUploadSize" value="104857600" />
      ! }- g! }$ ~5 _7 m
    39.                 <property name="maxInMemorySize" value="4096" />9 O0 ?% |9 {2 H2 @+ t7 x4 ~) T+ k+ E
    40.         </bean>; I5 O9 \: C4 r' c: g; t- _& \. O
    41. </beans>
    复制代码
    8 Q" N+ ]* V$ m$ ^
    2 F7 ?4 e  ]0 g6 S5 d
    7 e5 ^4 E9 N, G7 g  s
    SpringMVC+hibernate4.3+Spring4.1整合案例

    - @' X/ ?9 b& D2 [" X" Y- ~" I5 {& Y* p# h* ^" D. V

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-03-17 21:46:03 |只看该作者

    9 t( ]. Z0 r: ^' x* h; O下来,学习一下。谢谢
    回复

    使用道具 举报

    woniu 实名认证   

    2

    主题

    0

    听众

    330

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    1

    主题

    0

    听众

    80

    金钱

    三袋弟子

    该用户从未签到

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

    使用道具 举报

    4

    主题

    0

    听众

    302

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

    46

    主题

    1

    听众

    584

    金钱

    三袋弟子

    该用户从未签到

    80后

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

    使用道具 举报

    5

    主题

    0

    听众

    316

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

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

       

    关闭

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

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