TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码
* N# `( c5 b% K/ G. x H, N Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。& w9 N* I6 o1 T! |
! `4 h) d0 Z- Q- X NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。& L( Z& H9 v, l) ^8 B
$ s9 |. a; K. j% g) T Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。* T) I3 s" }2 u" v) m" H- y
- R; W3 {3 }! T" A1 b
环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
9 }! A5 R% L. ]5 t `: U
3 N! \* A* ^& T7 ~
: E7 s2 ~4 O0 u3 `; U5 c+ P 项目采用springmvc 结构。
" V' i. X1 d5 ~+ Q8 v% f 所需JAR包:
& s0 W; G: B' U. d
7 m4 B+ ^. G1 z# b* ~1 G" F8 F' k+ b
: o- I: ~: y1 B! M+ I# d4 M4 Q9 {* ? 2 k$ ]$ q- P( |7 Q/ a
applicationContext.xml配置:
$ ?) e( i2 G5 A2 L& k <?xml version="1.0" encoding="UTF-8"?>
- W5 t& D0 o0 f# N, I <beans xmlns="http://www.springframework.org/schema/beans" & {+ l2 c# z8 ]: Q/ I
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 8 z/ y( X1 ?$ r8 X: a1 B. E
xmlns:p="http://www.springframework.org/schema/p" ( K. O. ~* F3 n4 \
xmlns:context="http://www.springframework.org/schema/context" ]& I* w5 G: m4 |: _: I( |
xmlns:mvc="http://www.springframework.org/schema/mvc" 4 H) |& J# A3 P- T
xsi:schemaLocation="http://www.springframework.org/schema/mvc
: d& w1 ?0 \* p2 }6 ?8 o http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
3 c7 r: _) P0 ` http://www.springframework.org/schema/beans ) Q Y& x: k+ Z$ E! z+ s5 E
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
5 ~: Y# U$ [, Z http://www.springframework.org/schema/context 0 {; ?* Z$ o& f5 i. ~& t0 o& W
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - T, s6 g& g3 Z' }6 h
<context:component-scan base-package="com.mongodb.service" />, {: s" o; e |# B9 U# H- q9 n
<import resource="mongo-config.xml"/>% |9 w! t; M( J/ D6 J3 m9 c
</beans> 复制代码 mongo-config.xml配置:
( p8 J6 G' l/ Y! N- E, Q2 G <?xml version="1.0" encoding="UTF-8"?> 5 \3 a5 F7 |2 k4 ?) V$ D
<beans xmlns="http://www.springframework.org/schema/beans" ; d* _% e5 S$ T
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ( ?" j6 Y. R# w7 ^# O/ ?
xmlns:p="http://www.springframework.org/schema/p"
1 g2 L, y, N, d B xmlns:mongo="http://www.springframework.org/schema/data/mongo" , l9 Q" A4 |; [( x" o
xsi:schemaLocation="http://www.springframework.org/schema/beans
6 U) G$ {- \# g) | http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
3 W, \: V# X! m0 `/ T http://www.springframework.org/schema/data/mongo , R' _5 d! u y8 K- q
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd% Q8 |1 m/ y" Z( }$ c2 S$ r
http://www.springframework.org/schema/data/repository4 ~( V9 L4 D2 l8 `
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
9 S5 m3 J, L* L! k0 c) Y; c, g * N" Z1 R. `0 F* q& y$ V4 d3 D
<mongo:mongo host="121.42.26.72" port="12345"/> 8 j- ?2 c6 J. g# z O
. [; C) M+ ]5 B& c <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
+ b* t9 m( }0 u- M4 y <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
) z" M$ v8 O, v4 x4 B4 ~ <constructor-arg ref="mongo"/> ) h- I6 A" B+ y
<constructor-arg name="databaseName" value="itstyle"/> ( u# x ?# ]8 T' Z* _2 a r
</bean>
: I% t2 h, q% W3 r' o
! b/ K$ A8 m* F1 B# w </beans> 复制代码
; d) i; ?& J7 J8 f @2 Q spring-servlet.xml配置:
3 A, p8 R4 C5 f- U1 E- [
5 l/ U/ p5 [' J2 d4 ?* ? <?xml version="1.0" encoding="UTF-8"?>
# G( Y- n1 j# N- h <beans xmlns="http://www.springframework.org/schema/beans" ! j2 @- c2 ~+ }, |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
" c5 T' o4 x ]* z xmlns:p="http://www.springframework.org/schema/p" 2 R, A, ?$ _5 d* C1 ?; x9 h
xmlns:mvc="http://www.springframework.org/schema/mvc"
1 b) M6 I) |' b5 E2 E xmlns:context="http://www.springframework.org/schema/context"
& w& q7 ?- l/ ]" A! h xmlns:mongo="http://www.springframework.org/schema/data/mongo" 0 d; \0 F0 Y- b9 G: ]. N
xsi:schemaLocation="http://www.springframework.org/schema/beans
5 R) @' H( p2 L http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
! N b* z8 g+ `3 J3 T( I b9 Y$ R http://www.springframework.org/schema/context
2 d4 `) ^. Y7 ^$ {7 [2 {( E( } http://www.springframework.org/schema/context/spring-context-3.0.xsd
2 N" O6 g/ C# G8 w8 n http://www.springframework.org/schema/data/mongo
* c$ Z5 y4 @ w# F http://www.springframework.org/schema/data/mongo/spring-mongo.xsd- H/ ~3 d, P6 u
http://www.springframework.org/schema/mvc 7 _" X$ K0 J3 N* J z* R
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">0 d7 Z" q# ], p/ {9 v
$ K) ?3 Q7 V- v: |4 P$ k9 K <!-- 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-->
5 q' B/ ^& W! G8 M9 Z <context:component-scan base-package="com.mongodb.controller" /> 6 K2 A( g3 v! t" J7 f
+ B, t4 o2 r5 b% l& n# f
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! --> 9 `5 m1 g/ s' u5 C& i
<mvc:annotation-driven /> " `- Q9 @1 X% k R2 f( m2 K
5 o/ V% q2 m/ r, m1 ^
<!-- Declare a view resolver --> " R2 c! W/ O2 s. `3 Y
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
' O0 g& g3 o! U7 A K3 D; J. Z p:prefix="/users/" p:suffix=".jsp" /> o- f- W( g+ y ]
/ `* v8 y/ W( q; N M1 X5 x- O
( r3 g- t) i1 U% Y; ^+ O8 X
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;9 U8 q1 d: |- f, {9 g& ]4 e" e
import java .util.List;+ F" H5 r8 j! ~" U H
import org.springframework.beans.factory.annotation.Autowired;
2 G9 n: D2 ~( [: u( b, l" k import org.springframework.data.mongodb.core.MongoOperations;+ B2 ^5 k2 D+ `3 Y* O7 @
import org.springframework.data.mongodb.core.query.Criteria;
/ ^9 I& G) B) Z0 B0 Z/ x3 ^2 i( A import org.springframework.data.mongodb.core.query.Query;% _; g6 S j w( c# V( m" e* d0 G
import org.springframework.data.mongodb.core.query.Update;1 }5 @# s3 F; d7 J( n) m0 C
import org.springframework.stereotype.Service;
7 j* f- H5 r( J) y) i import com.mongodb.domain.Users;; Y" i; j# M3 Z
@Service(value = "userService")
+ W0 l9 N, A$ J5 S, |) L public class UserServiceImpl implements UserService{ 4 k+ [* _" M, N) T5 z
private static String USER_COLLECTION = "users";
; P7 [9 W/ i+ U5 l+ M @Autowired : D) `- h3 K& f* @& W6 K6 X7 B* z
MongoOperations mongoTemplate;
- K4 P. W: J! d6 X) Z3 C5 { public void saveUser(Users users){ 2 {/ Q8 i/ w: m+ M( M) A2 T) S
mongoTemplate.save(users, USER_COLLECTION);
6 H! w! P& h+ v0 h$ A/ h2 }3 _ } , G7 M6 N2 m* W. R, n' |' d
public Users findUserByName(String name){
8 L1 ^1 B: _6 }8 ?& ` return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION); : G0 B; P# O1 S/ \$ {
}- k" G8 [" M6 O
public void removeUser(String name) {
( q5 l+ O% O( F* S4 \$ d$ H8 q mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);9 j" h$ v4 M8 t# \' |: i9 o+ i. }2 D! | ~0 k
}
; v; @1 y' z* _, N public void updateUser(String name,String key,String value) {8 `' J( X" j$ a E* n, C
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
3 w7 l* _& g" U: B: e 3 l( u; X+ L/ j- |5 x
}
" d& z7 g+ r9 l# a4 [% L, Z public List<Users> listUser() {) n6 G& H+ y) a& O" y
return mongoTemplate.findAll(Users.class);
( O8 I- P! n8 l9 _ }7 |$ A) W4 w( r" F
}4 s& C5 P+ q& J6 Z& F( [
复制代码
/ `) A, @% T& k
- y! a* I0 { r( U9 m 演示地址:http://121.42.26.72:8080/SpringMongod 4 p( A5 h/ a4 ]$ r0 p+ k6 R
: d" Q. w) F6 W2 e6 O/ A 项目下载地址:Spring4 整合MongoDB详细讲解项目源码 ( O+ E: m. d8 l. I; F3 j
# _4 k9 K' x& U7 P
解压码:www.52itstyle.com ; o ~0 B+ A3 z
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :