TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合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 d Mongo 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 <?xml version="1.0" encoding="UTF-8"?>
+ A, J$ S& Q3 j* P. e <beans xmlns="http://www.springframework.org/schema/beans" . n0 m" i! p+ ?6 V1 k7 s$ M6 [
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 K( p. F: I* n$ \ xmlns:p="http://www.springframework.org/schema/p" . j% R6 y% S, L3 n' C
xmlns:context="http://www.springframework.org/schema/context" 2 S, \+ X; l/ S( ]" W
xmlns:mvc="http://www.springframework.org/schema/mvc" 5 ?$ g# d! ^) r9 b+ e
xsi:schemaLocation="http://www.springframework.org/schema/mvc
d$ ^/ ^3 b+ G8 a1 P http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd + |' e: p# @) ?" X
http://www.springframework.org/schema/beans 3 G3 A3 F$ g c+ w1 U
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd * ?4 U0 |# }* x K, ?! e7 M' V
http://www.springframework.org/schema/context
# ?6 `. s ^/ P9 @0 x http://www.springframework.org/schema/context/spring-context-3.0.xsd">
2 [* D+ Q' N9 V% R6 I4 M) _ <context:component-scan base-package="com.mongodb.service" />$ { `# _) |% O0 s
<import resource="mongo-config.xml"/>' K0 E( n, b0 E
</beans> 复制代码 mongo-config.xml配置:
) O% c9 r0 D; e7 ]2 F5 [ <?xml version="1.0" encoding="UTF-8"?> 9 ?; M/ K+ g- t
<beans xmlns="http://www.springframework.org/schema/beans" ; o* k4 a, j! R2 S$ Y8 q
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/ k2 s! J$ _/ r% X+ I xmlns:p="http://www.springframework.org/schema/p"
9 H. p: s0 Q) g& r4 O+ ` xmlns:mongo="http://www.springframework.org/schema/data/mongo" 5 P7 O3 Y5 ?; g% L( G% `3 Z
xsi:schemaLocation="http://www.springframework.org/schema/beans " k: t: c7 r( U9 A' o
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd % K" O" Y2 \, X/ ^
http://www.springframework.org/schema/data/mongo
* S) D, }5 F8 B http://www.springframework.org/schema/data/mongo/spring-mongo.xsd1 o1 s" n( ]$ b, v4 g+ V
http://www.springframework.org/schema/data/repository- o0 x N& v! Q( ^/ p5 Z! p/ G
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
) O5 k5 b) j w" x) J& M3 Z0 H
& }4 V0 I: U. t- @" j' ~ <mongo:mongo host="121.42.26.72" port="12345"/>
: V+ s+ G4 |" B& t8 a+ f
# R7 P- ]0 L" m2 c2 W, d( L <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> 9 E& O6 n2 v4 \( ?1 ?2 a
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 7 ^ K( ~: Z2 \8 O8 h
<constructor-arg ref="mongo"/> 2 W5 j5 S- Q# o- ^3 a" j. X
<constructor-arg name="databaseName" value="itstyle"/>
( Y. U9 f% M1 j1 F1 F+ e. W4 } </bean> ) |8 }* q/ r' y" ^& L- L6 P0 l, }
- b/ l& F4 H$ b) j
</beans> 复制代码
3 g' ?3 W6 J9 G( ^+ @8 ] spring-servlet.xml配置:
3 A7 \2 \- }3 T! ] ^! [, C) z 8 g! ^/ E5 y# u8 `& l
<?xml version="1.0" encoding="UTF-8"?> - [/ I/ l- ?/ k& I8 U. Q8 |7 w
<beans xmlns="http://www.springframework.org/schema/beans"
( ?9 H0 L6 G; x1 F' U8 J xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - b$ v. `9 T" n! B+ O
xmlns:p="http://www.springframework.org/schema/p"
% e' I- x4 ~; K; Q xmlns:mvc="http://www.springframework.org/schema/mvc" # o1 c# w6 _- k P+ w w0 ~+ q
xmlns:context="http://www.springframework.org/schema/context"
' l5 f2 ^8 H8 r& L- | xmlns:mongo="http://www.springframework.org/schema/data/mongo"
- h% ^1 a; q ?5 O/ W xsi:schemaLocation="http://www.springframework.org/schema/beans
% v' w2 B7 W* G0 u: @0 V http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 2 [$ C9 X7 x4 L% `+ Q$ @
http://www.springframework.org/schema/context
# l# p' I0 w4 B http://www.springframework.org/schema/context/spring-context-3.0.xsd 0 K, v$ d5 Q" U/ v. ~
http://www.springframework.org/schema/data/mongo
4 W; Y' q. x- c3 y' q http://www.springframework.org/schema/data/mongo/spring-mongo.xsd7 f( k/ l V$ k/ C
http://www.springframework.org/schema/mvc ) v9 M! g9 C) d
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">/ F0 W9 o' `# v: \0 Q3 t$ t- i
- ]8 z1 D; U" \ H
<!-- 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 <context:component-scan base-package="com.mongodb.controller" />
7 ?+ a! d c) ?7 W e1 n8 A& v
* r5 n2 m/ {9 z0 \7 ^" f' H7 A <!-- 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 <mvc:annotation-driven />
2 s, n, h- t9 W+ q* q
6 }. i* n3 V3 o& c* @: v) \- k <!-- Declare a view resolver -->
4 D! Q# p; [4 r <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
% L' [7 o: [, B' ~! i# c$ E p:prefix="/users/" p:suffix=".jsp" /> . t: Q+ a3 d9 P& Z# R- X! e
8 V$ t% `8 I' |: E' F
' X! h+ v- T" g: L' d </beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
2 h! P- F" C* F' C" S$ W' E import java .util.List;0 X3 [& `9 r0 t
import org.springframework.beans.factory.annotation.Autowired;, h# ^% H) E, C& P8 X& G
import org.springframework.data.mongodb.core.MongoOperations;
4 x/ z, e2 c6 w1 b# q1 \$ n1 o import org.springframework.data.mongodb.core.query.Criteria;
& ^9 @! b! X$ d7 i0 e5 ]# K import org.springframework.data.mongodb.core.query.Query;! `5 b' Y3 o8 g' e, S
import org.springframework.data.mongodb.core.query.Update;$ z8 R8 f7 c; _
import org.springframework.stereotype.Service;
' F0 V! g+ O4 ]) S& Q import com.mongodb.domain.Users;
& j7 X. L5 }/ v/ U @Service(value = "userService")
8 L: y4 V! o1 i! b7 t8 P$ J public class UserServiceImpl implements UserService{
, j9 T& r+ V. a/ {, b* j3 ^$ C/ c6 R private static String USER_COLLECTION = "users";
; z7 Y* N j% s5 z! ? @Autowired 2 R; u- P2 p1 _/ b5 C
MongoOperations mongoTemplate; ! o: V$ z, l$ `, w2 G5 N
public void saveUser(Users users){
* Z3 T2 M. l B8 l' U2 `/ b mongoTemplate.save(users, USER_COLLECTION); / V; I& T T/ L; r
} 0 k/ R7 C2 [0 ?
public Users findUserByName(String name){
( ^% P0 i' ?3 _ s return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION); 9 O3 G! g& W' k$ d; |) G
}
3 {- @" ]5 {7 Q& X7 a: B public void removeUser(String name) {0 I: j4 B) m; g
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);$ Y6 p' U" r- @, ?- l' t% |
}
: _: H- q" G t0 S. z public void updateUser(String name,String key,String value) {
* ]7 O2 H$ h6 u7 i; \2 A mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);0 E5 g, P5 \; a. J! E2 t
) N; I3 a r4 ^2 e
}
4 a% y+ n; G& L0 {$ s X public List<Users> listUser() {" v1 f! ^9 b& L; b
return mongoTemplate.findAll(Users.class);
& K! A/ y3 r& u" {9 q9 C }
' a: X( j. \. P& J" X6 t" e }) `; 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爱好者③ :