科帮网

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

动态微博

查看: 8640|回复: 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:7 |3 A6 {* ]7 Z: Z& c$ \4 B
    - p0 H5 T8 C% K2 m6 ?
    1. <?xml version="1.0" encoding="UTF-8"?>  # F0 V  q8 |& G2 S/ L, A
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
      ' D: w: L) z0 t  t
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   - W5 D  a0 M  i  L: C  b( w7 ^4 m
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      - R+ d/ j' W) Z
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      + G( `! R$ o5 z" [. u7 @
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      ( G$ O. S( `1 w5 U# Z4 r5 K
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  5 P0 |) ]% ?2 j" y/ p; j; M
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  2 R9 E- A: v7 h! n( I9 [6 m: W+ M
    9.     xsi:schemaLocation="    . n2 V6 ]5 P7 h2 C3 m) Z- `$ J
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      ; u, H3 J/ W, p! Z# D9 A- V
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      1 s( z1 W! T3 P8 F! S: k( a8 w3 e6 [
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  1 A8 G' s; D% ^) g! G0 H0 r
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  9 E  U  `, O: p! H
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> % Q4 O& k/ W. F" y$ D! T
    15.     1 \$ C2 k! w" K4 S" R) Z
    16.     <!-- <mvc:annotation-driven /> -->3 C: F5 C" N5 _7 R
    17.     <!-- 【配置视图解析器】 -->  
      ( M" e6 d  ]1 c0 M( T* \
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  2 p, j  b/ z" y) ?
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  * }9 N4 e1 B! u* b# F9 k  S
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  " ~, d+ Z1 C/ ~" M+ u) W, M9 Z
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      9 t7 \* r; E; g) Q
    22.         <property name="suffix" value=".jsp"/>
      3 k+ q- Q9 C/ f+ x; p& z. u1 e! f
    23.     </bean> -->; x% U. h7 s% g4 G( O# m
    24.    
      * [- ?+ M; c- K1 _# N5 `+ i5 ]
    25.     <!--  导入springMvc.xml配置文件 -->. I! Q7 n8 ~# ?# v) D
    26.     <import resource="classpath:config/spring-mvc.xml" />
      - r' M$ _! G1 M! W
    27.     <!-- 加载数据库配置 -->
      3 R5 n% w/ k" {: C+ c7 F
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      ( [3 T: b$ t3 A! \/ D2 P8 l) p
    29.         <!-- 加载数据源 -->9 Y) u, N* [& J/ {
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">% q# w0 ?  S- @( E8 }
    31.                 <property name="driverClassName" value="${db.driverClassName}" />) j) Z& j, }. F8 Q7 P: R- R0 @
    32.                 <property name="url" value="${db.url}" />& s; Q6 `- i  D8 S
    33.                 <property name="username" value="${db.username}" />+ T+ n; p& W; K: S- b  C6 t
    34.                 <property name="password" value="${db.password}" /># }( u# d' _' z' x5 I
    35.                 <property name="maxActive" value="${db.maxActive}" />) ?6 Q7 E/ {1 F2 k; @4 L. X2 o0 {
    36.                 <property name="maxIdle" value="${db.maxIdle}" />
      1 {& r6 G; q  p9 _" K" r+ P. w
    37.                 <property name="minIdle" value="${db.minIdle}" />. I3 U) g* F/ t" R" t
    38.                 <property name="maxWait" value="${db.maxWait}" />1 f7 Q+ @' |9 {1 g$ I
    39.         </bean>+ J! W) N- [' A# [: \6 \$ Y8 p. o
    40.     5 B- t& R/ ^" P6 O' @$ G
    41.    
      2 Q& T8 [! U5 T2 d; S: L+ V
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      ' A' d! m" y; |6 J
    43.         <property name="dataSource" ref="dataSource" />  
      4 d; Y: Y8 l; N! Z
    44.         <property name="mappingDirectoryLocations">  3 M6 q, H2 a' I' U9 F
    45.                   <list>; m3 y' r  j5 o9 e8 S! S0 A* h
    46.                                 <value>classpath:config/hbm/</value>& A7 m+ a0 e5 c" S8 Z# q
    47.                         </list>
      4 Z  r, O9 N+ c
    48.         </property>  ' M5 z, A" k* |& J9 Q  y
    49.         <property name="hibernateProperties">  
      2 H* d; }  p1 e. g
    50.             <props>
      5 l# G  u+ d7 g  |
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        N8 ]; S/ W+ H8 g4 T
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>2 `8 Q  Q: C" A* @4 U. K" e
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      , v& O9 b1 z, s2 X+ v, d
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->9 J7 z4 E  A; x+ L/ x$ C8 F2 c
    55.                                 </props>
      4 d' z! @& p: c& G3 h% B) {
    56.         </property>  / x: h+ `4 e/ q0 X  l
    57.     </bean>6 D9 l  v  `  C' m/ J
    58.    
      ; h$ a' Q- j& ]; `4 i% y
    59.     <!-- 声明式事务 -->  7 b1 c( s4 {3 o( d" c% [3 _
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  2 X. R8 M# @' }& l4 d; Z6 ~# C
    61.         <property name="sessionFactory" ref="sessionFactory" />  ( g+ y& I0 x9 m+ j
    62.     </bean>  7 E5 Y! B; c1 {
    63.           ' U0 ~8 ~; k; h# l
    64.     <aop:config>  
      / [1 k5 f6 t6 S, J+ d( v' N3 J3 _
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  4 R, t3 w/ k2 q* D- W
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  ! E+ N" K8 k# \2 @& u3 w& m
    67.     </aop:config>  
      & F5 o! I1 z, u$ P' h
    68.       
      1 N+ x9 n: ]) ]+ r* i, b* F9 O4 Q2 K% ?
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  " |6 T4 S) N$ s  f
    70.         <tx:attributes>  
      ; y: x, B& f4 y/ E2 [3 o& B
    71.                 <tx:method name="save*"   propagation="REQUIRED" />0 ?  b& O0 Q- Y3 B2 J
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      4 f8 ]) a$ e: Y* p! c, t5 c
    73.                         <tx:method name="create*" propagation="REQUIRED" />2 k7 O. W% s( V& s9 r# o- Q
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      " @/ i& I: g* ~) x
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      # J( [0 q' d% m5 ]+ m
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      4 i, S6 y/ R$ i8 w1 ^
    77.                         <tx:method name="del*"    propagation="REQUIRED" />7 o1 R: \( p+ X* I0 n; Q
    78.                         <tx:method name="remove*" propagation="REQUIRED" />
      $ X  o7 K5 C# n* k2 j$ ^
    79.                         <tx:method name="put*"    propagation="REQUIRED" />
      9 U9 u! I! |/ ^9 k/ i) ?7 o( T
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  / W  G8 q  Z& K1 j* p
    81.         </tx:attributes>  
      9 Y5 R; i$ c8 S; Q
    82.     </tx:advice> 5 b3 S; o2 I& m% D" L5 [
    83. </beans>  
    复制代码
    spring-mvc.xml:
    ( V+ |- {: I! l3 {5 t2 h
      X& s8 u; g1 g* G- \
    1. <?xml version="1.0" encoding="UTF-8"?>
      7 Z  e# M, u# M7 h$ W; w  A# f" d
    2. <beans xmlns="http://www.springframework.org/schema/beans"' D5 z# l5 n. {0 ^2 c  X6 C
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"' v1 [2 T- `/ P
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; {4 r9 ]  T7 [3 t- C4 J
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"
      % w9 {* @2 o' _. ^8 ~6 t
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      9 z( b: b$ W' K; K: H0 r6 @" i# X
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      * `  l, u, R) C! `" Z! l+ f, [0 j
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      : A5 g7 R$ O. \+ P! P/ m
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      " Y8 ?* I8 |9 f+ O
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd# m9 |. N7 e- M# m
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">+ {& F3 X, Y, a! }# G0 J3 B
    12.     <!-- 自动扫描的包注解  -->
      6 |1 Y5 Z! o8 e0 u( u
    13.         <context:component-scan base-package="com.hoodo.rci" />  A; {) i- M2 W& Y/ e, I8 e4 r/ R* k3 K
    14.         <!-- 自动注入  -->
      ( C! @% R1 K6 ^1 X
    15.         <mvc:annotation-driven />9 Y7 Z0 k8 R3 A/ W  x
    16.     <!-- 视图解析器 -->
      * D( x+ K& u" w9 ?
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">$ P4 ~, _; z  t; _2 Q7 U
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />
      : `* P* F! L& X1 P1 k: e2 u$ R: b
    19.                 <property name="suffix" value=".jsp" />
        i3 E& w! H) _8 W* r
    20.         </bean>
      * I% S) l. r, ?
    21.         & x1 F1 ^5 j4 a/ F' V, P" p( p
    22.      <!-- 异常解析器 -->7 d5 N) d# C- d' I; i
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">5 j- K" E* P+ F# W/ N
    24.         <property name="defaultErrorView" value="common/error" />$ P, E3 H- o( }2 x
    25.    </bean> -->% f: j5 O' N; ~% `; w: ~+ F3 [
    26.      % G! u: G7 d' B, _
    27.      <!-- 拦截器  -->
      . d4 e2 `2 s7 q& H- C# R
    28.         <!-- <mvc:interceptors>
      2 Z5 r( M8 r6 q" e; Z
    29.             <mvc:interceptor>
        Z  d4 i6 Z6 |" `" D) v# E
    30.                <mvc:mapping path="/admin/*/*" />2 M* ~6 O: c2 N3 D1 q+ k. T
    31.                <bean class="com.filter.CommonInterceptor"></bean>
      & s9 C& N" x) c  d% D
    32.             </mvc:interceptor>
      6 v0 ~! J& o2 ?
    33.         </mvc:interceptors> -->
      / X7 x. I8 j- y/ z8 A

    34. . ^# y$ ^" \+ I% s$ x) q
    35.         <!-- 上传文件相关的配置 -->, o/ v4 W& s; u. d2 s- t0 W
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      ! p4 g/ ^9 n8 J: ]
    37.         <property name="defaultEncoding" value="utf-8" />
      ) H" q; M! k3 L. [4 O$ U- W5 N
    38.                 <property name="maxUploadSize" value="104857600" />
      : ]* S8 s2 G  s) }2 _+ s1 |
    39.                 <property name="maxInMemorySize" value="4096" />8 G8 N7 W; ~2 z- c9 a/ |
    40.         </bean>
      + e% `) h' \. z+ c9 |' e: z6 H- \
    41. </beans>
    复制代码

    3 ]; @" `- R9 t  `6 ]0 K- G( M5 X3 y/ W$ u8 w" [5 Z$ Q. L
    " k+ n6 k; d) M) v' C% `! \
    SpringMVC+hibernate4.3+Spring4.1整合案例
    ' |* X4 J# l6 E8 h' f( N) V: ?% I+ E; b! E

    - d+ d1 ]( F/ _2 t$ Z$ g$ i8 w) K

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-03-17 21:46:03 |只看该作者
    6 b' K7 \8 K% ?; y& d
    下来,学习一下。谢谢
    回复

    使用道具 举报

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