科帮网

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > Spring4 整合MongoDB详细讲解项目源码
总共48087条微博

动态微博

查看: 3933|回复: 3

Spring4 整合MongoDB详细讲解项目源码

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2015-03-10 20:24:53 |只看该作者 |倒序浏览
    spring4 整合MongoDB详细讲解项目源码
    ( O" R, p* V$ f" h! J; OMongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。/ K* g* U& x3 U. t
    / O, b2 m) x1 C2 m3 s& F  o
    NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。, B: U+ W9 F3 O, d2 @

    ! {/ S9 N; @: Q: e, QMongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。( I4 r4 ]3 E- ?3 i( w9 f) G

    $ \" H& N* z  k* ^! H! ]0 Y8 k环境配置: myeclipse 8.6  、mongodb 3.0  、spring 4.0.6+ P; ]8 U; a1 }6 K$ s

    + o, f; |7 F! z; X
    2 w+ t1 ^: ^: [. f, F项目采用springmvc 结构。: ~- V: ~# m# g5 m. \9 k- M  [
    所需JAR包:% t' @, V# c8 G& ]
    1 z) M# x: }* Q3 @$ q! P7 l* K" v9 f
    6 ~' y' ]9 j; O: t4 K

      T! p: X! d. g8 a. I" Z2 TapplicationContext.xml配置:
      a/ _/ u7 w" V- A+ X8 ~
    1. <?xml version="1.0" encoding="UTF-8"?>  / E: I. s7 P* c, {$ `- h; `4 o
    2. <beans xmlns="http://www.springframework.org/schema/beans"  * p* A! l8 E8 y
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      5 o6 @1 @- w. b" g1 ]* X4 a% b
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      ! `' |3 [: O$ L9 s. b* d2 n
    5.     xmlns:context="http://www.springframework.org/schema/context"  8 q2 m3 u8 ]( m: m* b5 B% ^* H8 X
    6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
      1 f8 r5 M* h! v# w2 Z: _9 R
    7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   5 g- h/ C: [& H6 q" z' A" H
    8.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  + T; V2 _3 v4 }, V
    9.         http://www.springframework.org/schema/beans
      3 |$ r0 r: v7 f. O9 V; R2 S
    10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  $ l7 W9 b) e3 o8 q/ i  ], R
    11.         http://www.springframework.org/schema/context . a7 B- p! B6 ]1 _, H5 x
    12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> * P$ Q1 O( }  `9 c2 ~$ r
    13.      <context:component-scan base-package="com.mongodb.service" />. H: I* }* s) o! {
    14.      <import resource="mongo-config.xml"/>
      1 o. L) c: m2 J2 ^" ?
    15. </beans>
    复制代码
    mongo-config.xml配置:* y5 J" y$ c2 w+ q" e& j& C( \8 @' y
    1. <?xml version="1.0" encoding="UTF-8"?>  
      " n* O5 N1 Z7 a0 @3 a
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      4 L* r7 P8 X4 _8 S* U0 |! m
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      ! d- M9 H6 J/ T8 B9 v4 B  s( {
    4.     xmlns:p="http://www.springframework.org/schema/p"   
      3 \6 E. O+ U+ A0 `0 h
    5.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  , Y: ^. M) ]7 `
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      ! n( O$ n# A( P6 X
    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      ' k- z. P, W2 I$ W1 G. l, n/ F/ J
    8.             http://www.springframework.org/schema/data/mongo    D  `4 D  r$ B
    9.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd* n( ^. a6 n- L
    10.             http://www.springframework.org/schema/data/repository0 O1 U. ?3 p3 k: e) s% Q5 S% ?
    11.             http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">        * T% r% e* |( K( F( P3 N& l" g
    12.    
      : S! P6 s! J  _- [
    13.     <mongo:mongo host="121.42.26.72" port="12345"/>  ( D7 e  }" ^! _; L5 e" m  x
    14.       # U7 L8 l0 H. W9 Z8 G
    15.     <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->  7 R& _& B7 ?  @4 V
    16.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  : q# S6 C0 K# p( u8 K% i
    17.             <constructor-arg ref="mongo"/>  
      , M8 K, z, p" u5 T
    18.             <constructor-arg name="databaseName" value="itstyle"/>  1 J/ Q/ A( R  E9 `: G& d
    19.     </bean>  
        G1 I3 ^/ E4 ]4 a! G  l8 T7 ^+ S
    20.       2 d: M4 e# c7 W3 w
    21. </beans>
    复制代码

    + |8 \% A7 s' D& T2 bspring-servlet.xml配置:
    3 X; W+ @4 b) C' ?' ]8 Z
    * K- i7 n6 Y6 R& v
    1. <?xml version="1.0" encoding="UTF-8"?>  9 b/ g2 E/ T) C
    2. <beans xmlns="http://www.springframework.org/schema/beans"  ) T* L0 R& X  x; ^/ `4 D
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      $ V2 y- _0 _* j# s6 r# Z
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      / E9 q6 Q* ^! v4 a
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc" ( X0 [; j8 n0 B
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      - @$ u' J1 u4 t: f, ]
    7.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  . H8 e! H0 W, ~, z, ~% x) Y
    8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      . O8 u7 v" k! t3 V% J" m/ @6 ]
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # u. ~) W+ a& s+ j
    10.             http://www.springframework.org/schema/context
      ( L& {0 G  w, h4 ^5 b
    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd
      7 d! n/ W! ^0 D& W
    12.             http://www.springframework.org/schema/data/mongo  
      6 n# b* I, O# B2 q* H
    13.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd' k" y0 N4 v9 g' h" E$ P( T
    14.             http://www.springframework.org/schema/mvc        - I# x# v$ j% S* p* M! Z
    15.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">) L6 K" T* p0 o
    16.         
      4 S/ t% }  ?# k. X; Z2 {: q% U
    17.     <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.For example @Controller and @Service . Make sure to set the correct base-package-->  
      ) a6 H9 r! @+ F; E: @) t2 `/ f
    18.     <context:component-scan base-package="com.mongodb.controller" />   , O% J) N+ D% X' g
    19.       0 @- N; A: T; ~- v
    20.     <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->  
      & f' Z! m, o6 }3 S) H. ]% A: q: |
    21.     <mvc:annotation-driven />   
      9 T% H! P! @3 X! c
    22.     * f+ I0 v+ s4 g& o; V6 S- a9 F
    23.     <!-- Declare a view resolver -->  & f  R0 a+ y8 S
    24.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"   
      % V$ d3 i+ |0 \0 r& U
    25.             p:prefix="/users/" p:suffix=".jsp" />  $ j5 X  H1 r( J
    26.             . [; ~3 o- ~' G' O- v
    27.    . O3 O* \: Y( j5 P& o, I
    28. </beans>
    复制代码
    UserServiceImpl 基本实现代码:
    1. package com.mongodb.service;
      ) E( }5 y- ~$ x7 C$ A
    2. import java.util.List;
      5 ]% Q& \9 }0 B- l$ d
    3. import org.springframework.beans.factory.annotation.Autowired;
      + r: N5 t, K: m
    4. import org.springframework.data.mongodb.core.MongoOperations;, D% t( ^" c: Y0 u$ s0 y
    5. import org.springframework.data.mongodb.core.query.Criteria;4 ^7 z: @+ h0 @. g$ z, u
    6. import org.springframework.data.mongodb.core.query.Query;, [( Y( A; N2 @  b5 W
    7. import org.springframework.data.mongodb.core.query.Update;
      9 D. u5 L# j$ b  M8 W
    8. import org.springframework.stereotype.Service;' b4 s# d# M& d4 b* w5 ]
    9. import com.mongodb.domain.Users;% [- S' w* \2 I
    10. @Service(value = "userService")   
      ' s/ a; J, s8 y! z0 s, Z* A7 v
    11. public class UserServiceImpl implements UserService{ ) ]8 _4 o  i, J, |' j
    12.     private static String USER_COLLECTION = "users"; # A8 S  n5 `! ?/ N4 q7 ]5 m0 X
    13.     @Autowired  0 ^1 i% A; b/ J9 _% z9 u: R+ T7 }& c
    14.     MongoOperations mongoTemplate;
      2 k' A  R) c7 Q8 M" g  P
    15.        public void saveUser(Users users){            
      - ^/ H9 b: h5 |
    16.         mongoTemplate.save(users, USER_COLLECTION);            8 h# ^) U/ G( Z- l$ l
    17.     }        % t- ]5 v: V+ T4 E8 Z
    18.     public Users findUserByName(String name){
      3 `$ s! n  [/ R* C' g6 k
    19.         return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);  
      / }; U. x& h+ V" c1 m8 C/ V% ?
    20.     }
      0 @; t' d9 m2 a( [' n# @
    21.         public void removeUser(String name) {8 b4 \9 N5 v1 Z5 q
    22.                 mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
      1 z6 S, E; K4 V* q2 f
    23.         }  @, \; A7 n8 N3 q7 R
    24.         public void updateUser(String name,String key,String value) {
      2 ]! k6 G6 k9 _0 M7 d' f; r
    25.                 mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
      8 E1 h( E' c0 e0 T" }# c7 r# e
    26.                   N7 m! q; V! D6 b' l
    27.         }* e, s* Z$ A" h4 g  y2 I- C  |3 c: h0 `
    28.         public List<Users> listUser() {# k1 c. _+ U( @9 }2 G8 ?' k
    29.                 return mongoTemplate.findAll(Users.class);* C+ n+ |( P5 t. C
    30.         }* B! j$ z* V2 R' h
    31. }8 P; S  Y, _) ^  [5 d& I: X
    复制代码
    . }  k; Z# {; N0 \* y+ f! e4 @2 v3 D
    % ]" m& g& X4 y/ x' P
    演示地址:http://121.42.26.72:8080/SpringMongod# u, J) X# r: K) q# ^2 e
    2 G' B+ Y+ M: a  `$ D
    项目下载地址:Spring4 整合MongoDB详细讲解项目源码9 Y3 m2 }3 m0 J  z& L6 X  n$ e

    7 E+ |7 |5 _9 y5 i+ U解压码:www.52itstyle.com
    / t3 {+ `0 O: ]9 J7 i3 D2 s

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


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

    admin    

    1244

    主题

    544

    听众

    1万

    金钱

    管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    沙发
    发表于 2015-03-10 20:46:22 |只看该作者
    解压码:本站网址               
    回复

    使用道具 举报

    7

    主题

    3

    听众

    383

    金钱

    版主

    该用户从未签到

    板凳
    发表于 2015-03-10 21:09:32 |只看该作者

    点评

    admin  你妹的 小斑竹  发表于 2015-3-10 21:11 回复
    回复

    使用道具 举报

    2

    主题

    0

    听众

    128

    金钱

    三袋弟子

    该用户从未签到

    地板
    发表于 2015-05-04 00:46:42 |只看该作者
    谢谢分享
    7 |) B. Z7 S; k2 T$ O  V5 s& T' ~
    回复

    使用道具 举报

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

       

    关闭

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

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