TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 8 o6 G- H8 U& o% n( W$ A
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
' E+ G8 |- m; l2 I+ J" U1 E . {6 }! d6 q( ?8 C# V& p& S4 ~' Q
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。1 A. {: l8 d( ]) C8 y3 k! x9 b
0 L& j- W- c2 {, _, p
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
! e( y. o( J% ?) V' r. L* ~
4 @4 f+ W: ~3 Q4 {/ \) P 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6* n/ D7 g2 x) z; K; R" v
; G8 l- H$ S3 j( \1 N3 u% v+ U
& _1 p& c- o/ R2 V 项目采用springmvc 结构。
3 V7 V3 i' R# U3 p- N8 ^- ]# f 所需JAR包:
5 r8 \* d: R% e7 l2 B
5 w- j: c# p! Q1 s: U# \( @
7 o; z3 s ~$ t5 F: r& m% S
7 m \4 S, t/ m applicationContext.xml配置:
6 T- w. m% T$ _ c# x1 ^ <?xml version="1.0" encoding="UTF-8"?> ' Q" V9 V0 h! ^+ u1 d9 e8 f
<beans xmlns="http://www.springframework.org/schema/beans" 3 Z) V8 M7 f7 J0 W3 [
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 z. M9 P9 e% N, O& u9 ]" l xmlns:p="http://www.springframework.org/schema/p" % O3 _% {* ~: F
xmlns:context="http://www.springframework.org/schema/context" : i' q- l% s8 k' r& I% g& o) W' I
xmlns:mvc="http://www.springframework.org/schema/mvc" 7 r, A3 {6 X# Z7 Q- z3 d
xsi:schemaLocation="http://www.springframework.org/schema/mvc 4 ^0 [) w& a0 s6 v4 O) Q
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
0 v5 b) e$ \/ {" {) E* N http://www.springframework.org/schema/beans $ ]' d# r6 w* J
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
& ?8 `- W8 M. K8 t http://www.springframework.org/schema/context . J: ~- v& ~ n! h; n, Y# H
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> ( |: u3 O# t5 B
<context:component-scan base-package="com.mongodb.service" />
/ A/ E7 b/ v6 `# X S* P5 C <import resource="mongo-config.xml"/>( j% i7 H8 ]/ H: M6 i/ q
</beans> 复制代码 mongo-config.xml配置:
8 ^; N& ~9 A4 _) W( |7 ^. I <?xml version="1.0" encoding="UTF-8"?>
) n& M* K: @+ v$ h <beans xmlns="http://www.springframework.org/schema/beans"
, }4 z' N$ t- p8 q5 ?4 D4 x+ Y+ K7 e xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8 S% A+ L9 g6 n5 W) W8 H xmlns:p="http://www.springframework.org/schema/p"
- U$ ]. ] g- T% q xmlns:mongo="http://www.springframework.org/schema/data/mongo" ( M" S0 L0 u: o' y I, B5 W( J1 }- \4 S
xsi:schemaLocation="http://www.springframework.org/schema/beans
; u8 M. p( ?; u% ?: ?, F http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # g+ \& u+ g7 U' _) ~) K: e# `
http://www.springframework.org/schema/data/mongo : S, z* Q' N! L* j/ Q& g/ A
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd [7 D3 j: r! I& T, ^4 Z
http://www.springframework.org/schema/data/repository9 G, L F1 J6 G0 T
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> . q. c1 O! O7 E* o
( P( f* ]& Q/ F$ p3 l: j7 R
<mongo:mongo host="121.42.26.72" port="12345"/> ) E$ J$ K+ k1 D% A
, E. s& {/ m2 X f$ h* P
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
- G3 ~& k1 K$ M8 ~# {" R& @ <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
k0 {3 x, p7 D3 M; l9 I' d3 z <constructor-arg ref="mongo"/>
" K+ ~- e1 Y' c1 @2 I. ]6 { <constructor-arg name="databaseName" value="itstyle"/> 9 I# Y" _1 A# K
</bean>
+ R3 z% |; W# j8 O. h+ o3 y 8 \- I- a) W0 s4 a
</beans> 复制代码
. p3 M6 ]3 V2 K2 U* U7 ^4 R6 T spring-servlet.xml配置:" s* C9 n' [# ^$ t: m. U
! q1 d3 X9 A0 c
<?xml version="1.0" encoding="UTF-8"?> # u( I8 w# ?! b% i$ e. u! S
<beans xmlns="http://www.springframework.org/schema/beans" ; t) w4 X* M2 z7 F: ?9 g
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" & W ^. {: z2 g% }/ H
xmlns:p="http://www.springframework.org/schema/p"
; l3 A1 U" j m2 U) k& [8 W) Z% K xmlns:mvc="http://www.springframework.org/schema/mvc" 3 w( A* j5 z$ g3 I+ W
xmlns:context="http://www.springframework.org/schema/context"
! e9 D1 X- a; h xmlns:mongo="http://www.springframework.org/schema/data/mongo"
. v6 H, G1 o% `. e. O6 V! s xsi:schemaLocation="http://www.springframework.org/schema/beans
. X" d1 d: T1 C, X) | http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # f4 `8 R; `' m4 V
http://www.springframework.org/schema/context ; V! e5 p2 T) R% w
http://www.springframework.org/schema/context/spring-context-3.0.xsd - O. R# Y- Z. k! X* h
http://www.springframework.org/schema/data/mongo ! f' E0 r- M) e! _+ w* ^4 s! d2 J
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd' ~2 }, L3 N8 c, [
http://www.springframework.org/schema/mvc
; ?; y$ B3 n9 F; ^ http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> s% _7 o' p# I0 D2 k
: K3 j8 o% ^2 c: Z1 V6 [9 A+ i) Y
<!-- 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-->
6 s" I- k8 I( Q3 x% x <context:component-scan base-package="com.mongodb.controller" />
- o; w/ P( S6 N% ]% k1 h 6 k- o, `3 K M, z$ @' h/ I- N
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> 2 n2 k3 d' q( E1 t; v% }! f
<mvc:annotation-driven />
) R0 x9 _ u# \3 o U8 T
" Q1 @# D9 g1 ~ <!-- Declare a view resolver --> ; ~- D3 H$ p, T( ^4 K
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
$ e, V2 F( b) p3 U; u- J$ r N; I p:prefix="/users/" p:suffix=".jsp" />
! F0 \3 }3 u+ v% g5 ? ' m8 P* I; x Z0 Z3 M
0 {7 m1 h9 ]# m- J2 a6 v$ l
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;, i6 t. t- f) L$ c
import java .util.List;. _' p6 s( K) b3 K9 X
import org.springframework.beans.factory.annotation.Autowired;5 z- h8 |1 l( @, F
import org.springframework.data.mongodb.core.MongoOperations;
' m4 a5 V5 R. T' W8 c3 d9 w import org.springframework.data.mongodb.core.query.Criteria;
_; M: I7 `" N3 C6 ^ import org.springframework.data.mongodb.core.query.Query;, o3 h3 u: Y6 _) n% {) D) k, C
import org.springframework.data.mongodb.core.query.Update;
5 R6 ^4 ~; Z j; y6 X3 ^ import org.springframework.stereotype.Service;
+ w: h. w. s+ L import com.mongodb.domain.Users;4 c6 }* f# `3 x' F/ Z
@Service(value = "userService") % q8 f; H( I# a3 o9 l
public class UserServiceImpl implements UserService{
. l' C; l& F2 ^: o4 N* p private static String USER_COLLECTION = "users"; + m" X& C% J: k, B \
@Autowired
Q/ E6 X& t& F9 _# G/ F6 u MongoOperations mongoTemplate; " c5 S. k; `( l- s9 \
public void saveUser(Users users){
; a; |( ~6 c P mongoTemplate.save(users, USER_COLLECTION);
% Y- X: g; S6 Q' ~( K" { }
9 ~3 h" ?$ P- {1 F: t5 ~+ m1 z public Users findUserByName(String name){
* k( l; \2 B* [( @0 M( C d return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION); 9 D: ]- H% t9 X B
}" @7 b9 D$ k) L5 u( D
public void removeUser(String name) {
2 _; M' a B( }' k L9 u" Z mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
8 f4 ~: E5 z8 _. y! M2 N }
; u( U# A! ~9 s. ~! b$ S public void updateUser(String name,String key,String value) {: ]8 m4 Y" b7 b L/ o# _
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);+ R8 H& R$ |! e [. j
% V! {1 [' ^; X- M5 K/ ^( b }
, [0 V1 x2 f0 }2 [ public List<Users> listUser() {/ o' L1 c! C b0 H7 C7 u }( W
return mongoTemplate.findAll(Users.class);
! r$ i4 L6 ~* i3 V/ z" M }! K E0 P% q/ P- W; w
}
" d' S+ w' H, o- k! Q 复制代码 4 D9 p- Z: z, e, W+ L$ t6 U
5 W8 E7 W$ N. L3 r# ] 演示地址:http://121.42.26.72:8080/SpringMongod
2 y; D" d: T# e- h$ u
* T; K7 k& V! B! J7 e Y: e4 g 项目下载地址:Spring4 整合MongoDB详细讲解项目源码 " L3 m& P, i. `6 ?3 M0 \% {+ f
) l% A: ~3 j" X4 |5 V 解压码:www.52itstyle.com o I& V# z" p; x" c: R
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :