科帮网

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

动态微博

查看: 3934|回复: 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详细讲解项目源码# v# g8 f9 \! |. Q7 C
    Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
    * K/ S, ?- Z( Q& i
    5 @) M' w3 ?3 Q9 B: D. S* oNoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。& c) {4 v& y6 t) ]9 R
    5 Z- x+ a" B3 c8 C1 R
    Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
    " ^) @8 x- N; U
    : X. ~0 b% X' p( y/ y! S/ }环境配置: myeclipse 8.6  、mongodb 3.0  、spring 4.0.6
    , [5 r4 a* O* N" C; [0 P& Q% g, Y& T, @( u) Q! Q; h
    0 H& `$ C- Y/ x/ d  {
    项目采用springmvc 结构。
    ( X# U# ~, {7 F! q" s2 I* s5 P' H所需JAR包:* ?) D. G; J2 y
    : M2 b, y# l$ W$ S$ |# _
    : E4 f' t% B6 v* M9 h  b

    $ h* a: c9 S; D5 s1 VapplicationContext.xml配置:
    3 B! U, O6 [& U2 |. g$ s) x
    1. <?xml version="1.0" encoding="UTF-8"?>  9 g* y! H6 \8 D% \8 c8 Z! s
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      $ `1 K3 h7 @4 H- S  j
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      $ G4 O! w6 y( z
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      8 [; U0 T  v( P) H& p
    5.     xmlns:context="http://www.springframework.org/schema/context"  
      , ], F" F) N( |7 P" P$ Z
    6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  ) [; [1 u( r/ P9 r. S" m) f! H" q
    7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
      9 r  Q+ R, g/ d8 U& q9 p
    8.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
      9 e9 X6 [7 B% }; e- S+ \7 V
    9.         http://www.springframework.org/schema/beans
      3 y: S( K* y8 B& q6 K8 v
    10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  5 J: K: x# r7 N2 H# s6 j
    11.         http://www.springframework.org/schema/context % j! `. t- e6 ^" ]- u
    12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      0 [, g% X2 m6 v, L, ]/ d3 Z
    13.      <context:component-scan base-package="com.mongodb.service" />
      3 w4 D# w& S2 v( o. F+ M
    14.      <import resource="mongo-config.xml"/>3 K8 t) T2 e: N! F8 V  x; N! h+ ?
    15. </beans>
    复制代码
    mongo-config.xml配置:4 l+ b  R( z! f0 K5 @! Z' C  [- ?% Z
    1. <?xml version="1.0" encoding="UTF-8"?>  9 ]* R5 p2 }1 c! O3 ^5 p3 ?
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      ) A- Z# X4 _* H/ L: n
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      4 J) @8 A0 N0 k6 b! B
    4.     xmlns:p="http://www.springframework.org/schema/p"   # z; ^& ~" \+ C' d' L
    5.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
      " k. _9 X, P. u$ e: A8 A& I
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
        _3 I: y! M$ Z: J# |' X
    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  ' c* R) `# N9 N( d
    8.             http://www.springframework.org/schema/data/mongo  
      4 A4 b, l3 @, m$ S$ C6 u
    9.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd3 p* d+ ]$ \  t/ t
    10.             http://www.springframework.org/schema/data/repository+ h' p1 ^5 P! p/ ?" f+ x- Q
    11.             http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">        
      + q4 B, d* ]5 N* q9 h
    12.    
      & L5 s, }3 l& Y) S+ C7 f2 W" E
    13.     <mongo:mongo host="121.42.26.72" port="12345"/>  
      ' G8 t: g" H9 C& x$ B8 K
    14.       
      # {. O% x" F* W; J/ \  |
    15.     <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->  
      # r7 E% H4 ?/ Y: T* W; N: S* q# E
    16.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  $ ^7 v. }) b/ g  Z9 n' o: m
    17.             <constructor-arg ref="mongo"/>  
      + v1 _  \: Q. t% y2 ?- g
    18.             <constructor-arg name="databaseName" value="itstyle"/>  
      ' z5 R3 z2 A' _. s9 e1 t
    19.     </bean>  
      " C7 v  g4 R; `8 ^. S8 u) _
    20.       * Y2 z* Q2 Y" S
    21. </beans>
    复制代码

    0 |* p) {! Q6 kspring-servlet.xml配置:
    - {4 [6 O9 y+ X7 }% E# [9 }
      s$ c. O# M# q# D: \! E5 X# e
    1. <?xml version="1.0" encoding="UTF-8"?>  
      + f; [1 R$ x6 x, M" L1 G  N
    2. <beans xmlns="http://www.springframework.org/schema/beans"  6 l7 H- z6 b- h( C+ B6 y+ R% q
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      2 b  R$ l* x. ?
    4.     xmlns:p="http://www.springframework.org/schema/p"  ! P3 l' @: x* q! Q
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc"
      : ^8 }/ z% P* I
    6.     xmlns:context="http://www.springframework.org/schema/context"  3 |: g7 R4 R9 H
    7.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
      8 x7 a3 i  P4 _2 d" b# m9 I2 v2 O
    8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   % }: h$ [! j. @: q
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      * |+ p$ H/ x7 e+ Q9 t7 e0 \
    10.             http://www.springframework.org/schema/context ; F% s7 F2 E8 s3 b
    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd   B. ]" ^1 B- i- g0 ?% |% P1 M
    12.             http://www.springframework.org/schema/data/mongo  6 f2 ]. v- Y% b( r5 c4 o
    13.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
      0 Y0 q! Y2 i( b6 Z1 D; @
    14.             http://www.springframework.org/schema/mvc        9 w# Y4 a1 Z1 I* c& ]
    15.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">, @  n' Z- e. c* W! C. p9 q3 I! G
    16.         * m9 z. ^- 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-->  " X. k5 N3 D& X% U- ^+ v& @5 J
    18.     <context:component-scan base-package="com.mongodb.controller" />   - E9 s* G: y( Q# _# B5 p' z
    19.       7 g" Z2 y$ z& z$ y2 X# M' x
    20.     <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->  + p! D. J% ~% z4 ]0 r+ h( W
    21.     <mvc:annotation-driven />   
      . T1 N* k+ |9 f" `# ~
    22.    
      ) o6 J  u% e4 p* [
    23.     <!-- Declare a view resolver -->  
      5 }# V6 e; p# j" N; I
    24.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"   5 ^: o$ A" o# U# p6 A# m
    25.             p:prefix="/users/" p:suffix=".jsp" />  5 B0 {8 F) F- A$ J  F* w# e
    26.             ) r) v- q( w6 W# G2 q0 D  q
    27.    / Y) s: }; b' e, l0 @
    28. </beans>
    复制代码
    UserServiceImpl 基本实现代码:
    1. package com.mongodb.service;  |% y; o+ q& y) K4 L
    2. import java.util.List;
      ; }& Z- J) y1 S3 N. e+ c
    3. import org.springframework.beans.factory.annotation.Autowired;
      2 H. H$ B# b! I7 ~! _/ ?
    4. import org.springframework.data.mongodb.core.MongoOperations;
      ' }! i! j+ \$ j* `- c" X( }6 @
    5. import org.springframework.data.mongodb.core.query.Criteria;* C1 e7 N. ?/ I4 K
    6. import org.springframework.data.mongodb.core.query.Query;8 K7 f3 ?! b5 M+ F( y0 }2 f7 [
    7. import org.springframework.data.mongodb.core.query.Update;9 g, {1 U# R( W8 D
    8. import org.springframework.stereotype.Service;
      2 \, J# Z. W- }- w- H; {
    9. import com.mongodb.domain.Users;
      ; ~6 u6 R' V+ i3 [/ q$ P
    10. @Service(value = "userService")   
      6 H/ g9 Q6 z1 q3 `& O
    11. public class UserServiceImpl implements UserService{ 8 M" l% ]% a( f5 E; ?5 f
    12.     private static String USER_COLLECTION = "users"; % x0 K4 M" I3 B. U# G% Z' ^' Y
    13.     @Autowired  
      0 E6 x; P* v* e
    14.     MongoOperations mongoTemplate;
      - z$ n" k8 B9 h" P" d, @: J
    15.        public void saveUser(Users users){            
      8 p/ Y: d) q, R" s- q  x: w
    16.         mongoTemplate.save(users, USER_COLLECTION);            
      # S% h9 Y4 \( Y4 |2 H$ @, a
    17.     }        $ l3 D/ D, `: x: C7 J
    18.     public Users findUserByName(String name){; ]( F$ Y! N/ @! m% p0 V( q
    19.         return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);  
      , D: R  S/ ?  x' [, _) W2 @
    20.     }+ I( n: Q# J% h8 P1 @
    21.         public void removeUser(String name) {
      , ]9 r3 L# f( ~) k
    22.                 mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
      ) o) D$ j6 k; q" I! h" N
    23.         }
      ; w4 L6 S: h$ E* r) M( b
    24.         public void updateUser(String name,String key,String value) {! E% q2 m) Y* g+ [2 I
    25.                 mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);9 P4 y6 \* ?. A3 L
    26.                
      / S- I, u1 [* ]+ A  w
    27.         }
      + V4 X/ D+ U6 A! Y; p9 f* U
    28.         public List<Users> listUser() {
      9 f1 F: W& ]  Y2 E
    29.                 return mongoTemplate.findAll(Users.class);
      3 f$ W* i7 S1 i% V* ^& S
    30.         }1 P; _0 F3 V" W" [, p. K; x
    31. }
      1 i! q  W& l% a  ]
    复制代码

    4 V& M- g1 y. M! ^
    ' X2 Z! ~, O% f, B' q, V9 Y' ~- A2 B演示地址:http://121.42.26.72:8080/SpringMongod. j9 u( Z5 F; Z

    ; s; I8 k- K7 z3 x# k0 p项目下载地址:Spring4 整合MongoDB详细讲解项目源码
    & [) h8 D0 J. C; H; {8 m) Y7 `
    8 _5 Y# L! O! G6 h, s0 T: }解压码:www.52itstyle.com1 R# s2 T. h+ l- k6 u1 y/ A

    科帮网 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 |只看该作者
    谢谢分享
    : ]# G- @4 k' h5 S' o
    回复

    使用道具 举报

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

       

    关闭

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

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