TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码
- |( }2 h/ h# _ h: x) o Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。# S$ S" ?# V0 L- @
$ t/ v3 |4 N, U. {( A
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。
8 a4 M& S$ Y& K/ Q$ n 0 p5 A# J. u- h8 P
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
% Y2 s( J8 Z- @# R3 A, S
: B4 ` r' o. A( Y# L. N7 Z. K4 h 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
4 }) C ^+ S4 c) N- x
& W& U9 `) [& \9 e+ C/ n
4 O J E( N- L6 L+ s/ | 项目采用springmvc 结构。/ ~2 A8 M9 Q, d5 P; `
所需JAR包:
/ [) |, |# ?) ]& z2 U9 ^# u- U
: j. O d3 n1 X4 q i
% S) }7 t1 p# Z% f, I0 l* h; |
6 d- y$ W; N2 ]. @* F! I# ? applicationContext.xml配置:- h- g T5 m) D) l. k8 L; y
<?xml version="1.0" encoding="UTF-8"?>
" c7 ~ p) `4 f! z( ^3 e# a+ Z <beans xmlns="http://www.springframework.org/schema/beans"
8 |# r: ?- a5 ]4 l2 J3 u& ~" B* }4 _ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" . d* K2 o* b7 b1 b, T+ K% r( z
xmlns:p="http://www.springframework.org/schema/p"
1 x. G* ]$ J( o( M xmlns:context="http://www.springframework.org/schema/context"
) }) k3 o% Y: ^2 W9 a2 ]) ^ xmlns:mvc="http://www.springframework.org/schema/mvc"
& X" C9 H3 j; r S3 M xsi:schemaLocation="http://www.springframework.org/schema/mvc * ^; O6 _6 V) ^! X( J, ]4 ]" Q
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ! |1 O' E2 C( X" a' X; d( [
http://www.springframework.org/schema/beans . u! H, M R! f8 o9 T. ~( l
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 2 P" f+ b/ F) \6 _2 W% [
http://www.springframework.org/schema/context 5 m9 q0 d" D! D/ }
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> ! A1 k g" m% ]
<context:component-scan base-package="com.mongodb.service" />
, K0 Z, A+ F& A5 B <import resource="mongo-config.xml"/>% U6 P* ], `2 e3 ~0 I
</beans> 复制代码 mongo-config.xml配置:
0 I7 P5 e) \2 V5 {2 X) u ?1 Z% K I <?xml version="1.0" encoding="UTF-8"?>
7 I0 W( n) q6 m) o; g' J( W <beans xmlns="http://www.springframework.org/schema/beans"
7 A# l6 A) {: w8 S4 [) w6 L% t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0 p$ h: n* L. M9 l% J* V xmlns:p="http://www.springframework.org/schema/p" : s! q- n0 Q5 T
xmlns:mongo="http://www.springframework.org/schema/data/mongo" 3 f2 X6 q$ D6 d' [" @
xsi:schemaLocation="http://www.springframework.org/schema/beans
8 G: C2 g7 U; v: r3 T3 U& q http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
& l" n! y" Z1 l0 t0 o, B. _ http://www.springframework.org/schema/data/mongo
4 e+ n, s! p D5 [8 y http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
/ x5 V+ G- t) s$ w @) H2 M5 h http://www.springframework.org/schema/data/repository
* m3 u6 ?, G9 E' @- ~4 m9 Z. Y http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> + ^; [8 A& ^4 h# f
# @& k5 t. a) |* c
<mongo:mongo host="121.42.26.72" port="12345"/>
6 i" n4 [* \( Q: Q
6 F: F. b; a' e, r+ f/ K# [9 `# i <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> 8 Z1 Q) [# d6 ?" V0 n7 t P6 Z8 L
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 2 H' H2 ^7 [3 n% @7 f$ L" a
<constructor-arg ref="mongo"/> 8 h% b! H t1 k; G, |
<constructor-arg name="databaseName" value="itstyle"/>
1 D' Z3 z, z# Z9 G. X </bean>
1 H8 H/ V7 t) @- ~' p8 J
$ ^" Z/ { Q7 [5 S0 f5 C </beans> 复制代码
0 r, |5 X# B7 U+ Q8 `+ v spring-servlet.xml配置:) x/ _+ W2 o. V) V8 w+ w6 Y
( z8 R F# `! v6 \ <?xml version="1.0" encoding="UTF-8"?> + N) K4 g" ^" g5 `1 p
<beans xmlns="http://www.springframework.org/schema/beans"
1 V4 W* V6 Y P v' I \ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
n X" j" |$ }- C3 ? xmlns:p="http://www.springframework.org/schema/p" * x7 p D7 f9 Q" F! B/ X+ K
xmlns:mvc="http://www.springframework.org/schema/mvc" 9 v% W# H$ ]( `4 f
xmlns:context="http://www.springframework.org/schema/context" ' T: o8 e8 a+ o7 y8 ~/ u9 X
xmlns:mongo="http://www.springframework.org/schema/data/mongo" * e) w4 e% o$ J d
xsi:schemaLocation="http://www.springframework.org/schema/beans
! ~- v: ` g2 \* Q8 x7 \% h1 p+ y http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - D! @. K6 _3 r' Z' K9 I
http://www.springframework.org/schema/context
& |" v% p v+ M7 W http://www.springframework.org/schema/context/spring-context-3.0.xsd
2 I' F& r* X2 Z/ }- p- N( u8 R3 P http://www.springframework.org/schema/data/mongo 2 ^1 a/ Y; {& l* i% k; i
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd& W3 R3 V1 v% j& \9 ~
http://www.springframework.org/schema/mvc
3 V# P% d8 n3 @3 @ http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
4 g% Z. N4 B2 P* T4 O
; p) h# I* J4 s) g <!-- 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-->
( R' |7 w/ A5 C; |+ h) ~, ] <context:component-scan base-package="com.mongodb.controller" /> 7 H! D+ k/ `6 D' Q7 J& M4 ^0 W
# ^ j1 e! o) T6 z! a7 t9 P <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> & y0 p9 I3 N, G" u
<mvc:annotation-driven />
% H# _5 U: f& Z* K) p - M/ l! f/ k5 O- o4 L W. q! k
<!-- Declare a view resolver -->
9 A$ q7 H3 q; I1 g9 S% I <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
& ]2 A8 B4 v" ^' i4 g ~' B p:prefix="/users/" p:suffix=".jsp" />
( z A) F8 l4 b, L( k# x
/ f& C+ R! q/ M8 p! F + z" G/ x5 k- p Y/ h1 O4 J
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
' {4 A4 P; d& [) m% o7 m- { import java .util.List;: a) ?( {8 A L; _
import org.springframework.beans.factory.annotation.Autowired;
2 }0 _& S) [# I' T import org.springframework.data.mongodb.core.MongoOperations;% N' s2 L% s* l5 a5 }
import org.springframework.data.mongodb.core.query.Criteria;6 _% L8 s5 S1 i6 K4 L3 h
import org.springframework.data.mongodb.core.query.Query;. o* ?0 @+ b9 o9 L
import org.springframework.data.mongodb.core.query.Update;
1 a7 H$ u6 w1 {( A+ L1 F import org.springframework.stereotype.Service;# y* K! T o/ p$ E3 Z$ H N
import com.mongodb.domain.Users;" n- Q6 x: a: v- N$ \) A+ z
@Service(value = "userService") ( d, j% E! |/ r2 B) [; c8 Q
public class UserServiceImpl implements UserService{ & `1 L( j4 u8 Y4 A
private static String USER_COLLECTION = "users"; " L2 ? i7 z" r4 o5 L0 `
@Autowired
6 U/ `/ M7 d- V) F5 [ MongoOperations mongoTemplate; $ _% a, {) u7 Y; K& f2 v
public void saveUser(Users users){ 8 j- k% Y5 M; h R9 }, X
mongoTemplate.save(users, USER_COLLECTION);
7 U9 J* P! B7 F7 M0 z; D, o9 c }
) R D/ o6 D- u5 ^5 t" Y public Users findUserByName(String name){
# x. ^3 _/ w$ F/ z return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION); * j8 P& |3 G! _$ a" ~( R
}
# ^7 ~( ]' W, \ public void removeUser(String name) {; z8 {8 e: r, ]3 r: m" k. i
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
* c2 V2 m2 h0 ?8 x. u) _* k }4 |# h0 ]0 L W9 i7 C
public void updateUser(String name,String key,String value) {% q" ]4 n$ x% S h3 ]" ^6 E
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
, R& b% @# r- r% h1 y$ K3 ~
# {, w& t2 l( R, k! `3 Y7 ~ }
! P% N4 D' C' U# { public List<Users> listUser() {/ s4 f+ `3 S9 U- X
return mongoTemplate.findAll(Users.class); |6 z3 V$ N( E2 Y" v' J* F
}9 E; p5 \% f8 E
}0 S6 k' B" Y! B
复制代码
, R! m1 T9 ]+ k" V* O. B ! Y$ ]4 T: K; i# H
演示地址:http://121.42.26.72:8080/SpringMongod
2 R# h; C" S" q7 n& ]1 o 2 u8 c' B* X& q, Q. ^, e
项目下载地址:Spring4 整合MongoDB详细讲解项目源码 ! q4 k6 [4 T- S3 o/ W. ]; Q% B
4 E$ s$ j9 n/ L- y 解压码:www.52itstyle.com
: P# L0 h0 |& X2 K$ x6 \* W0 F2 {* N
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :