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

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

动态微博

查看: 3647|回复: 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详细讲解项目源码. B+ s" z/ S7 l
    Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
    ' s1 A, J. W, j9 Y
    " r! j( s  c. L( z( F9 |5 i, ANoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。; {$ ?' L7 c3 \2 R  x' @
    1 q& \5 h- ]4 ?
    Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
    ( S, i% d! R: Z, z' X! s+ B; O2 {' M' o/ A; x4 Z0 |7 `' B
    环境配置: myeclipse 8.6  、mongodb 3.0  、spring 4.0.6  S$ u3 w+ n( ]3 _% Y
    3 G0 W; m9 }7 z! z
    3 h3 M: q, ~" N( r. L  f
    项目采用springmvc 结构。+ p5 n) g/ @8 i+ p* t" M! a
    所需JAR包:2 q& K( O5 V) x$ w) ^% s
    0 E; `- g9 l4 N  k
    ( o) L( G: a/ p& Z" x

    $ d; P6 |' e; t- E* T8 W' aapplicationContext.xml配置:
    3 J+ Y' G, X. q0 F7 k
    1. <?xml version="1.0" encoding="UTF-8"?>  ' ^7 ?0 w( ]0 f  E3 d
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      9 e8 `: B* M! L' Q) h+ }
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      $ k  f5 W6 Y9 Q) i& u+ X. |) N8 _
    4.     xmlns:p="http://www.springframework.org/schema/p"  ( `1 `. S5 x. ~6 b$ o: Y( C
    5.     xmlns:context="http://www.springframework.org/schema/context"  2 h2 s' n* G- i& O/ \; @7 D
    6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
      # ]% e7 c) T3 J' l/ B$ N3 S
    7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
      ; T. I8 n$ `% \* T
    8.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  : H! l  ~2 h& I
    9.         http://www.springframework.org/schema/beans 7 a9 S* V! o3 D# H& c% n
    10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        T. v; c1 F& Z
    11.         http://www.springframework.org/schema/context
      8 ^4 R7 d1 ~% T! B! b6 z5 q4 `2 t/ O
    12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      ) U/ `/ P% v6 R$ z/ s7 ]* q3 @/ O( k3 j! Z
    13.      <context:component-scan base-package="com.mongodb.service" />. U- l6 ~5 `$ E& W7 y
    14.      <import resource="mongo-config.xml"/>
      7 r% B; |5 Z) d7 d* c+ w. r
    15. </beans>
    复制代码
    mongo-config.xml配置:' L# C( t) h" V+ d) U9 Z3 [# u
    1. <?xml version="1.0" encoding="UTF-8"?>  
      9 x2 O4 l& [; Z( {1 [! F
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      + F5 D6 W$ [3 R& f; P: m$ R& }
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   " k+ V0 k) v5 d/ N# [
    4.     xmlns:p="http://www.springframework.org/schema/p"   : b+ \6 \$ N/ T7 u5 T& |
    5.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
      & R! J* B9 E- ]
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      4 B; J$ [- B4 v+ S1 w5 H
    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  $ T) B0 l8 g* T% g
    8.             http://www.springframework.org/schema/data/mongo  4 b. C, n% q" D# L9 _
    9.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd- l% z# L& p, P6 U. |
    10.             http://www.springframework.org/schema/data/repository
      % A2 g( `, S5 F7 j* D
    11.             http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">        0 i4 |+ m6 H+ {0 h
    12.    
      ' n* F2 h" s9 E* c" W7 Y1 @" V0 \) H
    13.     <mongo:mongo host="121.42.26.72" port="12345"/>  
      ) U  w9 |. O& T% m; V( B5 Z5 c6 k
    14.       ' o& o3 v1 p. s
    15.     <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->  
      1 i4 }2 q, z" @5 w7 ]
    16.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  - b  T4 d* \" B- p% o9 o
    17.             <constructor-arg ref="mongo"/>  
      ! A( E" I, o+ {: `  B  s( Q
    18.             <constructor-arg name="databaseName" value="itstyle"/>  + m& G) u( Y9 E! ^0 O  M0 [$ Y5 v
    19.     </bean>  
      * d/ u; f) U( |9 S- G" f- J
    20.       
      , b: F4 Y) y2 O" w6 K3 \
    21. </beans>
    复制代码

    ' ^5 V6 W; Y' [1 {spring-servlet.xml配置:
    % G/ h0 L! X( ~! b5 B
    ! d6 ~5 F' r. t5 i3 h3 L& q% ?& _
    1. <?xml version="1.0" encoding="UTF-8"?>  
      4 C0 `) p. l* Z6 R3 z% y: l& B
    2. <beans xmlns="http://www.springframework.org/schema/beans"  1 K, q9 K( ]9 S# Y3 U* x
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   6 x, }/ u9 r$ ~- \: ?1 \
    4.     xmlns:p="http://www.springframework.org/schema/p"  ! Q4 a# U) g" R& C
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc" # A( c9 C% O  Y) q
    6.     xmlns:context="http://www.springframework.org/schema/context"  % \5 E" l/ r' e" J' F/ ^( `
    7.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
      + A1 C! b9 Z* V2 m6 N" f8 z
    8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      ) H! \. G& y+ e
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 w# c) x& k9 L. N! r& B- B
    10.             http://www.springframework.org/schema/context % ~- Z$ @8 K4 c8 \5 z1 E. r4 ^
    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd
      0 c' U  U( ~  L0 T+ I1 v* w
    12.             http://www.springframework.org/schema/data/mongo  
      + A8 b! H( O+ U- Y" x1 O2 M
    13.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd. n! V( H1 |1 ]) A6 o6 F6 L5 K
    14.             http://www.springframework.org/schema/mvc        
      , m0 V3 g% c& c- s* A* M3 C/ R
    15.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">2 x" e7 Q/ q* m
    16.         0 L/ f) ^9 @4 q2 [$ \
    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-->  
      0 t! g+ b/ ]& V& P% i& T; C
    18.     <context:component-scan base-package="com.mongodb.controller" />   & O) T$ M# J; f! _/ C: M/ c- r
    19.       
      # R( a* R" [7 ?* q5 u* R. x
    20.     <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->  
      8 [5 ^6 l. d% B& r  C2 V; s
    21.     <mvc:annotation-driven />   : p0 p+ ^/ c$ ^7 \+ G  P7 V( L
    22.    
      $ d; i4 J2 ], [5 t
    23.     <!-- Declare a view resolver -->  
      / C+ B! g, M- \, r
    24.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"   " p  U: U6 M: W1 K6 |; j( L+ S
    25.             p:prefix="/users/" p:suffix=".jsp" />  
      3 t3 B, u* |3 @  B8 B# k% E$ y7 \
    26.             
      / A. p1 A' u# k
    27.    6 m7 n" C0 M3 F- G  f9 x
    28. </beans>
    复制代码
    UserServiceImpl 基本实现代码:
    1. package com.mongodb.service;& l7 r% c$ f" I9 H" F* C6 g# `8 O
    2. import java.util.List;
      9 Y' Y3 i9 U7 T0 e
    3. import org.springframework.beans.factory.annotation.Autowired;
      5 k& M9 _/ ~: f; g
    4. import org.springframework.data.mongodb.core.MongoOperations;
      ! N: c3 T4 Q  F8 C( p
    5. import org.springframework.data.mongodb.core.query.Criteria;
      + |4 N, a7 Z6 u5 f; o& @" {
    6. import org.springframework.data.mongodb.core.query.Query;
      " f1 h6 Z9 g. P0 U  r* ?
    7. import org.springframework.data.mongodb.core.query.Update;! ?( I2 v5 o' f1 n& i) C7 a
    8. import org.springframework.stereotype.Service;
      9 `. v$ x! B  Z
    9. import com.mongodb.domain.Users;
      , F/ g$ m1 o' F7 d+ S
    10. @Service(value = "userService")   
      0 l3 d1 L* l' `, y% B
    11. public class UserServiceImpl implements UserService{
        z7 w3 X# |' Z) j% U
    12.     private static String USER_COLLECTION = "users";
      6 m* F- \, K3 U, X% S4 X; j5 m2 S
    13.     @Autowired  + F+ D+ R' E+ b) w1 w
    14.     MongoOperations mongoTemplate;
      - O1 b3 }2 m' E4 `4 g, G1 N0 z
    15.        public void saveUser(Users users){            
      $ ^; x2 B! A  ^: X
    16.         mongoTemplate.save(users, USER_COLLECTION);            
      4 \4 ]( R. e1 t
    17.     }        3 ]& w! a* E5 \# Z" g0 x
    18.     public Users findUserByName(String name){
      ; c% D4 i7 R  o6 j0 |% _
    19.         return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);  
      $ y3 t$ y' Z( g2 L+ A. ?. @
    20.     }" F8 u' b. V2 D* T2 w: }) ~
    21.         public void removeUser(String name) {
      " \4 M# p0 z  `
    22.                 mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
        B4 @2 ?( ~' D, H4 x
    23.         }
      9 e) E% W% U/ m) K. m- E
    24.         public void updateUser(String name,String key,String value) {
      ( ^) W- f8 j- z: b/ L
    25.                 mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
      . A) Z; U! n4 \  S2 A8 T: ~
    26.                 / ~6 }6 t; J, {( k$ {1 G3 O
    27.         }, j3 T% ]* P& f. j0 |: c
    28.         public List<Users> listUser() {
      + |% ~! z# [6 X* M! m" F
    29.                 return mongoTemplate.findAll(Users.class);
      , `' h' c5 c( p7 _$ W$ L4 h
    30.         }
      9 ~; ^/ x5 Y$ |2 \. Z& ?4 n: w) j
    31. }% V& Q) F( b' F' s; {; X/ s
    复制代码

    - [- i* x2 i# _0 d
    8 q3 j' N1 S  q; ?% c" e演示地址:http://121.42.26.72:8080/SpringMongod
    1 F7 s4 j6 c" p# H
    5 `' ^4 b$ U" S: B" u9 W项目下载地址:Spring4 整合MongoDB详细讲解项目源码
    9 O+ i: L8 X- y" ], @5 ]% f1 {( i
    2 {4 l2 f  [& W7 a& U" d- M9 P解压码:www.52itstyle.com8 I. q  N; ~' G, w4 R

    科帮网-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爱好者③

    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 |只看该作者
    谢谢分享0 o/ W8 U/ V) H: ^2 v
    回复

    使用道具 举报

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

       

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