TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码
! v" \6 u: F2 j- i, l2 L! D) x Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。# |! D0 \# Y% v& j
1 q S( Y& s5 C8 s( ` NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。
: K7 N0 g1 @" E1 S; K1 }
1 m& l% v7 r8 c* J+ A$ q, i Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
2 H5 l1 C# u+ o8 s2 i: T - F( M' o& i! N8 m
环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
" t) X8 @9 l& r+ J: O 7 z5 r- W5 U ?. h
2 v3 U' V, ]0 Q) m+ }
项目采用springmvc 结构。! P' g) ~) ]1 f" d. d
所需JAR包:
9 C) |9 N2 G! z1 p- s& p
3 t8 E ?/ j5 C; N ; {9 _0 I- N/ c$ q: j1 l/ y' d
2 `% a( g% N0 l applicationContext.xml配置:
! w, e4 @; H! z5 m: a! Y$ w' B/ ] <?xml version="1.0" encoding="UTF-8"?>
- O' ]+ v- m5 T <beans xmlns="http://www.springframework.org/schema/beans"
, o# Z' m2 t7 ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* D/ x: A4 G- ]7 a2 w" M/ I" q h& u7 d xmlns:p="http://www.springframework.org/schema/p"
8 m k$ m* G: K* y0 Y9 s* A xmlns:context="http://www.springframework.org/schema/context"
5 l) ^9 v* j9 `6 k+ M xmlns:mvc="http://www.springframework.org/schema/mvc"
) q# @3 Z! N( ]7 K1 x! }& Q3 T xsi:schemaLocation="http://www.springframework.org/schema/mvc
: x4 x2 c, ~8 \2 ?0 b5 `0 @: O6 l http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- Y3 Q4 x2 ~' Q http://www.springframework.org/schema/beans
" ?# K* ?( T: D: h/ t( F ^ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd v; n: z; C! n4 H2 d
http://www.springframework.org/schema/context 8 Z7 \5 V9 d9 O! _* m1 C
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
# H! M& X/ R" B7 i9 p# b! c" C <context:component-scan base-package="com.mongodb.service" />
3 \/ D/ w# |2 A- ^) {( Z) j <import resource="mongo-config.xml"/>
4 G1 M4 ^' h- m: g# \ </beans> 复制代码 mongo-config.xml配置:
- i% N) b9 u0 \4 ~4 M <?xml version="1.0" encoding="UTF-8"?>
5 U2 C H+ ^+ @7 T% \* t8 u <beans xmlns="http://www.springframework.org/schema/beans"
$ A+ A6 |+ X% r7 ` xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ) d8 l2 a! y" W, v0 T, m
xmlns:p="http://www.springframework.org/schema/p" ! W- E' s& D& \0 Z
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
* F6 [+ A1 J' g/ N. a' U xsi:schemaLocation="http://www.springframework.org/schema/beans N4 z- y; g4 @, v
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
, \& ^ N: E7 }/ `; {3 D; ? http://www.springframework.org/schema/data/mongo & g& j, @1 f+ Z1 H* A
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
o# B' ^8 n! I, K# m C3 N k9 G. L: l http://www.springframework.org/schema/data/repository
6 H7 q5 d/ W) ?$ l7 ~ http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> ; X0 y: ~ m/ U0 c
9 O7 P9 ~- ?9 s4 Y, ]1 M7 T
<mongo:mongo host="121.42.26.72" port="12345"/> - x4 ?4 {; L3 P, F1 Z
; \9 F9 D4 a v4 p8 D
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> 1 q5 }! }# {( W- l- t6 f
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> & K7 C2 A4 c$ |5 l( S6 f% |3 O9 A
<constructor-arg ref="mongo"/>
+ z: f1 I* x5 u" F/ r: ` <constructor-arg name="databaseName" value="itstyle"/>
) F6 ~7 T9 c6 ` </bean>
6 V+ f: J. h, {1 M+ q
3 `+ C7 L+ D3 [/ k+ ^ </beans> 复制代码 # L# r/ P7 M( I& j+ T/ c; G5 I
spring-servlet.xml配置:
- h& |9 [2 g. W0 C/ T8 P" @ , \5 G3 ~3 h. w# B4 ]9 |, |
<?xml version="1.0" encoding="UTF-8"?>
6 `) j3 |9 c o4 Q/ Q <beans xmlns="http://www.springframework.org/schema/beans" 5 c$ Y7 t7 ]5 r/ l+ w; k
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ) I6 ^# L' l& ]! G- g- e" _9 ?
xmlns:p="http://www.springframework.org/schema/p" ( H, x5 e1 }& z( { C( a0 I7 r
xmlns:mvc="http://www.springframework.org/schema/mvc" ; J1 N: R, D* P; P; x
xmlns:context="http://www.springframework.org/schema/context"
- }& f* J" W) W0 _" Z xmlns:mongo="http://www.springframework.org/schema/data/mongo" 6 V3 R8 E% ]" I7 {
xsi:schemaLocation="http://www.springframework.org/schema/beans
: M% Z% G( z/ U1 T2 z! ^ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 k3 N W1 C# p3 s
http://www.springframework.org/schema/context - k' L! q |9 q* w$ a4 m5 X
http://www.springframework.org/schema/context/spring-context-3.0.xsd & X$ R5 g7 f) [; d- U8 J" E
http://www.springframework.org/schema/data/mongo
" q* s; w+ i' G- q/ n http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
, j1 t2 `2 t) B7 S7 ^ http://www.springframework.org/schema/mvc $ W2 M5 F, M3 J
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">; O& H8 M( B5 c, O1 }
' a+ h$ P6 v4 q6 y- s( C <!-- 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--> 9 ~" \. ^8 l N8 G
<context:component-scan base-package="com.mongodb.controller" /> ' B& [7 w7 \4 |5 j! A! I$ |
# \7 A0 A, w: a9 N
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
, ?- v0 e& W5 G7 l* [& O6 f9 S <mvc:annotation-driven /> 9 P% }. X1 X+ ~* Z1 i
; `: R. K5 R" n
<!-- Declare a view resolver -->
$ x/ Y: j- X* S. ^4 Q <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 6 ]4 F8 y$ E( D5 A
p:prefix="/users/" p:suffix=".jsp" /> ! C2 C: t0 C! x7 R1 A+ I; k+ p
% n" X1 m6 s& \$ n Z. w ! r5 S) k3 e% t* u) ^
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
4 V# H- d6 x z. Z8 Q import java .util.List;
7 B1 `2 Q* k! H2 k7 [ import org.springframework.beans.factory.annotation.Autowired;0 `9 r" P: Q7 f9 ], M8 b5 H
import org.springframework.data.mongodb.core.MongoOperations;
& t. j5 } U7 x import org.springframework.data.mongodb.core.query.Criteria;. O2 L' V- a* y E) ~
import org.springframework.data.mongodb.core.query.Query;6 L- O1 b D6 J( e. Q
import org.springframework.data.mongodb.core.query.Update;" E! ]# O- q( o1 I" L7 y
import org.springframework.stereotype.Service;
0 T4 ]2 H1 C1 N- \* n* s import com.mongodb.domain.Users;
5 e- r* A! w8 c- q2 W @Service(value = "userService")
1 f0 j6 _6 b. s public class UserServiceImpl implements UserService{
3 j' z3 k, l5 g1 H# H3 v5 P private static String USER_COLLECTION = "users";
- d: h. W! y! V: H, w6 ~- I @Autowired 6 a! Q- c) M( K/ K* v$ X. k! w
MongoOperations mongoTemplate; , E* q" R6 i4 Y' e; ~
public void saveUser(Users users){
; |5 V B8 p6 Z7 f! u% P+ `# L! q mongoTemplate.save(users, USER_COLLECTION);
3 Y$ L: |7 s1 u M }
0 L' z$ |# ]$ G8 z: p public Users findUserByName(String name){# X# T* b5 n' n! y: d6 G n# f
return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
6 @: L1 R* G; A }
# k7 f7 N) h9 [ public void removeUser(String name) {3 M' Z' B3 J" O: N1 a
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
, M+ C A7 @, s% J0 s( _7 ^2 D }
2 [6 r; |8 _% @$ a! j& s0 b0 M public void updateUser(String name,String key,String value) {
: M* m( I! \/ F% O) M" d mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);1 R. j, x; Y* ?7 |4 D8 {
. F( W, m n& t! _' ]# ]
}
" g- @- F6 q: x! J; X6 P6 C public List<Users> listUser() {1 Y" A. n/ W! E0 g; d
return mongoTemplate.findAll(Users.class);) H* s2 J; `' t1 C
}
& }% E- o" ~/ Z2 M2 W1 k. c" N$ ? }
! [" D" I K- I& U( e4 |- N 复制代码 / X1 D, F# p, l8 D4 k! \
# x( ?- w! B) ]- m1 E
演示地址:http://121.42.26.72:8080/SpringMongod
+ m4 y$ m1 ?3 f0 Y+ B" R" J9 }
: q* p @" s; T0 Q6 o2 O7 N; d 项目下载地址:Spring4 整合MongoDB详细讲解项目源码
7 v4 y! Y' h f8 e 1 D8 s9 H- _: O9 w' U1 T+ a* X5 ~
解压码:www.52itstyle.com
' y! B/ M" p' I" a9 s. i# Y5 ^% T
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :