TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 2 _' y2 W% ?: s0 c9 H
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。/ x6 R, J- p) {2 |& G' F/ Z, u
5 m+ U7 c3 W, ]. z NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。% H; _# c" d) q/ q; A. o( ^5 d+ j) b3 J
+ s* g' F2 \; S2 i Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。! y, i. @7 \+ j
7 U O) w0 f) @; W+ T9 U: l 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
' |6 S4 ^8 P+ s3 t) W; R
! O; }& s1 y p5 l5 f! j3 S3 ^
/ C3 }+ _/ m* `. w 项目采用springmvc 结构。
& n2 U. m! H( W& q0 _/ | 所需JAR包:, R* k4 O! `6 a e
; ?* Z$ w8 g/ e+ b/ A( S( V2 s3 S+ F
* G* W' }/ t# }6 G" o, u( q7 [ - L3 e0 i* D/ o% e4 j
applicationContext.xml配置:9 h( M7 F% F' K4 Z; F
<?xml version="1.0" encoding="UTF-8"?> ; Y, W: a4 _$ |5 H& j
<beans xmlns="http://www.springframework.org/schema/beans"
& H" h: ^0 N' ?) v xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 8 u V. u$ w# ]+ p8 k4 F4 D1 E
xmlns:p="http://www.springframework.org/schema/p"
2 x( l' ]! {; y9 I xmlns:context="http://www.springframework.org/schema/context" 3 I8 C6 g: l7 ^. u
xmlns:mvc="http://www.springframework.org/schema/mvc" ; \9 t& |" W0 |/ C2 z( N
xsi:schemaLocation="http://www.springframework.org/schema/mvc 9 j1 z3 N6 J- F7 O0 U
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd : D) t. J2 }" X1 s& R. g) X/ @
http://www.springframework.org/schema/beans 0 v+ [ _; L+ P" n
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
4 |. K# t. g2 e6 _# W http://www.springframework.org/schema/context ; I& V {8 l! H+ }8 Q6 T
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
/ m. F7 `: ]" K! @" ?, B. | <context:component-scan base-package="com.mongodb.service" />
; i) L; j0 j g8 s$ T <import resource="mongo-config.xml"/>8 H3 A: i9 K) I$ V
</beans> 复制代码 mongo-config.xml配置:
; e2 v3 L5 H" t* K <?xml version="1.0" encoding="UTF-8"?>
8 x8 ]" y9 P+ p <beans xmlns="http://www.springframework.org/schema/beans"
7 s/ W. Z/ s, o8 o3 r" u6 r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + ^# e% U( Q6 D" c! G
xmlns:p="http://www.springframework.org/schema/p"
0 X8 a0 A) Q; g xmlns:mongo="http://www.springframework.org/schema/data/mongo"
" I9 a2 t0 R( J" R xsi:schemaLocation="http://www.springframework.org/schema/beans
) S# @! o- H/ Y% C6 M http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # {0 I b% `2 H0 K1 k
http://www.springframework.org/schema/data/mongo
. J- Q ?4 n: @/ \! y& U/ Z, k% M http://www.springframework.org/schema/data/mongo/spring-mongo.xsd& @7 ?' s. ?/ D2 a% @0 Q- q3 s
http://www.springframework.org/schema/data/repository* B8 N# u- Q: c0 Z9 u) B
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
# x$ M7 i7 K) m o$ n
& h/ {$ k6 E7 R, S* w <mongo:mongo host="121.42.26.72" port="12345"/> e2 G3 _* C+ W* a7 }* r
' A8 G4 o9 s' G <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
8 K( k! U( d* R+ `- F9 d <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> : J" [7 \/ |, ~) }
<constructor-arg ref="mongo"/> , e; N- J4 p) W8 P: `8 U2 q
<constructor-arg name="databaseName" value="itstyle"/>
; C6 T% N8 `. R2 {. q; C </bean>
$ D% t. F' L- l# F: T 0 E2 X3 i: ~$ w; E. b
</beans> 复制代码
8 p3 A) v# i& f% y spring-servlet.xml配置:% {/ @; `8 h% Y$ m+ \, S; b# G+ A3 {6 s
) y$ W$ A8 ?# z# A+ r7 ]! O
<?xml version="1.0" encoding="UTF-8"?>
1 v' C1 f% ]) q) N7 p# \4 b! t/ p <beans xmlns="http://www.springframework.org/schema/beans"
, @7 ?0 G" F2 h xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 v2 _' V. A* c% _. A) ? xmlns:p="http://www.springframework.org/schema/p"
3 N7 X9 V! k6 I xmlns:mvc="http://www.springframework.org/schema/mvc" ( W0 N$ ~: ], A/ E0 }, M
xmlns:context="http://www.springframework.org/schema/context"
. J) \, f/ H# l+ t; F' ~" D' E5 B xmlns:mongo="http://www.springframework.org/schema/data/mongo"
4 F! j/ O$ W3 C1 X3 {4 N$ O( i0 F xsi:schemaLocation="http://www.springframework.org/schema/beans
- L/ \$ J& o! M1 R- o" n) L( [* _ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + |1 C) r& ?: Y
http://www.springframework.org/schema/context
8 ^/ M, t2 b0 M3 {* c http://www.springframework.org/schema/context/spring-context-3.0.xsd ' F( q9 n) L: V( _, o, l, W. U
http://www.springframework.org/schema/data/mongo / E8 Y/ X7 b7 d# `3 o
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
0 @' Y( W0 I" }6 z, p; @ http://www.springframework.org/schema/mvc
, Q3 Z1 J" e# ?; c- B4 ` http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
% U" e; E% ^; P5 H# C+ A$ Q ; G8 O% n' W8 E8 ~
<!-- 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--> & c6 h+ F7 N- `& f0 P: c2 E @
<context:component-scan base-package="com.mongodb.controller" />
4 X' P: {' t6 u7 k! A* g
7 \5 c" A0 O- x ]" ]2 `: h9 Z- t1 g <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> 7 U5 _2 w2 c: t8 { L+ P! E1 o5 F
<mvc:annotation-driven />
8 D# V1 o: \2 n# H4 p+ M
% ~# T+ I* A% z$ S <!-- Declare a view resolver -->
\6 G4 o( v2 W" t <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
0 b1 M0 O- R. a! y p:prefix="/users/" p:suffix=".jsp" /> 1 l6 l7 P' ?) E3 { w
+ m# U) [& ?( D: ~/ x1 }8 w
, P8 ] h$ N3 V5 `
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;7 P. M( B6 U& Z
import java .util.List;0 L) T2 e5 {) x) i* S# t4 B
import org.springframework.beans.factory.annotation.Autowired;" B, c- |3 M: _" c1 _2 _3 v: @# ]
import org.springframework.data.mongodb.core.MongoOperations;& s9 |( T; m, S
import org.springframework.data.mongodb.core.query.Criteria;8 }, _2 u c# v% p1 G
import org.springframework.data.mongodb.core.query.Query;8 F$ v, z+ l- a6 Y
import org.springframework.data.mongodb.core.query.Update;
, \3 g$ l' o) h1 Q6 R$ O import org.springframework.stereotype.Service;
I, E. \# M/ ~6 E: F import com.mongodb.domain.Users;
/ H7 [: {# {% p @Service(value = "userService") ; [' P/ E2 r) V# S, s
public class UserServiceImpl implements UserService{
% [4 J; H; l6 |6 @( K. E. ?2 Q private static String USER_COLLECTION = "users"; 8 j4 y+ B. ~% M& }
@Autowired 6 A" `! h: @4 f* j" Q. y
MongoOperations mongoTemplate;
) k! [' \. h/ ?' l3 ~( m/ L7 `* Z Y! x public void saveUser(Users users){
3 `+ ^7 ]/ `* B+ o$ s9 X/ O mongoTemplate.save(users, USER_COLLECTION);
; S( R2 I+ B8 ^! R }
% {# a4 v$ K, D, R public Users findUserByName(String name){
1 ?- S( ?. Y7 s) o) @ return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
# {' ?- _$ n# i3 k" b; ~8 t; W2 ` }
( [5 Q' [6 _7 F. B P# |! B% k public void removeUser(String name) {
" ^/ s3 x/ U m& B4 z( v0 [9 _2 \ mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);! k' u# Q* O8 P. C
}" y; ~# F/ Q# C9 J; Y" y3 Q. Q5 H' {
public void updateUser(String name,String key,String value) {
$ `) ?6 N5 N7 q7 q; O* H mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
! J' |. n! }$ g3 X& c
7 `- g( o H( d+ V }; @$ h. a' m: Z
public List<Users> listUser() {, v9 v5 L' A+ {1 P. K: W6 F) y
return mongoTemplate.findAll(Users.class);
4 u K, W8 [2 B. o$ t; C; ^ }! s. `/ Z+ _/ ] _; @
}% ?- x- X( m [" _( S
复制代码
* n& [2 g8 X# ~% M7 k. D
% Y8 _. P% X- G4 e. B 演示地址:http://121.42.26.72:8080/SpringMongod - O9 Y" n0 z; z3 W9 t" s; E5 W5 ` o. {. @
3 s! [# C7 ~9 N; T. F0 `
项目下载地址:Spring4 整合MongoDB详细讲解项目源码
1 E9 C% I* _) o# b! ~ 0 i& H" M6 _: s( M, `( ?0 Y
解压码:www.52itstyle.com " o' c+ ?, @ M9 i7 ]5 h6 J N2 l
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :