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

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

动态微博

查看: 8179|回复: 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 |9 O+ q& r$ F7 u9 X' D2 u

    * G4 g  C: _! `: L2 j# U
    1. <?xml version="1.0" encoding="UTF-8"?>  
      ' C, V. j% q6 ~2 y
    2. <beans xmlns="http://www.springframework.org/schema/beans"    - {. G2 D9 q3 o( c  x9 r
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   / N8 c! {" C9 U  b+ n. O0 k8 s6 C
    4.     xmlns:p="http://www.springframework.org/schema/p"  , ?( _. B+ h% _* {9 x; B1 A' e
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   9 d$ m# e* a( R6 l3 K1 l5 m
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      5 K! S  T5 w1 A
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  ) A6 o0 i* J  ]$ ^! t/ H# s, E9 s
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  5 K) Y# u/ E- _* e, m! ^  F
    9.     xsi:schemaLocation="   
      # y7 J, [3 ^* r$ B2 K
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  / V% M# }; |4 \; `9 \3 M. h3 V" M  |
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      3 R# e4 u( _/ ?6 y. T2 I- O& p
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  ! a  w1 a# U* A, r2 O4 `
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  8 S9 T  O6 c8 \
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> & i/ q5 \7 f5 N! L9 t; K
    15.     7 b' ?2 b$ r2 R9 E' f
    16.     <!-- <mvc:annotation-driven /> -->9 }* S7 m7 D/ d, {( e7 K
    17.     <!-- 【配置视图解析器】 -->  & Q5 p( H1 I  h# A* h; I
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
      7 f' m  }: z# o9 D: Q- n
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  0 i# g+ F+ }! b6 V+ z. J
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      4 p/ `7 [* |* q' }. k& f2 l0 P4 E
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  ( Y- y" z3 P9 y4 J1 {" o9 i
    22.         <property name="suffix" value=".jsp"/>
      # `2 P1 S, S# P: O* J
    23.     </bean> -->
      # `* Q- Y6 W' d
    24.    
      & o+ ?  n$ x( u0 s$ a- P! @
    25.     <!--  导入springMvc.xml配置文件 -->
      ( \1 m9 d. o$ v0 v/ i" k
    26.     <import resource="classpath:config/spring-mvc.xml" />
        S" ^3 }% c" L& B0 R, ?4 |
    27.     <!-- 加载数据库配置 -->
      # r2 @  E% C& u1 G, y2 a* ]
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      , d9 |' m/ t: }! W& P
    29.         <!-- 加载数据源 -->
      ; c& x. [+ {! b0 h9 Q
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">; H* c, _( z  N, \
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      ' D% \# Y# ?6 r1 F' d
    32.                 <property name="url" value="${db.url}" />9 t7 I, C! N4 F% R4 ~
    33.                 <property name="username" value="${db.username}" />
      * ~& O7 F% f9 I7 f
    34.                 <property name="password" value="${db.password}" />- r& u: r: |! v, J# @. ~  H, e
    35.                 <property name="maxActive" value="${db.maxActive}" />
      & ^. D, b/ d7 V7 }  o
    36.                 <property name="maxIdle" value="${db.maxIdle}" />0 [! v; A# k; H/ W9 m3 q' X! u/ F
    37.                 <property name="minIdle" value="${db.minIdle}" />9 N0 `% g# q' E% }* \' t6 C
    38.                 <property name="maxWait" value="${db.maxWait}" />
      4 N/ M: O1 x2 m9 n! u$ D
    39.         </bean>. n5 [: N$ K& w; `$ P9 b; A: ]
    40.    
      8 a! O4 H8 v9 i& L
    41.    
      ; r0 u; R! L. O# W9 ]. K5 I% B
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      ! @( B9 I! B' d4 H6 X
    43.         <property name="dataSource" ref="dataSource" />  
      6 ^5 q; }* r+ d2 J0 a6 \7 ~& W' [
    44.         <property name="mappingDirectoryLocations">  & [# ~  Y$ s6 {
    45.                   <list>5 t. j9 T9 n& Z9 }/ D  R
    46.                                 <value>classpath:config/hbm/</value>2 K7 I/ q" p7 Z2 ~
    47.                         </list>. Y1 m/ |" E" r0 f: v
    48.         </property>  
      9 l* e7 s+ `- b) {, {) j( |
    49.         <property name="hibernateProperties">  1 Z: P, C# w( V
    50.             <props>) w0 @7 P  C" [
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>$ x2 O4 L9 m: I: Z6 @
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>2 w0 u1 K$ d7 U* M8 ^
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      4 S, u, W( o# r, q
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
      2 x* X) U# T4 x2 X) S. e2 A
    55.                                 </props>
      ! n' e5 ?# I8 g9 `
    56.         </property>  
        A1 {: n- S( _6 V' s
    57.     </bean>
      6 E4 ?* u8 `, d9 k9 O+ \. E
    58.     8 i' O0 V+ H6 l5 \2 \5 j
    59.     <!-- 声明式事务 -->  8 i4 L* ^, J9 G# K# e5 K
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  : M! p. U. J# K$ d7 _7 i# @
    61.         <property name="sessionFactory" ref="sessionFactory" />  ( @# a. Q3 j' E! ?
    62.     </bean>  & I/ g6 ]2 h, K0 Y! |2 v6 y
    63.           * A' L& |, t! }' P( M5 V7 y; D
    64.     <aop:config>  & u$ J3 P! o- a+ x( k4 n. m
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      7 M4 z3 X+ r7 T3 K! a
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
      2 p. K  r# d/ b- C
    67.     </aop:config>  ) Q% {" ~8 E; O0 P5 h% S- x
    68.       / l) ?: o5 S1 U3 Z* c! R
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
      . o  B) Q% E( h  c( ^
    70.         <tx:attributes>  
      ) K+ s4 v7 M; \- g$ e9 l
    71.                 <tx:method name="save*"   propagation="REQUIRED" />4 k% Z; `8 A, B9 }1 g& \: K
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      4 k0 `; t) o8 \: Y; B6 O0 Q! x+ L
    73.                         <tx:method name="create*" propagation="REQUIRED" />
      : E9 }8 q1 m2 W. Y% w
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      6 J: n, v8 }% e
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      ! j  m! S1 u, {
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      8 @- o6 n( _, p' V' t9 f
    77.                         <tx:method name="del*"    propagation="REQUIRED" />
      3 j: D! E( H& b8 C( ]! I
    78.                         <tx:method name="remove*" propagation="REQUIRED" />$ g4 s1 T2 d" p7 e# E. K+ {4 A3 a8 m
    79.                         <tx:method name="put*"    propagation="REQUIRED" />: }' {! k& V7 m! i
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      ; X. ^0 K' X* C+ b
    81.         </tx:attributes>  4 s8 O3 `+ z5 L% j3 M
    82.     </tx:advice> - w# W) e' }. U3 s0 ~
    83. </beans>  
    复制代码
    spring-mvc.xml:0 e+ M6 p# G3 f& B1 r

    5 ]3 {1 k, w+ P' x% S4 [
    1. <?xml version="1.0" encoding="UTF-8"?>7 f% c1 r! h: b# y9 Y$ }) F" B
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      & D" B+ x% C; W% _
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      ; x, g* @# |! ^, i) v# O2 a! L
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"" p  T3 b; U- B6 \( a/ v* I" P0 l) n) ^
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"
        Q6 }! Z% J4 Y# l
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  " w3 ~% h: |' B; Z8 O
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  ( Q: g  L) l/ I8 ]+ n
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  4 [, ~: ]" ?; P  d6 K/ o
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  ) c: U- G; ~; y& W" D; B* v2 F
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd6 k+ x9 c) F+ m. }- b/ X/ t
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      $ O7 c2 q3 @+ C6 `" B
    12.     <!-- 自动扫描的包注解  -->
      ) ?1 C5 \6 p# g' v! J& y( S0 s
    13.         <context:component-scan base-package="com.hoodo.rci" />4 a; Q, }: C1 c+ m7 |- y
    14.         <!-- 自动注入  -->
      # e% ^9 m* ~7 a0 x: P3 N
    15.         <mvc:annotation-driven />
      ( Z) w9 h. _1 D" c" N& v
    16.     <!-- 视图解析器 -->
      7 n5 ?8 ~) ~: a6 L5 `& O: B( d
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      ( T. G7 a# V3 T5 ]% o
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />2 s6 o+ l# a6 J2 m. W5 m( l7 q. U
    19.                 <property name="suffix" value=".jsp" />
      . \5 {) i$ H* ^# O& V
    20.         </bean>4 H$ |% |3 T0 Y: ~9 Y! g
    21.         
      5 c& X4 D% Q7 J" T/ Q1 \- [
    22.      <!-- 异常解析器 -->0 m- i$ l& a2 ^: z; ^
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      6 I0 Y: f8 R- ~2 S! [7 R
    24.         <property name="defaultErrorView" value="common/error" />
      0 W, j- |5 E' e; j8 s  }- c
    25.    </bean> -->1 v9 q' x  N' i( X
    26.      5 b. a; C! O4 w: H
    27.      <!-- 拦截器  -->
      ; V$ N) P: L( j2 R* `: X
    28.         <!-- <mvc:interceptors>$ C/ S) r: z$ z: d9 d; Z: C
    29.             <mvc:interceptor>3 u3 S( P6 V# J$ V
    30.                <mvc:mapping path="/admin/*/*" />- ?8 S1 _5 W% Y. J2 n5 H
    31.                <bean class="com.filter.CommonInterceptor"></bean>
        c9 }$ f! V& U- V' X
    32.             </mvc:interceptor># o# u: A5 N4 o
    33.         </mvc:interceptors> -->) W! q8 i( O: d7 [7 w  T

    34. 2 n& S$ t4 @8 P# P1 D
    35.         <!-- 上传文件相关的配置 -->9 ?/ \8 e1 O) ^5 j4 l, }, @
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      3 }' h3 a2 m3 k; F, X, ^
    37.         <property name="defaultEncoding" value="utf-8" />
      ) I. u; U1 L" _# i  G6 c$ t
    38.                 <property name="maxUploadSize" value="104857600" />
      2 M2 L4 [" }, @& M! }6 a
    39.                 <property name="maxInMemorySize" value="4096" />
      + x1 r- b  @) g+ V" \
    40.         </bean>/ y8 F' ?& V. B! I
    41. </beans>
    复制代码

    - p: {6 t0 _# s( k8 D; U5 e9 Y$ s7 N2 m

    : q5 ]8 s0 d% u, ~SpringMVC+hibernate4.3+Spring4.1整合案例

    # L* _/ m: s  X5 l- r; U  V2 m. I! a& p

    科帮网-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 |只看该作者

    6 ]6 |( Z- L8 }/ b2 r下来,学习一下。谢谢
    回复

    使用道具 举报

    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群 科帮网手机客户端
    快速回复 返回顶部 返回列表