科帮网-Java论坛、Java社区、JavaWeb毕业设计

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

动态微博

查看: 8180|回复: 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:* a$ g$ W! ~( O2 U) }  J4 N% D* O& w
    # j  Z- J1 _3 C, j
    1. <?xml version="1.0" encoding="UTF-8"?>  
      4 Q: L6 E) n" [: S- o' D  n8 U
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
      $ I* a* [' ]  B( o. Q
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      2 g$ q( S7 E1 w4 \' h
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      7 u0 U! u) _% x6 T3 K
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      ' p* P/ T) }* K1 f. e
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      8 g! L+ ]9 w( N; _7 K& o
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      3 k% i+ ?5 |* m, n) i' a- P
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  $ g' ~6 G# ~4 P' k
    9.     xsi:schemaLocation="   
      8 ?- B9 [$ R3 ]% _0 {
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  6 ~! @8 q0 t8 X+ u" `0 H1 ^; S
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  ' V1 Q0 C3 h- W* A( K) d
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      # f. L+ F; k4 @1 _
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      ( ?* x; G/ I  ~0 J
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ; Q& E) I/ l  ~, C+ ]
    15.    
      2 ~$ P# u2 k( {& Y' Z& Z+ `
    16.     <!-- <mvc:annotation-driven /> -->
      8 S+ A( S- B0 Y; Y2 d. ]) Y( l" L- f
    17.     <!-- 【配置视图解析器】 -->  ) o" _1 u, M7 G8 I! R) B
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
      . G& O/ n- m, @$ S# `0 [: ?5 A( r
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      $ W5 T  I6 E# p
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  2 E9 G2 _8 b( G5 i2 |. {" R) D
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  ' F6 U$ D; B' [4 Y% c: N
    22.         <property name="suffix" value=".jsp"/>' P! U1 {( ?3 W+ ^
    23.     </bean> -->( d# c6 a7 @) q6 V
    24.    
      % z" w6 |8 q* c' W2 ~% I: Q4 L+ x% j1 i
    25.     <!--  导入springMvc.xml配置文件 -->! ~9 S8 s2 ~' O  |
    26.     <import resource="classpath:config/spring-mvc.xml" />
      " F8 A7 ^$ T) |5 y& W' M/ U2 A
    27.     <!-- 加载数据库配置 -->! U: _- r# f- H* q9 t! w4 d
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      8 y1 j8 G2 [/ c
    29.         <!-- 加载数据源 -->
      4 _1 [- X' C% w+ B3 ?% ?( M
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">$ V; I4 R1 e* ]2 a* ]9 S1 b
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      * ?3 A8 a. H$ o! W- T. L, g
    32.                 <property name="url" value="${db.url}" />, A4 C) [) V/ u$ @  K
    33.                 <property name="username" value="${db.username}" />
      , S/ |7 p1 i% Y8 [+ J, v
    34.                 <property name="password" value="${db.password}" />' n. y' X# Q+ K: ~9 b+ l
    35.                 <property name="maxActive" value="${db.maxActive}" />
      " D2 \" ~+ ^& r4 ]; I
    36.                 <property name="maxIdle" value="${db.maxIdle}" />7 B8 s3 X$ ]' V, {: R( ?
    37.                 <property name="minIdle" value="${db.minIdle}" />
      / T+ p( n4 L- S) d0 K3 f* x
    38.                 <property name="maxWait" value="${db.maxWait}" />  v) ^6 h. L4 C7 x1 S# x
    39.         </bean>
      1 l; G( ?2 |0 m5 g9 ]% E4 _+ a, ~4 c
    40.     4 R: }: z. I$ ]2 e8 Z  F
    41.     5 a, L( L, {$ g3 G4 b
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  $ @) \% H$ ^+ N( ], q
    43.         <property name="dataSource" ref="dataSource" />  
      & H: i  E2 R8 [3 o
    44.         <property name="mappingDirectoryLocations">  : L- Q$ s' Z! o. M5 }* {" e( |
    45.                   <list>
      + N8 P  c; I4 E8 S; Y
    46.                                 <value>classpath:config/hbm/</value>
      ' ]  m. w+ x% h$ V
    47.                         </list>
      9 t$ N% r/ k# U( G- f
    48.         </property>  
      + s8 `2 B- V  b8 E; P- m% X) I: o
    49.         <property name="hibernateProperties">  
      1 Y* g* [& Q1 ^) [) {
    50.             <props>
      7 B7 i. X8 l% \& H* S1 R
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      ' X5 V; \/ W: E' H) U2 `# L
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      $ {3 q. x$ z( `4 }, l* S
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      5 v2 |% Q! n2 @; z' l, A* d" X- Z' \
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
      2 ~- ?. i# j1 m
    55.                                 </props> ; f, j9 |- B* k1 d5 o6 n* L( r
    56.         </property>  
      3 p7 \0 g+ p9 S" a" y& v
    57.     </bean>$ F9 d0 m9 ?: e$ E7 B1 j5 Q! Y
    58.     ! x( i& R. t( R, Y) P
    59.     <!-- 声明式事务 -->  4 _. _+ X, z- ?* h  y
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      - V. T/ e+ J* [8 o3 x8 X
    61.         <property name="sessionFactory" ref="sessionFactory" />  
      " @0 l4 w1 ^4 ^) U. E# }# o
    62.     </bean>  ( w/ k5 n- v  v2 U- S6 i& Q
    63.          
      * Q0 v# Q5 B" f' Z. I# y
    64.     <aop:config>  % v7 I5 A) P0 n( _( s( z
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      # A6 u' F/ C4 z5 c, n# T) W
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
      ' k4 O8 X2 s5 A$ z  b# l. E
    67.     </aop:config>  / @: K+ _6 J9 }* ]6 M2 H6 H: a
    68.       
      8 s! I% Z1 I1 X! H. `9 u* q- P/ b
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
      5 u% G; R) s5 a! I) v# q9 e
    70.         <tx:attributes>  
      1 j  l5 y9 K/ q- x) H) o2 I
    71.                 <tx:method name="save*"   propagation="REQUIRED" />
      9 M( S. Q/ d# N; H  `$ b
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      8 `7 z1 ]  e- }0 _! g
    73.                         <tx:method name="create*" propagation="REQUIRED" />6 n5 Y7 U: w$ ]% Z" V, V
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      ! J9 X& N7 u5 S1 Z
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      : ?* F1 c; u! p$ V$ }9 X9 w
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      : f. G1 |, e. c
    77.                         <tx:method name="del*"    propagation="REQUIRED" />
      , q1 Y8 t* w% t) f
    78.                         <tx:method name="remove*" propagation="REQUIRED" />
      # @8 v6 u; g) j% k4 l
    79.                         <tx:method name="put*"    propagation="REQUIRED" />6 W5 y2 k( K# o  v3 ~9 H8 A
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  6 h8 e, Z3 H2 _
    81.         </tx:attributes>  5 G) V6 O' ~9 W
    82.     </tx:advice>
      # h- v/ D% j/ R
    83. </beans>  
    复制代码
    spring-mvc.xml:/ A% l, T4 J, y/ _8 n* H1 e, G0 U
    4 H& E1 D4 n" G" s; i
    1. <?xml version="1.0" encoding="UTF-8"?>
      * y' H) I, Z: F; L" ?9 O
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      6 c! D! f. E+ a$ S4 Q
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"5 Z; x* S$ W8 t! J, ?
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; {' z' F8 P; l1 r; p4 R  P2 M
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"
      & I$ |8 U; _' L! G6 o, P
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      1 [2 F2 ]1 `! a( i5 x* |
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  8 J% J: w7 S6 Q1 l) \6 S. R
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      4 x+ ?# p, t3 v; {
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      , K/ l" W( q; A5 }
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd; r! p. }' O$ e$ {  ~* _2 d9 K
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      - D4 O1 I+ h# {6 K
    12.     <!-- 自动扫描的包注解  -->' ]4 ]- z( h) G( B# k: q( \
    13.         <context:component-scan base-package="com.hoodo.rci" />  Z) ^1 I- V# y* `
    14.         <!-- 自动注入  -->3 C1 ^7 p& K& ]$ a# R8 O! c
    15.         <mvc:annotation-driven />
      " G! X0 u$ m& s8 M$ t. ^
    16.     <!-- 视图解析器 -->
        ^, F6 E. W3 o8 \5 t! c  N; G
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      # N- S( s( J% z6 X# @9 P
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />0 M. f! Y) E) H
    19.                 <property name="suffix" value=".jsp" />
      8 X' z2 b+ q. d- p" c$ W
    20.         </bean>" s' H8 ~3 j; p
    21.         8 {; l8 O8 h% m) s) i
    22.      <!-- 异常解析器 -->5 c; h' d2 a! Z# T! C  T
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">5 J7 V/ }2 b# l2 M6 z, C$ U# I/ x
    24.         <property name="defaultErrorView" value="common/error" />% N+ @6 x4 x9 D$ P  X
    25.    </bean> -->
      * V+ u& X. x* R/ {5 v& P2 n" C
    26.      
      ' L- |) C: |" N1 l; x4 N3 X
    27.      <!-- 拦截器  -->  E  t0 D4 Y. l' H7 L/ I) c
    28.         <!-- <mvc:interceptors>
      3 y( s, `/ f6 R' s4 g# P  S( f+ O* n& l
    29.             <mvc:interceptor>
      , I+ ]+ W* P1 b* U+ i
    30.                <mvc:mapping path="/admin/*/*" />
      , J  N' v& H& w/ `% O3 B, U5 G! X
    31.                <bean class="com.filter.CommonInterceptor"></bean>; }8 `. G* O- i- e% W# L% p
    32.             </mvc:interceptor>, N! o+ o5 ~/ ^0 g. G6 c
    33.         </mvc:interceptors> -->
      * x* k, v4 L1 ]& A9 A$ @$ B, z

    34. 2 y$ A! \& ^- H5 R' i% D
    35.         <!-- 上传文件相关的配置 -->
      4 ?3 c3 }. i2 r/ I. L9 }
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">$ H9 W. k( f  i* u7 v/ L
    37.         <property name="defaultEncoding" value="utf-8" />
      . ^6 h- X0 Z4 X4 w+ z
    38.                 <property name="maxUploadSize" value="104857600" />
      , l1 o! X4 C0 e: M, f+ y7 j
    39.                 <property name="maxInMemorySize" value="4096" />
      " v8 {! U1 s# u* @5 @, w( `% k; ]
    40.         </bean>- b5 B# `6 Y4 L# Y. J2 w3 p* D
    41. </beans>
    复制代码

    # ~. ^* _$ e5 y! t; a9 A$ P, Y& Z4 X# U% [
    3 y; d5 k5 G+ {5 f4 ~/ [1 L8 e
    SpringMVC+hibernate4.3+Spring4.1整合案例
    . Z6 ]4 a& y5 L# S

    ( _# e7 X5 l1 ]$ H

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-03-17 21:46:03 |只看该作者
    ) t5 U6 ]; f. h& H% L$ }6 V
    下来,学习一下。谢谢
    回复

    使用道具 举报

    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 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

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

       

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