科帮网

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

动态微博

查看: 3932|回复: 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详细讲解项目源码7 b  F- u/ W/ e: s% O/ I
    Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。  s2 `5 `4 ?3 L1 e) S9 v: T1 r' s+ s

    9 [  t. S. Y. p) V* ?NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。6 o5 ~; s! X+ i% S: g% {

    + X$ h$ S& I7 y+ r0 dMongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
    0 G# t: R3 ^9 J* p0 W( @& |7 \3 y: _( `
    环境配置: myeclipse 8.6  、mongodb 3.0  、spring 4.0.6! X. k0 G. U2 b( L

    7 x# V' g6 A+ W; x# N
    % b' ?! ~% \6 x9 k) m9 |+ w8 `: y) ]项目采用springmvc 结构。: z1 ^2 G: ]$ @' d2 ^( h! B
    所需JAR包:
    " I2 _/ x* E6 S$ Z& L 6 I9 p3 h) o$ `: u

    " V& h# U, Z$ Q0 K& ?" {; L7 i0 W- d% `
    applicationContext.xml配置:
    % \$ c- N* D6 z7 Z4 p5 L
    1. <?xml version="1.0" encoding="UTF-8"?>  
      + A, J$ S& Q3 j* P. e
    2. <beans xmlns="http://www.springframework.org/schema/beans"  . n0 m" i! p+ ?6 V1 k7 s$ M6 [
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      9 K( p. F: I* n$ \
    4.     xmlns:p="http://www.springframework.org/schema/p"  . j% R6 y% S, L3 n' C
    5.     xmlns:context="http://www.springframework.org/schema/context"  2 S, \+ X; l/ S( ]" W
    6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  5 ?$ g# d! ^) r9 b+ e
    7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
        d$ ^/ ^3 b+ G8 a1 P
    8.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  + |' e: p# @) ?" X
    9.         http://www.springframework.org/schema/beans 3 G3 A3 F$ g  c+ w1 U
    10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  * ?4 U0 |# }* x  K, ?! e7 M' V
    11.         http://www.springframework.org/schema/context
      # ?6 `. s  ^/ P9 @0 x
    12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      2 [* D+ Q' N9 V% R6 I4 M) _
    13.      <context:component-scan base-package="com.mongodb.service" />$ {  `# _) |% O0 s
    14.      <import resource="mongo-config.xml"/>' K0 E( n, b0 E
    15. </beans>
    复制代码
    mongo-config.xml配置:
    ) O% c9 r0 D; e7 ]2 F5 [
    1. <?xml version="1.0" encoding="UTF-8"?>  9 ?; M/ K+ g- t
    2. <beans xmlns="http://www.springframework.org/schema/beans"  ; o* k4 a, j! R2 S$ Y8 q
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      / k2 s! J$ _/ r% X+ I
    4.     xmlns:p="http://www.springframework.org/schema/p"   
      9 H. p: s0 Q) g& r4 O+ `
    5.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  5 P7 O3 Y5 ?; g% L( G% `3 Z
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   " k: t: c7 r( U9 A' o
    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  % K" O" Y2 \, X/ ^
    8.             http://www.springframework.org/schema/data/mongo  
      * S) D, }5 F8 B
    9.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd1 o1 s" n( ]$ b, v4 g+ V
    10.             http://www.springframework.org/schema/data/repository- o0 x  N& v! Q( ^/ p5 Z! p/ G
    11.             http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">        
      ) O5 k5 b) j  w" x) J& M3 Z0 H
    12.    
      & }4 V0 I: U. t- @" j' ~
    13.     <mongo:mongo host="121.42.26.72" port="12345"/>  
      : V+ s+ G4 |" B& t8 a+ f
    14.       
      # R7 P- ]0 L" m2 c2 W, d( L
    15.     <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->  9 E& O6 n2 v4 \( ?1 ?2 a
    16.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  7 ^  K( ~: Z2 \8 O8 h
    17.             <constructor-arg ref="mongo"/>  2 W5 j5 S- Q# o- ^3 a" j. X
    18.             <constructor-arg name="databaseName" value="itstyle"/>  
      ( Y. U9 f% M1 j1 F1 F+ e. W4 }
    19.     </bean>  ) |8 }* q/ r' y" ^& L- L6 P0 l, }
    20.       - b/ l& F4 H$ b) j
    21. </beans>
    复制代码

    3 g' ?3 W6 J9 G( ^+ @8 ]spring-servlet.xml配置:
    3 A7 \2 \- }3 T! ]  ^! [, C) z8 g! ^/ E5 y# u8 `& l
    1. <?xml version="1.0" encoding="UTF-8"?>  - [/ I/ l- ?/ k& I8 U. Q8 |7 w
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      ( ?9 H0 L6 G; x1 F' U8 J
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   - b$ v. `9 T" n! B+ O
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      % e' I- x4 ~; K; Q
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc" # o1 c# w6 _- k  P+ w  w0 ~+ q
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      ' l5 f2 ^8 H8 r& L- |
    7.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
      - h% ^1 a; q  ?5 O/ W
    8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      % v' w2 B7 W* G0 u: @0 V
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 2 [$ C9 X7 x4 L% `+ Q$ @
    10.             http://www.springframework.org/schema/context
      # l# p' I0 w4 B
    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd 0 K, v$ d5 Q" U/ v. ~
    12.             http://www.springframework.org/schema/data/mongo  
      4 W; Y' q. x- c3 y' q
    13.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd7 f( k/ l  V$ k/ C
    14.             http://www.springframework.org/schema/mvc        ) v9 M! g9 C) d
    15.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">/ F0 W9 o' `# v: \0 Q3 t$ t- i
    16.         - ]8 z1 D; U" \  H
    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-->  
        t4 W- r7 T  ^2 Z- ?" B) t5 k1 D2 d
    18.     <context:component-scan base-package="com.mongodb.controller" />   
      7 ?+ a! d  c) ?7 W  e1 n8 A& v
    19.       
      * r5 n2 m/ {9 z0 \7 ^" f' H7 A
    20.     <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->  
      + p3 k; d# U$ s$ F/ w& z% z
    21.     <mvc:annotation-driven />   
      2 s, n, h- t9 W+ q* q
    22.    
      6 }. i* n3 V3 o& c* @: v) \- k
    23.     <!-- Declare a view resolver -->  
      4 D! Q# p; [4 r
    24.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"   
      % L' [7 o: [, B' ~! i# c$ E
    25.             p:prefix="/users/" p:suffix=".jsp" />  . t: Q+ a3 d9 P& Z# R- X! e
    26.             
      8 V$ t% `8 I' |: E' F
    27.    
      ' X! h+ v- T" g: L' d
    28. </beans>
    复制代码
    UserServiceImpl 基本实现代码:
    1. package com.mongodb.service;
      2 h! P- F" C* F' C" S$ W' E
    2. import java.util.List;0 X3 [& `9 r0 t
    3. import org.springframework.beans.factory.annotation.Autowired;, h# ^% H) E, C& P8 X& G
    4. import org.springframework.data.mongodb.core.MongoOperations;
      4 x/ z, e2 c6 w1 b# q1 \$ n1 o
    5. import org.springframework.data.mongodb.core.query.Criteria;
      & ^9 @! b! X$ d7 i0 e5 ]# K
    6. import org.springframework.data.mongodb.core.query.Query;! `5 b' Y3 o8 g' e, S
    7. import org.springframework.data.mongodb.core.query.Update;$ z8 R8 f7 c; _
    8. import org.springframework.stereotype.Service;
      ' F0 V! g+ O4 ]) S& Q
    9. import com.mongodb.domain.Users;
      & j7 X. L5 }/ v/ U
    10. @Service(value = "userService")   
      8 L: y4 V! o1 i! b7 t8 P$ J
    11. public class UserServiceImpl implements UserService{
      , j9 T& r+ V. a/ {, b* j3 ^$ C/ c6 R
    12.     private static String USER_COLLECTION = "users";
      ; z7 Y* N  j% s5 z! ?
    13.     @Autowired  2 R; u- P2 p1 _/ b5 C
    14.     MongoOperations mongoTemplate; ! o: V$ z, l$ `, w2 G5 N
    15.        public void saveUser(Users users){            
      * Z3 T2 M. l  B8 l' U2 `/ b
    16.         mongoTemplate.save(users, USER_COLLECTION);            / V; I& T  T/ L; r
    17.     }        0 k/ R7 C2 [0 ?
    18.     public Users findUserByName(String name){
      ( ^% P0 i' ?3 _  s
    19.         return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);  9 O3 G! g& W' k$ d; |) G
    20.     }
      3 {- @" ]5 {7 Q& X7 a: B
    21.         public void removeUser(String name) {0 I: j4 B) m; g
    22.                 mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);$ Y6 p' U" r- @, ?- l' t% |
    23.         }
      : _: H- q" G  t0 S. z
    24.         public void updateUser(String name,String key,String value) {
      * ]7 O2 H$ h6 u7 i; \2 A
    25.                 mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);0 E5 g, P5 \; a. J! E2 t
    26.                 ) N; I3 a  r4 ^2 e
    27.         }
      4 a% y+ n; G& L0 {$ s  X
    28.         public List<Users> listUser() {" v1 f! ^9 b& L; b
    29.                 return mongoTemplate.findAll(Users.class);
      & K! A/ y3 r& u" {9 q9 C
    30.         }
      ' a: X( j. \. P& J" X6 t" e
    31. }) `; u; h7 p7 n3 {- N& s; z
    复制代码
      x9 t3 ]* ]2 ]* c, p  ?

    / T8 W! k; k6 o, ~* R演示地址:http://121.42.26.72:8080/SpringMongod
    2 t: w* |/ j4 I4 E' B) N* c7 N, h: h* h% {
    项目下载地址:Spring4 整合MongoDB详细讲解项目源码
    4 Q# y! Z8 ]: G9 n  F
      p; s) l) Z: Y* d解压码:www.52itstyle.com
    # n2 R# e) t" z# t+ H; p7 m

    科帮网 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 |只看该作者
    谢谢分享
    % c6 I: r& t/ T; c- \
    回复

    使用道具 举报

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

       

    关闭

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

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