TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码
], C* b6 u" C) F& y7 T Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
9 K0 q, l3 `5 Q4 y4 B
) o: a0 g1 e7 I; W9 m NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。
# l9 o" n: u- V0 I6 N & K8 a( F, t! R0 c! X. u/ i; R5 ^
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
; p) q( R/ o% k% Y3 B$ }4 g: ~ ; O% a2 L( ]9 w, |8 {: C3 K3 W6 C
环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6) D' T- J3 Y. z* r3 l
0 B% b" ], e1 Z" u
( h2 e# F' T, s5 A 项目采用springmvc 结构。
, r; w$ y- H) D- F 所需JAR包:& {. [0 a- y' g! p
: s8 c: _2 Y# f& i8 l
5 O6 q! A& @, z- g' f$ d
, S y1 K- y: @1 u applicationContext.xml配置:* J- z5 h4 I1 Z% _
<?xml version="1.0" encoding="UTF-8"?>
0 i8 A3 N8 \' |. I, E5 {2 m4 [ <beans xmlns="http://www.springframework.org/schema/beans"
* m- v7 a& Y5 ^5 W xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + J$ L9 s5 N- l/ [& F2 W I" B; D
xmlns:p="http://www.springframework.org/schema/p" a- E3 `/ i- e, j5 \6 D
xmlns:context="http://www.springframework.org/schema/context"
: `5 P1 a4 x( a1 \ xmlns:mvc="http://www.springframework.org/schema/mvc"
2 V+ C5 u4 E! w% X0 q xsi:schemaLocation="http://www.springframework.org/schema/mvc 5 R5 M7 V/ u# k \
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd * t. |1 z% p. \; u; z, \
http://www.springframework.org/schema/beans ) \' \0 W/ \/ s
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd * \3 x( u9 G ?+ G
http://www.springframework.org/schema/context + @# \8 s+ e3 e) d' P
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> / y( b# S( s& w' w
<context:component-scan base-package="com.mongodb.service" />
5 x1 k n3 w8 W' E <import resource="mongo-config.xml"/>
/ m+ c z# ]3 ~5 `; y- | </beans> 复制代码 mongo-config.xml配置:. E) d& a9 f& H0 q
<?xml version="1.0" encoding="UTF-8"?> 7 \9 X" }" a W4 [' D! K
<beans xmlns="http://www.springframework.org/schema/beans"
f4 P7 t* ]6 x; @ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" # c; x& o9 j% k* I* y0 T
xmlns:p="http://www.springframework.org/schema/p"
+ E* c1 P, K, W$ I; [ t xmlns:mongo="http://www.springframework.org/schema/data/mongo" & q2 R# v: s0 c, P7 x# M% B
xsi:schemaLocation="http://www.springframework.org/schema/beans
1 ^ N" j4 Z# U; L http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
* ?' b- B: p: ^$ J http://www.springframework.org/schema/data/mongo 9 ~/ c% x6 V' O: c, w6 X$ x3 `' C
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
4 u9 [, l4 B' P* }# v http://www.springframework.org/schema/data/repository3 y5 H, j% n: o
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> ; T) y0 ~- U9 S7 P
6 g/ Z1 {) g3 x6 O <mongo:mongo host="121.42.26.72" port="12345"/> 8 I% X1 |4 I$ D) }7 ^
6 u. W) c2 v$ O% P
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
7 E5 R* R# @% A* ]/ L <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 8 E( n% P+ c: I' e+ ^
<constructor-arg ref="mongo"/> . _7 ? o5 w! P1 a2 j8 H5 |
<constructor-arg name="databaseName" value="itstyle"/> 4 A5 U2 b4 `& X" w
</bean> $ W9 i9 b3 {, X2 ?! z2 v! R
! u1 |: H# t' T* C% Y </beans> 复制代码
7 e2 K" z. V- E9 T5 D+ t: c spring-servlet.xml配置:
$ m6 M6 L9 t- ^! T8 b# Z3 o
) i+ \6 e) E# ` <?xml version="1.0" encoding="UTF-8"?>
& i+ o7 a' Q: v i <beans xmlns="http://www.springframework.org/schema/beans" k; Q4 @- I8 h5 n+ l
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" $ v* e9 P5 W/ k) _" y0 f- b
xmlns:p="http://www.springframework.org/schema/p" + p \+ D8 B: i4 e" }, a
xmlns:mvc="http://www.springframework.org/schema/mvc" 2 s" R4 e4 F' M: e! E: U" A
xmlns:context="http://www.springframework.org/schema/context" ( V8 h' [4 U/ X+ k0 `7 p
xmlns:mongo="http://www.springframework.org/schema/data/mongo" 3 F! N$ @3 e: O/ H/ c
xsi:schemaLocation="http://www.springframework.org/schema/beans * V- [; B) N/ A5 R6 j
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
5 ]4 F' o* c! n B http://www.springframework.org/schema/context 5 w: V* {. G' {$ J4 D
http://www.springframework.org/schema/context/spring-context-3.0.xsd . T9 r) f# a- E2 W4 @8 H/ b" R3 v; s
http://www.springframework.org/schema/data/mongo 1 Q0 {7 c5 H% B" s% |
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
+ b5 P3 h1 w" a! M: v2 A http://www.springframework.org/schema/mvc
7 R6 k$ W2 N' C* E8 M4 o( W http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
3 a# `; W* ~2 x5 a ' r$ e3 {' U( ]8 I* k ~! j
<!-- 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--> + n7 B" g1 C" i$ M2 F- [. S7 M: Z
<context:component-scan base-package="com.mongodb.controller" />
( y" l$ v& n/ R! h7 m5 l; r6 n ( n$ B- {3 i# W! P4 ^5 ^/ Y
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> 6 c$ ?; a# w& C, m) A/ n
<mvc:annotation-driven /> 8 U7 H; C! q6 x4 X+ s2 w8 Z' {
, ^2 t+ _) o' p3 `, ?# ?
<!-- Declare a view resolver -->
* ?4 Z% t% P: {: E8 K4 f6 Q <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 3 \% V! F7 \, i/ C9 g7 v
p:prefix="/users/" p:suffix=".jsp" /> 8 h m( f. n% P* R2 y8 ?
1 |2 ^& T: t2 ~& t! L& t) f1 y0 u$ P % {: {* o) q( T3 r1 u) ^" p6 ?
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
- h) b, Z% n, t$ ` import java .util.List;
6 c8 P: y% F( F5 O$ _+ T) d" r- x import org.springframework.beans.factory.annotation.Autowired;) ]+ t0 B ]0 M. o. `6 _5 y7 n; D
import org.springframework.data.mongodb.core.MongoOperations;
! W8 v8 ~6 T1 Y* D( V import org.springframework.data.mongodb.core.query.Criteria;
$ N9 j& L1 x9 [! L; V% x/ }% A import org.springframework.data.mongodb.core.query.Query;
6 k/ x' m! h0 s' v import org.springframework.data.mongodb.core.query.Update;" T3 z0 Z- t0 a' [6 n
import org.springframework.stereotype.Service;
4 c n c; p M import com.mongodb.domain.Users;
, d! K# g" t: u0 t9 v @Service(value = "userService")
7 B/ h9 p% E5 o$ q* E% e public class UserServiceImpl implements UserService{ 8 a( ^, o) }1 K( X
private static String USER_COLLECTION = "users";
! f% h3 f- y6 S3 L! n) t4 R @Autowired & [& D$ m4 ~* q0 j0 w0 p5 i
MongoOperations mongoTemplate;
( Y% p5 T, \* `& v9 D public void saveUser(Users users){
+ ?, K) [# z( w mongoTemplate.save(users, USER_COLLECTION);
7 N. d! c8 p: r8 X }
4 @+ W8 @! \3 r* n. S4 b public Users findUserByName(String name){
) L4 K3 _/ H. C4 r# H: } return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
* g) l: I J8 K. i& x2 }9 X9 b }
" r! h, X6 v* M- q* { public void removeUser(String name) {% `& {# O* F) l5 D1 F# [+ a/ K
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
( o) x$ X3 d0 a0 Q1 E7 k+ ? }
. f ?( r0 B- y7 {1 H, P public void updateUser(String name,String key,String value) {
. F3 d& T9 u. D# {+ H mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);" j. Z: V5 N8 O Z3 B$ p% O
* N( n- w6 h9 I- W1 p1 _+ } }
" a8 y' J) t3 J+ Y, E: o public List<Users> listUser() {1 K% X0 p3 f' o( v7 d% U- x
return mongoTemplate.findAll(Users.class);: J3 G7 P% \8 W7 ?( v- ^; R
} r( ~9 Z9 S& T6 B
}
* V5 z. u& c9 q6 Q 复制代码
. N ~/ o3 j. i7 I' k! e7 U + g# }- ~6 ]7 _% v3 D8 x
演示地址:http://121.42.26.72:8080/SpringMongod
6 N t7 V; a- q! G# ^) W 0 T' `9 B) V7 U
项目下载地址:Spring4 整合MongoDB详细讲解项目源码
( h% W! z# H2 b( F8 U! W
, I. W6 `2 I7 a1 m8 t$ r3 z! g 解压码:www.52itstyle.com + s% e( K3 M* x/ n4 _/ r) l6 R
科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网-Java论坛、Java社区、JavaWeb毕业设计 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :