TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 - [* L1 a" v! ?( ~% K
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。5 c+ F6 C. L4 e6 {. Z
0 d9 C0 S+ @4 }: `1 K
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。& |3 C8 i4 H5 F+ G a) O
+ {( a0 Q9 |, J5 S; x: M. ~9 R
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
3 n1 W- s, G: Q ) I( b- l. P) t& J
环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
5 [/ g2 R7 s: Y4 G
, s1 o+ S, d" r; O; L ' j2 C8 {7 {) s
项目采用springmvc 结构。
0 v: ]* o. f0 j2 P8 v 所需JAR包:
! y) [( s1 C3 C- g c+ o# `) s
% C' D I/ i9 N9 q) J# m+ p& x
7 L O# C7 `9 r/ c ; F& i( L: O u' P0 Q. T; S
applicationContext.xml配置:$ I/ H8 _! \. t5 r: x7 z. [
<?xml version="1.0" encoding="UTF-8"?>
% s& M" e1 G3 G+ F' S( _ <beans xmlns="http://www.springframework.org/schema/beans" - }9 h6 p7 H% Q# _, H) r
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
) v ^/ ~" \2 M' M xmlns:p="http://www.springframework.org/schema/p" 1 V7 A$ Z5 X! B( ^2 ~' Q1 P1 K
xmlns:context="http://www.springframework.org/schema/context"
3 g c: D) q1 a$ M, b9 { xmlns:mvc="http://www.springframework.org/schema/mvc" 7 F4 `9 y! Y z
xsi:schemaLocation="http://www.springframework.org/schema/mvc
9 M+ G* g( Y/ Z. x http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd % ^" o" @9 h. g* z4 `3 v- D. z1 P
http://www.springframework.org/schema/beans 8 M3 O, V3 k. ^, l
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # ^ ~0 X! w% D& m1 \8 `) R
http://www.springframework.org/schema/context
X2 k. p7 \" q X; F http://www.springframework.org/schema/context/spring-context-3.0.xsd"> ' S; {9 Y# k) I, m4 ?) Y O: T
<context:component-scan base-package="com.mongodb.service" />
* U+ I3 Z1 c9 _" r <import resource="mongo-config.xml"/>
) ?7 q8 H$ Q) @9 B8 V </beans> 复制代码 mongo-config.xml配置:, T4 K" F! l- T/ [. b
<?xml version="1.0" encoding="UTF-8"?>
) f4 k0 ~! k: s; w6 @ <beans xmlns="http://www.springframework.org/schema/beans" : W# l! {8 C* ^/ V- m; m- t* h3 Q9 J
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 O+ p# e& F1 J9 j# v2 d xmlns:p="http://www.springframework.org/schema/p"
$ L0 n( r& M1 V0 C4 |3 \' w xmlns:mongo="http://www.springframework.org/schema/data/mongo" + k1 d$ s3 L* J3 H2 ?( p+ |
xsi:schemaLocation="http://www.springframework.org/schema/beans ) l1 s, p7 }- S( T8 i
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
' Y! x7 _# X# v; }! t http://www.springframework.org/schema/data/mongo
- O5 G7 h* T- [! }" }1 w* ?" Q http://www.springframework.org/schema/data/mongo/spring-mongo.xsd6 C, i: M1 V. y
http://www.springframework.org/schema/data/repository
6 f. S' @' b1 h% T$ _, i http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
* b& {6 F; @# T* D$ {) Q 0 L/ B8 @) r0 ? W
<mongo:mongo host="121.42.26.72" port="12345"/> 1 p } |& a& X* F8 [
. q8 e2 h% Q! } <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> % k# F5 K% B. H
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 9 J e5 ?& F% t2 Q# e% j' B
<constructor-arg ref="mongo"/> 2 _4 _+ `* u, e$ K- h% Y
<constructor-arg name="databaseName" value="itstyle"/> 1 _. E$ R; r$ \
</bean>
8 b( o4 E/ e# M3 l9 B- D
. ~+ Q& t8 _3 B& t4 b+ ^1 X </beans> 复制代码 9 W0 \, i; E( P+ l8 P5 R$ B: W
spring-servlet.xml配置:
5 B+ \% M( G& j/ ]7 z2 o( B/ K " R% \" K" ]3 N1 g
<?xml version="1.0" encoding="UTF-8"?>
# Y7 z, U( p; s" a$ v7 ` <beans xmlns="http://www.springframework.org/schema/beans" 0 ^3 A* r0 y) G
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 I5 y8 M& H8 a/ ], c xmlns:p="http://www.springframework.org/schema/p" " Y3 o6 _7 l% }9 X
xmlns:mvc="http://www.springframework.org/schema/mvc" ! a( y, S7 D7 _2 T3 Q+ i* P0 u
xmlns:context="http://www.springframework.org/schema/context" $ k+ e5 @8 p7 g; ?3 ]$ c- j9 l
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
3 w9 s! M8 A* I xsi:schemaLocation="http://www.springframework.org/schema/beans 3 g4 `/ u5 k, }, I! x% T
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ) E/ ~# K* U, z. j
http://www.springframework.org/schema/context
3 E4 w% v, C1 M& F3 Q4 g http://www.springframework.org/schema/context/spring-context-3.0.xsd ) R: D0 }! O" h1 [1 O8 I- |7 J
http://www.springframework.org/schema/data/mongo
2 u ~7 V/ V% @( a6 ]) @' R. g http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
0 ^- h! u. t0 _% K http://www.springframework.org/schema/mvc Z8 c6 P# p* C, W; C7 g0 j1 @
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">6 O# d2 W9 p! ^2 |0 Z( D
$ R' L% m) m3 I0 i1 I2 B3 s <!-- 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-->
y! Z! s! f- D% K* Q) F <context:component-scan base-package="com.mongodb.controller" /> / e- L# ?& x! b6 \6 C- |
& q0 `# y, x X/ ^$ h! [ <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
1 E- I" s' H7 T( f/ D* P <mvc:annotation-driven /> 3 d: R. {$ n+ V! q8 E
6 z/ {% H9 n) p. O- B <!-- Declare a view resolver --> ) W; h3 o C$ U# J4 u6 \6 n! G
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" $ t3 d" [+ a! d
p:prefix="/users/" p:suffix=".jsp" />
0 s3 ~6 q+ x2 |2 a& T, N5 ~
- Z, b9 Q1 @& H6 w4 l2 t
, c3 Q3 C7 U# `% K% @ </beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
; F/ T3 R* x3 w5 ?0 A! F% e import java .util.List;
9 y! i! j4 F# S2 O6 M6 j import org.springframework.beans.factory.annotation.Autowired;) G8 [% v3 N4 b- N
import org.springframework.data.mongodb.core.MongoOperations;' d7 z u$ z1 D
import org.springframework.data.mongodb.core.query.Criteria;, `' n9 k1 ?8 T0 Z* F6 }
import org.springframework.data.mongodb.core.query.Query;
$ x8 N+ ?. f) m7 Y import org.springframework.data.mongodb.core.query.Update;
" O4 ~+ p3 t# z8 n _" c9 D' i import org.springframework.stereotype.Service;: ?/ G, [- A7 [7 M
import com.mongodb.domain.Users;
S4 C& W9 x0 b @Service(value = "userService")
; ?, b6 Z! |+ j+ T public class UserServiceImpl implements UserService{ ; w" D6 N! w- G
private static String USER_COLLECTION = "users"; $ g9 b! P! P( ] k6 t
@Autowired 0 ~# n) A: f+ @
MongoOperations mongoTemplate; 1 ?5 \! N3 m4 |$ W0 \: g
public void saveUser(Users users){ ! R9 [: S; c, N2 m! ?
mongoTemplate.save(users, USER_COLLECTION);
A7 j, Q1 i! o! @* ]8 p3 } } % \/ R/ t7 S9 l* {+ R
public Users findUserByName(String name){
) V" B" J% [! T return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION); 4 x+ c% {$ p; z [. @( [; I
}
& u7 G" J7 e1 J) `9 ^+ z5 j6 F+ J public void removeUser(String name) {4 d/ Z. [, L9 x9 F$ `
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);) `7 @9 t# D, A# `+ X
}
C& C, J4 J1 X# f7 A& K5 T public void updateUser(String name,String key,String value) {, ]1 P$ I) R2 L: [3 j1 b
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
! @9 J1 m" t3 O# N4 _3 g
+ V& U' {2 `) U9 v ^1 d; u) y }
# K; ^7 Z5 Q- C0 e' U' A( u public List<Users> listUser() {
0 ~. L2 k: S c8 k; j return mongoTemplate.findAll(Users.class);' `# P% n5 N! z/ w X' Q; g- A
}
7 t5 x5 i5 O- N( D& z3 X }: R- w3 I' _; ~2 y! X+ |1 C" B
复制代码 : s7 r4 k0 y" T' M# }
+ J& J% A( U7 _$ o Q& W 演示地址:http://121.42.26.72:8080/SpringMongod 3 ?, d7 P8 Y7 b/ {( _! C: ]6 m
& R$ s) n9 s, b) b7 x+ \9 c 项目下载地址:Spring4 整合MongoDB详细讲解项目源码
' T8 y% h: v* A& l9 h6 }3 r 6 R3 y$ s/ r$ N0 u9 W2 T% E
解压码:www.52itstyle.com * d! Y. S: ~: ?! V, z9 P/ G
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :