TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 # v# g8 f9 \! |. Q7 C
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
* K/ S, ?- Z( Q& i
5 @) M' w3 ?3 Q9 B: D. S* o NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。& c) {4 v& y6 t) ]9 R
5 Z- x+ a" B3 c8 C1 R
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
" ^) @8 x- N; U
: X. ~0 b% X' p( y/ y! S/ } 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
, [5 r4 a* O* N" C ; [0 P& Q% g, Y& T, @( u) Q! Q; h
0 H& `$ C- Y/ x/ d {
项目采用springmvc 结构。
( X# U# ~, {7 F! q" s2 I* s5 P' H 所需JAR包:* ?) D. G; J2 y
: M2 b, y# l$ W$ S$ |# _
: E4 f' t% B6 v* M9 h b
$ h* a: c9 S; D5 s1 V applicationContext.xml配置:
3 B! U, O6 [& U2 |. g$ s) x <?xml version="1.0" encoding="UTF-8"?> 9 g* y! H6 \8 D% \8 c8 Z! s
<beans xmlns="http://www.springframework.org/schema/beans"
$ `1 K3 h7 @4 H- S j xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
$ G4 O! w6 y( z xmlns:p="http://www.springframework.org/schema/p"
8 [; U0 T v( P) H& p xmlns:context="http://www.springframework.org/schema/context"
, ], F" F) N( |7 P" P$ Z xmlns:mvc="http://www.springframework.org/schema/mvc" ) [; [1 u( r/ P9 r. S" m) f! H" q
xsi:schemaLocation="http://www.springframework.org/schema/mvc
9 r Q+ R, g/ d8 U& q9 p http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
9 e9 X6 [7 B% }; e- S+ \7 V http://www.springframework.org/schema/beans
3 y: S( K* y8 B& q6 K8 v http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 5 J: K: x# r7 N2 H# s6 j
http://www.springframework.org/schema/context % j! `. t- e6 ^" ]- u
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
0 [, g% X2 m6 v, L, ]/ d3 Z <context:component-scan base-package="com.mongodb.service" />
3 w4 D# w& S2 v( o. F+ M <import resource="mongo-config.xml"/>3 K8 t) T2 e: N! F8 V x; N! h+ ?
</beans> 复制代码 mongo-config.xml配置:4 l+ b R( z! f0 K5 @! Z' C [- ?% Z
<?xml version="1.0" encoding="UTF-8"?> 9 ]* R5 p2 }1 c! O3 ^5 p3 ?
<beans xmlns="http://www.springframework.org/schema/beans"
) A- Z# X4 _* H/ L: n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 J) @8 A0 N0 k6 b! B xmlns:p="http://www.springframework.org/schema/p" # z; ^& ~" \+ C' d' L
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
" k. _9 X, P. u$ e: A8 A& I xsi:schemaLocation="http://www.springframework.org/schema/beans
_3 I: y! M$ Z: J# |' X http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ' c* R) `# N9 N( d
http://www.springframework.org/schema/data/mongo
4 A4 b, l3 @, m$ S$ C6 u http://www.springframework.org/schema/data/mongo/spring-mongo.xsd3 p* d+ ]$ \ t/ t
http://www.springframework.org/schema/data/repository+ h' p1 ^5 P! p/ ?" f+ x- Q
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
+ q4 B, d* ]5 N* q9 h
& L5 s, }3 l& Y) S+ C7 f2 W" E <mongo:mongo host="121.42.26.72" port="12345"/>
' G8 t: g" H9 C& x$ B8 K
# {. O% x" F* W; J/ \ | <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
# r7 E% H4 ?/ Y: T* W; N: S* q# E <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> $ ^7 v. }) b/ g Z9 n' o: m
<constructor-arg ref="mongo"/>
+ v1 _ \: Q. t% y2 ?- g <constructor-arg name="databaseName" value="itstyle"/>
' z5 R3 z2 A' _. s9 e1 t </bean>
" C7 v g4 R; `8 ^. S8 u) _ * Y2 z* Q2 Y" S
</beans> 复制代码
0 |* p) {! Q6 k spring-servlet.xml配置:
- {4 [6 O9 y+ X7 }% E# [9 }
s$ c. O# M# q# D: \! E5 X# e <?xml version="1.0" encoding="UTF-8"?>
+ f; [1 R$ x6 x, M" L1 G N <beans xmlns="http://www.springframework.org/schema/beans" 6 l7 H- z6 b- h( C+ B6 y+ R% q
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 b R$ l* x. ? xmlns:p="http://www.springframework.org/schema/p" ! P3 l' @: x* q! Q
xmlns:mvc="http://www.springframework.org/schema/mvc"
: ^8 }/ z% P* I xmlns:context="http://www.springframework.org/schema/context" 3 |: g7 R4 R9 H
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
8 x7 a3 i P4 _2 d" b# m9 I2 v2 O xsi:schemaLocation="http://www.springframework.org/schema/beans % }: h$ [! j. @: q
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
* |+ p$ H/ x7 e+ Q9 t7 e0 \ http://www.springframework.org/schema/context ; F% s7 F2 E8 s3 b
http://www.springframework.org/schema/context/spring-context-3.0.xsd B. ]" ^1 B- i- g0 ?% |% P1 M
http://www.springframework.org/schema/data/mongo 6 f2 ]. v- Y% b( r5 c4 o
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
0 Y0 q! Y2 i( b6 Z1 D; @ http://www.springframework.org/schema/mvc 9 w# Y4 a1 Z1 I* c& ]
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">, @ n' Z- e. c* W! C. p9 q3 I! G
* m9 z. ^- u- {
<!-- 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--> " X. k5 N3 D& X% U- ^+ v& @5 J
<context:component-scan base-package="com.mongodb.controller" /> - E9 s* G: y( Q# _# B5 p' z
7 g" Z2 y$ z& z$ y2 X# M' x
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> + p! D. J% ~% z4 ]0 r+ h( W
<mvc:annotation-driven />
. T1 N* k+ |9 f" `# ~
) o6 J u% e4 p* [ <!-- Declare a view resolver -->
5 }# V6 e; p# j" N; I <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 5 ^: o$ A" o# U# p6 A# m
p:prefix="/users/" p:suffix=".jsp" /> 5 B0 {8 F) F- A$ J F* w# e
) r) v- q( w6 W# G2 q0 D q
/ Y) s: }; b' e, l0 @
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service; |% y; o+ q& y) K4 L
import java .util.List;
; }& Z- J) y1 S3 N. e+ c import org.springframework.beans.factory.annotation.Autowired;
2 H. H$ B# b! I7 ~! _/ ? import org.springframework.data.mongodb.core.MongoOperations;
' }! i! j+ \$ j* `- c" X( }6 @ import org.springframework.data.mongodb.core.query.Criteria;* C1 e7 N. ?/ I4 K
import org.springframework.data.mongodb.core.query.Query;8 K7 f3 ?! b5 M+ F( y0 }2 f7 [
import org.springframework.data.mongodb.core.query.Update;9 g, {1 U# R( W8 D
import org.springframework.stereotype.Service;
2 \, J# Z. W- }- w- H; { import com.mongodb.domain.Users;
; ~6 u6 R' V+ i3 [/ q$ P @Service(value = "userService")
6 H/ g9 Q6 z1 q3 `& O public class UserServiceImpl implements UserService{ 8 M" l% ]% a( f5 E; ?5 f
private static String USER_COLLECTION = "users"; % x0 K4 M" I3 B. U# G% Z' ^' Y
@Autowired
0 E6 x; P* v* e MongoOperations mongoTemplate;
- z$ n" k8 B9 h" P" d, @: J public void saveUser(Users users){
8 p/ Y: d) q, R" s- q x: w mongoTemplate.save(users, USER_COLLECTION);
# S% h9 Y4 \( Y4 |2 H$ @, a } $ l3 D/ D, `: x: C7 J
public Users findUserByName(String name){; ]( F$ Y! N/ @! m% p0 V( q
return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
, D: R S/ ? x' [, _) W2 @ }+ I( n: Q# J% h8 P1 @
public void removeUser(String name) {
, ]9 r3 L# f( ~) k mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
) o) D$ j6 k; q" I! h" N }
; w4 L6 S: h$ E* r) M( b public void updateUser(String name,String key,String value) {! E% q2 m) Y* g+ [2 I
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);9 P4 y6 \* ?. A3 L
/ S- I, u1 [* ]+ A w }
+ V4 X/ D+ U6 A! Y; p9 f* U public List<Users> listUser() {
9 f1 F: W& ] Y2 E return mongoTemplate.findAll(Users.class);
3 f$ W* i7 S1 i% V* ^& S }1 P; _0 F3 V" W" [, p. K; x
}
1 i! q W& l% a ] 复制代码
4 V& M- g1 y. M! ^
' X2 Z! ~, O% f, B' q, V9 Y' ~- A2 B 演示地址:http://121.42.26.72:8080/SpringMongod . j9 u( Z5 F; Z
; s; I8 k- K7 z3 x# k0 p 项目下载地址:Spring4 整合MongoDB详细讲解项目源码
& [) h8 D0 J. C; H; {8 m) Y7 `
8 _5 Y# L! O! G6 h, s0 T: } 解压码:www.52itstyle.com 1 R# s2 T. h+ l- k6 u1 y/ A
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :