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

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

动态微博

查看: 3651|回复: 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详细讲解项目源码
      ], C* b6 u" C) F& y7 TMongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
    9 K0 q, l3 `5 Q4 y4 B
    ) o: a0 g1 e7 I; W9 mNoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。
    # l9 o" n: u- V0 I6 N& K8 a( F, t! R0 c! X. u/ i; R5 ^
    Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
    ; p) q( R/ o% k% Y3 B$ }4 g: ~; O% a2 L( ]9 w, |8 {: C3 K3 W6 C
    环境配置: myeclipse 8.6  、mongodb 3.0  、spring 4.0.6) D' T- J3 Y. z* r3 l
    0 B% b" ], e1 Z" u

    ( h2 e# F' T, s5 A项目采用springmvc 结构。
    , r; w$ y- H) D- F所需JAR包:& {. [0 a- y' g! p
    : s8 c: _2 Y# f& i8 l

    5 O6 q! A& @, z- g' f$ d
    , S  y1 K- y: @1 uapplicationContext.xml配置:* J- z5 h4 I1 Z% _
    1. <?xml version="1.0" encoding="UTF-8"?>  
      0 i8 A3 N8 \' |. I, E5 {2 m4 [
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
      * m- v7 a& Y5 ^5 W
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  + J$ L9 s5 N- l/ [& F2 W  I" B; D
    4.     xmlns:p="http://www.springframework.org/schema/p"    a- E3 `/ i- e, j5 \6 D
    5.     xmlns:context="http://www.springframework.org/schema/context"  
      : `5 P1 a4 x( a1 \
    6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
      2 V+ C5 u4 E! w% X0 q
    7.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   5 R5 M7 V/ u# k  \
    8.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  * t. |1 z% p. \; u; z, \
    9.         http://www.springframework.org/schema/beans ) \' \0 W/ \/ s
    10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  * \3 x( u9 G  ?+ G
    11.         http://www.springframework.org/schema/context + @# \8 s+ e3 e) d' P
    12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> / y( b# S( s& w' w
    13.      <context:component-scan base-package="com.mongodb.service" />
      5 x1 k  n3 w8 W' E
    14.      <import resource="mongo-config.xml"/>
      / m+ c  z# ]3 ~5 `; y- |
    15. </beans>
    复制代码
    mongo-config.xml配置:. E) d& a9 f& H0 q
    1. <?xml version="1.0" encoding="UTF-8"?>  7 \9 X" }" a  W4 [' D! K
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
        f4 P7 t* ]6 x; @
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   # c; x& o9 j% k* I* y0 T
    4.     xmlns:p="http://www.springframework.org/schema/p"   
      + E* c1 P, K, W$ I; [  t
    5.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  & q2 R# v: s0 c, P7 x# M% B
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      1 ^  N" j4 Z# U; L
    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      * ?' b- B: p: ^$ J
    8.             http://www.springframework.org/schema/data/mongo  9 ~/ c% x6 V' O: c, w6 X$ x3 `' C
    9.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
      4 u9 [, l4 B' P* }# v
    10.             http://www.springframework.org/schema/data/repository3 y5 H, j% n: o
    11.             http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">        ; T) y0 ~- U9 S7 P
    12.    
      6 g/ Z1 {) g3 x6 O
    13.     <mongo:mongo host="121.42.26.72" port="12345"/>  8 I% X1 |4 I$ D) }7 ^
    14.       6 u. W) c2 v$ O% P
    15.     <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->  
      7 E5 R* R# @% A* ]/ L
    16.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  8 E( n% P+ c: I' e+ ^
    17.             <constructor-arg ref="mongo"/>  . _7 ?  o5 w! P1 a2 j8 H5 |
    18.             <constructor-arg name="databaseName" value="itstyle"/>  4 A5 U2 b4 `& X" w
    19.     </bean>  $ W9 i9 b3 {, X2 ?! z2 v! R
    20.       
      ! u1 |: H# t' T* C% Y
    21. </beans>
    复制代码

    7 e2 K" z. V- E9 T5 D+ t: cspring-servlet.xml配置:
    $ m6 M6 L9 t- ^! T8 b# Z3 o
    ) i+ \6 e) E# `
    1. <?xml version="1.0" encoding="UTF-8"?>  
      & i+ o7 a' Q: v  i
    2. <beans xmlns="http://www.springframework.org/schema/beans"    k; Q4 @- I8 h5 n+ l
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   $ v* e9 P5 W/ k) _" y0 f- b
    4.     xmlns:p="http://www.springframework.org/schema/p"  + p  \+ D8 B: i4 e" }, a
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc" 2 s" R4 e4 F' M: e! E: U" A
    6.     xmlns:context="http://www.springframework.org/schema/context"  ( V8 h' [4 U/ X+ k0 `7 p
    7.     xmlns:mongo="http://www.springframework.org/schema/data/mongo"  3 F! N$ @3 e: O/ H/ c
    8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   * V- [; B) N/ A5 R6 j
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      5 ]4 F' o* c! n  B
    10.             http://www.springframework.org/schema/context 5 w: V* {. G' {$ J4 D
    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd . T9 r) f# a- E2 W4 @8 H/ b" R3 v; s
    12.             http://www.springframework.org/schema/data/mongo  1 Q0 {7 c5 H% B" s% |
    13.             http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
      + b5 P3 h1 w" a! M: v2 A
    14.             http://www.springframework.org/schema/mvc        
      7 R6 k$ W2 N' C* E8 M4 o( W
    15.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
      3 a# `; W* ~2 x5 a
    16.         ' r$ e3 {' U( ]8 I* k  ~! j
    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-->  + n7 B" g1 C" i$ M2 F- [. S7 M: Z
    18.     <context:component-scan base-package="com.mongodb.controller" />   
      ( y" l$ v& n/ R! h7 m5 l; r6 n
    19.       ( n$ B- {3 i# W! P4 ^5 ^/ Y
    20.     <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->  6 c$ ?; a# w& C, m) A/ n
    21.     <mvc:annotation-driven />   8 U7 H; C! q6 x4 X+ s2 w8 Z' {
    22.     , ^2 t+ _) o' p3 `, ?# ?
    23.     <!-- Declare a view resolver -->  
      * ?4 Z% t% P: {: E8 K4 f6 Q
    24.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"   3 \% V! F7 \, i/ C9 g7 v
    25.             p:prefix="/users/" p:suffix=".jsp" />  8 h  m( f. n% P* R2 y8 ?
    26.             
      1 |2 ^& T: t2 ~& t! L& t) f1 y0 u$ P
    27.    % {: {* o) q( T3 r1 u) ^" p6 ?
    28. </beans>
    复制代码
    UserServiceImpl 基本实现代码:
    1. package com.mongodb.service;
      - h) b, Z% n, t$ `
    2. import java.util.List;
      6 c8 P: y% F( F5 O$ _+ T) d" r- x
    3. import org.springframework.beans.factory.annotation.Autowired;) ]+ t0 B  ]0 M. o. `6 _5 y7 n; D
    4. import org.springframework.data.mongodb.core.MongoOperations;
      ! W8 v8 ~6 T1 Y* D( V
    5. import org.springframework.data.mongodb.core.query.Criteria;
      $ N9 j& L1 x9 [! L; V% x/ }% A
    6. import org.springframework.data.mongodb.core.query.Query;
      6 k/ x' m! h0 s' v
    7. import org.springframework.data.mongodb.core.query.Update;" T3 z0 Z- t0 a' [6 n
    8. import org.springframework.stereotype.Service;
      4 c  n  c; p  M
    9. import com.mongodb.domain.Users;
      , d! K# g" t: u0 t9 v
    10. @Service(value = "userService")   
      7 B/ h9 p% E5 o$ q* E% e
    11. public class UserServiceImpl implements UserService{ 8 a( ^, o) }1 K( X
    12.     private static String USER_COLLECTION = "users";
      ! f% h3 f- y6 S3 L! n) t4 R
    13.     @Autowired  & [& D$ m4 ~* q0 j0 w0 p5 i
    14.     MongoOperations mongoTemplate;
      ( Y% p5 T, \* `& v9 D
    15.        public void saveUser(Users users){            
      + ?, K) [# z( w
    16.         mongoTemplate.save(users, USER_COLLECTION);            
      7 N. d! c8 p: r8 X
    17.     }        
      4 @+ W8 @! \3 r* n. S4 b
    18.     public Users findUserByName(String name){
      ) L4 K3 _/ H. C4 r# H: }
    19.         return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);  
      * g) l: I  J8 K. i& x2 }9 X9 b
    20.     }
      " r! h, X6 v* M- q* {
    21.         public void removeUser(String name) {% `& {# O* F) l5 D1 F# [+ a/ K
    22.                 mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
      ( o) x$ X3 d0 a0 Q1 E7 k+ ?
    23.         }
      . f  ?( r0 B- y7 {1 H, P
    24.         public void updateUser(String name,String key,String value) {
      . F3 d& T9 u. D# {+ H
    25.                 mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);" j. Z: V5 N8 O  Z3 B$ p% O
    26.                
      * N( n- w6 h9 I- W1 p1 _+ }
    27.         }
      " a8 y' J) t3 J+ Y, E: o
    28.         public List<Users> listUser() {1 K% X0 p3 f' o( v7 d% U- x
    29.                 return mongoTemplate.findAll(Users.class);: J3 G7 P% \8 W7 ?( v- ^; R
    30.         }  r( ~9 Z9 S& T6 B
    31. }
      * V5 z. u& c9 q6 Q
    复制代码

    . N  ~/ o3 j. i7 I' k! e7 U+ g# }- ~6 ]7 _% v3 D8 x
    演示地址:http://121.42.26.72:8080/SpringMongod
    6 N  t7 V; a- q! G# ^) W0 T' `9 B) V7 U
    项目下载地址:Spring4 整合MongoDB详细讲解项目源码
    ( h% W! z# H2 b( F8 U! W
    , I. W6 `2 I7 a1 m8 t$ r3 z! g解压码:www.52itstyle.com+ s% e( K3 M* x/ n4 _/ r) l6 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 |只看该作者
    谢谢分享
    % o9 o3 q8 p" z1 w: h& F9 l1 y
    回复

    使用道具 举报

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

       

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