我的日常

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

动态微博

查看: 8318|回复: 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:
    9 S1 E  i0 A% N8 ~& H/ a( x$ h/ G! `- ~  D( u: l
    1. <?xml version="1.0" encoding="UTF-8"?>  
      2 R: |/ I3 f  |; ?! g
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
      % Z9 T/ j. ]5 M2 f, @
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      # z, J' e3 c5 c5 m7 |7 Q
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      & [0 a+ N7 E: A- `: t5 J. J1 Y6 ?4 s
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   / |! M  r/ M  U: U$ x3 ]
    6.     xmlns:context="http://www.springframework.org/schema/context"    g; d/ n1 r+ G% B/ T# s) C
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  6 M8 H4 ?! C5 R6 n1 S: r) Z
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  
      $ @0 y6 T2 E5 p( ^* d2 W; s
    9.     xsi:schemaLocation="    7 U" C/ `2 d+ k1 |9 Z
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      ( [8 H  h2 @( N2 w0 ?! E$ p  M" b. N
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  # I9 Q# x! J$ }; G1 g$ |2 t/ V
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      . u$ D/ M; K: r9 g
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      % r5 L1 F; `0 Y5 x- a' D- S9 z4 k5 n
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ' [- ]1 `" d0 j. V6 \" c. t
    15.    
      % K. U  o( g" N/ e
    16.     <!-- <mvc:annotation-driven /> -->
      6 |0 K& K0 \7 p0 [
    17.     <!-- 【配置视图解析器】 -->  6 P9 u8 u/ ?. T  x* }
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  1 s$ w/ J  K- h
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      3 a7 {$ K; f, j4 t, m5 C
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      : I3 n8 ?& }% l& Q. M9 j$ j+ M
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      . A: c8 X, K$ T$ W
    22.         <property name="suffix" value=".jsp"/>
      , D5 _# A7 `$ Y2 J0 l  {. [$ P" B2 C
    23.     </bean> -->/ H2 L- D7 O+ @% u2 f0 s
    24.     + u/ t+ G7 o3 l! |
    25.     <!--  导入springMvc.xml配置文件 -->
      ) A( y/ l6 s5 f1 a" t: n
    26.     <import resource="classpath:config/spring-mvc.xml" />( Y4 \  ?% h1 L
    27.     <!-- 加载数据库配置 --># p. _) S3 ?4 i
    28.     <context:property-placeholder location="classpath:config/db.properties" />: J' l8 f' [: [5 k+ ]* x
    29.         <!-- 加载数据源 -->
      4 ~# ^: h8 F* Z6 S( y! v
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">" N, I: p2 W: |+ x5 Z
    31.                 <property name="driverClassName" value="${db.driverClassName}" />8 s+ l" @: t; `3 Q: }/ _  a
    32.                 <property name="url" value="${db.url}" />; Y. p* L) H( [/ ^
    33.                 <property name="username" value="${db.username}" />
      + W! ~( g# y: y: E* }" C
    34.                 <property name="password" value="${db.password}" />
      * g, F" _1 t- F/ J
    35.                 <property name="maxActive" value="${db.maxActive}" />
      : e, Z/ @4 W7 b
    36.                 <property name="maxIdle" value="${db.maxIdle}" />& p* Y6 Y/ q+ \, O% Q6 I2 q1 O
    37.                 <property name="minIdle" value="${db.minIdle}" />& I, `- u( p6 ~- @* c
    38.                 <property name="maxWait" value="${db.maxWait}" />
      & ]# Q% `9 C% w5 p( `, k8 ]
    39.         </bean>
      $ A6 r3 g3 t8 j- R( |# u6 {
    40.     4 B; X. g% s  D. ^
    41.     ) a6 F& T3 M1 y' b7 ]" v
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  ( w' [- k5 C/ B7 E: y
    43.         <property name="dataSource" ref="dataSource" />  : j- h. D- C* }5 E; X
    44.         <property name="mappingDirectoryLocations">  
      3 X5 @# a0 m" [% X
    45.                   <list>
      : q& {' e: G: N, C; j
    46.                                 <value>classpath:config/hbm/</value>6 y  c& [2 @1 x6 ]7 o1 F# b
    47.                         </list>, ~- m; I2 W/ }2 L" b( U+ l3 l
    48.         </property>  
      ( T  X6 r+ d  S6 E! s) C+ R
    49.         <property name="hibernateProperties">  - i9 r" x2 l! X# j* @: q$ @* Q4 U: q
    50.             <props>
        A6 w9 x( I0 Y
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      ; P: p6 W+ l% c# ]8 \4 `
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>! f5 Y" @1 G* U) D  l, }9 F
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>7 d' D4 U/ e+ F
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>--># T* @8 \) s1 s2 q) L) r
    55.                                 </props> . E* q2 Z" \0 W" I
    56.         </property>  ' H; O  w" g2 p
    57.     </bean>
      $ ~6 r+ w% c8 B4 q1 F
    58.    
        \1 `, U* U" w2 k
    59.     <!-- 声明式事务 -->  & h; {; M. U. C  R
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      & N, S+ w8 D8 h3 d' ~7 C
    61.         <property name="sessionFactory" ref="sessionFactory" />  : R! }8 r: Y3 |$ _* d6 Q' Q4 P
    62.     </bean>  
      ( U6 N7 A, S3 S
    63.           % v- A0 W* V. Y+ R' ?
    64.     <aop:config>  / B; R; ]2 d% b/ i3 T
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      5 ?  ^1 X4 O# w4 N$ l8 F7 R
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  
      2 s8 V/ c% p& |% h
    67.     </aop:config>  + j+ J! m0 T1 q% x) p0 v2 n
    68.       
      6 C3 u1 C9 }# W* Y; \  {
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
      ! b2 _3 V5 r) ^3 O* \/ ~0 a! [
    70.         <tx:attributes>  
      ( y2 L/ \2 v/ O/ o& E
    71.                 <tx:method name="save*"   propagation="REQUIRED" />0 k# B! ]; I( O2 O/ i$ m2 H+ c9 S
    72.                         <tx:method name="add*"    propagation="REQUIRED" />. I, J+ Q% ^7 c! V2 _$ R& l
    73.                         <tx:method name="create*" propagation="REQUIRED" />5 W" @3 v4 L. @* _8 \2 }
    74.                         <tx:method name="insert*" propagation="REQUIRED" />! ]4 x$ v, U) l) u% u
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      & o+ V) {- |; t/ y$ F8 @5 i
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />, c3 [- E4 G) }
    77.                         <tx:method name="del*"    propagation="REQUIRED" />" Y( r+ B( P$ ?- M" u
    78.                         <tx:method name="remove*" propagation="REQUIRED" />
      % ]4 }! Y4 x2 t/ X% \  U2 n
    79.                         <tx:method name="put*"    propagation="REQUIRED" />- I. y0 |; T( E2 ]! ]
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      ' E$ k- E  ~, K
    81.         </tx:attributes>  8 \; W8 Q# n, w5 R2 R7 F
    82.     </tx:advice>
      0 _" k; s; ?8 w' o
    83. </beans>  
    复制代码
    spring-mvc.xml:/ g- j" y7 E# f# D
    6 s6 C- h! s1 J1 j
    1. <?xml version="1.0" encoding="UTF-8"?>5 N# F4 S3 D' z# Y! b  x& V( E$ ^
    2. <beans xmlns="http://www.springframework.org/schema/beans"" |% [- i) o  A# ^
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"7 S9 J3 t0 v: c% w. R+ ~
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      2 b/ }- [$ b0 R. B; n+ s
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"" v+ S# r" c- _, A
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      ! H$ Y+ }- U3 x& n. j6 `
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      ! L* p- k* H) L* Y/ r7 }+ D; u
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  ' ~+ o8 l2 L4 M) I& f: M3 S% d, {
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      1 B* t* ]. J( d  B% i2 ?
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd* u, P- x: s2 O/ V9 y
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      - ^- `7 u7 e% O7 I: \3 \
    12.     <!-- 自动扫描的包注解  -->
      3 j0 `6 B0 T# x+ J
    13.         <context:component-scan base-package="com.hoodo.rci" />7 x. C% @0 [) ?! x1 }, `) O
    14.         <!-- 自动注入  -->
      $ A& X2 D( O' n( Q
    15.         <mvc:annotation-driven />  [3 {9 Q$ S- j9 t
    16.     <!-- 视图解析器 -->. g) S. v% B+ X, q. c
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      9 o) d9 |. v; t- g
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />& N0 X+ r. ~$ Z9 Z6 ~: y& R
    19.                 <property name="suffix" value=".jsp" />, j  s! ]/ \, D: S7 l8 X
    20.         </bean>
      / |0 ?6 {% n7 S8 d3 o* v* S6 M
    21.         % W4 X4 N  _& H/ x; \
    22.      <!-- 异常解析器 -->7 d8 H" `! X, c, B& D. V1 a4 F- `
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">( G  T: I/ |! C8 O: Q; S  {. L+ C2 z6 Y
    24.         <property name="defaultErrorView" value="common/error" />+ O2 W% v* p& S: |% k& I: `
    25.    </bean> -->1 V9 M8 g" C3 m$ A4 m. h) t) I
    26.      ! I( P- B; h7 N9 N+ ]
    27.      <!-- 拦截器  -->6 G2 i0 b' |( \9 H' `4 v/ ]( H
    28.         <!-- <mvc:interceptors>2 b- g7 X* v2 l5 @+ l% z
    29.             <mvc:interceptor>0 _  M6 m7 [# s2 w
    30.                <mvc:mapping path="/admin/*/*" />) D2 l" S5 k) ^% u, ^$ o% A( {
    31.                <bean class="com.filter.CommonInterceptor"></bean>. H; H( B2 c% K, O
    32.             </mvc:interceptor>
      ' d( b. c' e: K6 \; P0 p
    33.         </mvc:interceptors> -->. w6 I) n& ?# x& Q% I' ?  O

    34. 6 T; h. S4 d$ l- h
    35.         <!-- 上传文件相关的配置 -->5 w4 h; P; h5 T# L: b
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      * \3 S( J2 j$ Q  w  C4 ^) J
    37.         <property name="defaultEncoding" value="utf-8" />
      $ z- U& N  ]6 Z5 F$ J! p5 O
    38.                 <property name="maxUploadSize" value="104857600" />9 w7 O( E4 |5 R7 M
    39.                 <property name="maxInMemorySize" value="4096" />6 _: U# h0 G, h, W5 q  ?0 r1 [( m$ T
    40.         </bean>! Z  k2 z6 o, m. R7 }+ `: J
    41. </beans>
    复制代码

    & g8 Y$ z1 l7 R
    8 K; s+ c* M- `6 ~- o) A; k
    / T1 x' f* m* d' K' H7 eSpringMVC+hibernate4.3+Spring4.1整合案例
    5 Y; c) z3 p7 I7 T, X  l

    1 ~7 e# L4 A9 Y# F4 Q

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

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

    ! b. I; e8 O* C* k. s( K$ X: 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 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

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

       

    关闭

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

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