TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码
( O" R, p* V$ f" h! J; O Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。/ K* g* U& x3 U. t
/ O, b2 m) x1 C2 m3 s& F o
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。, B: U+ W9 F3 O, d2 @
! {/ S9 N; @: Q: e, Q Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。( I4 r4 ]3 E- ?3 i( w9 f) G
$ \" H& N* z k* ^! H! ]0 Y8 k 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6+ P; ]8 U; a1 }6 K$ s
+ o, f; |7 F! z; X
2 w+ t1 ^: ^: [. f, F 项目采用springmvc 结构。: ~- V: ~# m# g5 m. \9 k- M [
所需JAR包:% t' @, V# c8 G& ]
1 z) M# x: }* Q3 @$ q! P7 l* K" v9 f
6 ~' y' ]9 j; O: t4 K
T! p: X! d. g8 a. I" Z2 T applicationContext.xml配置:
a/ _/ u7 w" V- A+ X8 ~ <?xml version="1.0" encoding="UTF-8"?> / E: I. s7 P* c, {$ `- h; `4 o
<beans xmlns="http://www.springframework.org/schema/beans" * p* A! l8 E8 y
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 o6 @1 @- w. b" g1 ]* X4 a% b xmlns:p="http://www.springframework.org/schema/p"
! `' |3 [: O$ L9 s. b* d2 n xmlns:context="http://www.springframework.org/schema/context" 8 q2 m3 u8 ]( m: m* b5 B% ^* H8 X
xmlns:mvc="http://www.springframework.org/schema/mvc"
1 f8 r5 M* h! v# w2 Z: _9 R xsi:schemaLocation="http://www.springframework.org/schema/mvc 5 g- h/ C: [& H6 q" z' A" H
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd + T; V2 _3 v4 }, V
http://www.springframework.org/schema/beans
3 |$ r0 r: v7 f. O9 V; R2 S http://www.springframework.org/schema/beans/spring-beans-3.0.xsd $ l7 W9 b) e3 o8 q/ i ], R
http://www.springframework.org/schema/context . a7 B- p! B6 ]1 _, H5 x
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> * P$ Q1 O( } `9 c2 ~$ r
<context:component-scan base-package="com.mongodb.service" />. H: I* }* s) o! {
<import resource="mongo-config.xml"/>
1 o. L) c: m2 J2 ^" ? </beans> 复制代码 mongo-config.xml配置:* y5 J" y$ c2 w+ q" e& j& C( \8 @' y
<?xml version="1.0" encoding="UTF-8"?>
" n* O5 N1 Z7 a0 @3 a <beans xmlns="http://www.springframework.org/schema/beans"
4 L* r7 P8 X4 _8 S* U0 |! m xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
! d- M9 H6 J/ T8 B9 v4 B s( { xmlns:p="http://www.springframework.org/schema/p"
3 \6 E. O+ U+ A0 `0 h xmlns:mongo="http://www.springframework.org/schema/data/mongo" , Y: ^. M) ]7 `
xsi:schemaLocation="http://www.springframework.org/schema/beans
! n( O$ n# A( P6 X http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
' k- z. P, W2 I$ W1 G. l, n/ F/ J http://www.springframework.org/schema/data/mongo D `4 D r$ B
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd* n( ^. a6 n- L
http://www.springframework.org/schema/data/repository0 O1 U. ?3 p3 k: e) s% Q5 S% ?
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> * T% r% e* |( K( F( P3 N& l" g
: S! P6 s! J _- [ <mongo:mongo host="121.42.26.72" port="12345"/> ( D7 e }" ^! _; L5 e" m x
# U7 L8 l0 H. W9 Z8 G
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> 7 R& _& B7 ? @4 V
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> : q# S6 C0 K# p( u8 K% i
<constructor-arg ref="mongo"/>
, M8 K, z, p" u5 T <constructor-arg name="databaseName" value="itstyle"/> 1 J/ Q/ A( R E9 `: G& d
</bean>
G1 I3 ^/ E4 ]4 a! G l8 T7 ^+ S 2 d: M4 e# c7 W3 w
</beans> 复制代码
+ |8 \% A7 s' D& T2 b spring-servlet.xml配置:
3 X; W+ @4 b) C' ?' ]8 Z
* K- i7 n6 Y6 R& v <?xml version="1.0" encoding="UTF-8"?> 9 b/ g2 E/ T) C
<beans xmlns="http://www.springframework.org/schema/beans" ) T* L0 R& X x; ^/ `4 D
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
$ V2 y- _0 _* j# s6 r# Z xmlns:p="http://www.springframework.org/schema/p"
/ E9 q6 Q* ^! v4 a xmlns:mvc="http://www.springframework.org/schema/mvc" ( X0 [; j8 n0 B
xmlns:context="http://www.springframework.org/schema/context"
- @$ u' J1 u4 t: f, ] xmlns:mongo="http://www.springframework.org/schema/data/mongo" . H8 e! H0 W, ~, z, ~% x) Y
xsi:schemaLocation="http://www.springframework.org/schema/beans
. O8 u7 v" k! t3 V% J" m/ @6 ] http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # u. ~) W+ a& s+ j
http://www.springframework.org/schema/context
( L& {0 G w, h4 ^5 b http://www.springframework.org/schema/context/spring-context-3.0.xsd
7 d! n/ W! ^0 D& W http://www.springframework.org/schema/data/mongo
6 n# b* I, O# B2 q* H http://www.springframework.org/schema/data/mongo/spring-mongo.xsd' k" y0 N4 v9 g' h" E$ P( T
http://www.springframework.org/schema/mvc - I# x# v$ j% S* p* M! Z
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">) L6 K" T* p0 o
4 S/ t% } ?# k. X; Z2 {: q% 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-->
) a6 H9 r! @+ F; E: @) t2 `/ f <context:component-scan base-package="com.mongodb.controller" /> , O% J) N+ D% X' g
0 @- N; A: T; ~- v
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
& f' Z! m, o6 }3 S) H. ]% A: q: | <mvc:annotation-driven />
9 T% H! P! @3 X! c * f+ I0 v+ s4 g& o; V6 S- a9 F
<!-- Declare a view resolver --> & f R0 a+ y8 S
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
% V$ d3 i+ |0 \0 r& U p:prefix="/users/" p:suffix=".jsp" /> $ j5 X H1 r( J
. [; ~3 o- ~' G' O- v
. O3 O* \: Y( j5 P& o, I
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
) E( }5 y- ~$ x7 C$ A import java .util.List;
5 ]% Q& \9 }0 B- l$ d import org.springframework.beans.factory.annotation.Autowired;
+ r: N5 t, K: m import org.springframework.data.mongodb.core.MongoOperations;, D% t( ^" c: Y0 u$ s0 y
import org.springframework.data.mongodb.core.query.Criteria;4 ^7 z: @+ h0 @. g$ z, u
import org.springframework.data.mongodb.core.query.Query;, [( Y( A; N2 @ b5 W
import org.springframework.data.mongodb.core.query.Update;
9 D. u5 L# j$ b M8 W import org.springframework.stereotype.Service;' b4 s# d# M& d4 b* w5 ]
import com.mongodb.domain.Users;% [- S' w* \2 I
@Service(value = "userService")
' s/ a; J, s8 y! z0 s, Z* A7 v public class UserServiceImpl implements UserService{ ) ]8 _4 o i, J, |' j
private static String USER_COLLECTION = "users"; # A8 S n5 `! ?/ N4 q7 ]5 m0 X
@Autowired 0 ^1 i% A; b/ J9 _% z9 u: R+ T7 }& c
MongoOperations mongoTemplate;
2 k' A R) c7 Q8 M" g P public void saveUser(Users users){
- ^/ H9 b: h5 | mongoTemplate.save(users, USER_COLLECTION); 8 h# ^) U/ G( Z- l$ l
} % t- ]5 v: V+ T4 E8 Z
public Users findUserByName(String name){
3 `$ s! n [/ R* C' g6 k return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
/ }; U. x& h+ V" c1 m8 C/ V% ? }
0 @; t' d9 m2 a( [' n# @ public void removeUser(String name) {8 b4 \9 N5 v1 Z5 q
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
1 z6 S, E; K4 V* q2 f } @, \; A7 n8 N3 q7 R
public void updateUser(String name,String key,String value) {
2 ]! k6 G6 k9 _0 M7 d' f; r mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
8 E1 h( E' c0 e0 T" }# c7 r# e N7 m! q; V! D6 b' l
}* e, s* Z$ A" h4 g y2 I- C |3 c: h0 `
public List<Users> listUser() {# k1 c. _+ U( @9 }2 G8 ?' k
return mongoTemplate.findAll(Users.class);* C+ n+ |( P5 t. C
}* B! j$ z* V2 R' h
}8 P; S Y, _) ^ [5 d& I: X
复制代码 . } k; Z# {; N0 \* y+ f! e4 @2 v3 D
% ]" m& g& X4 y/ x' P
演示地址:http://121.42.26.72:8080/SpringMongod # u, J) X# r: K) q# ^2 e
2 G' B+ Y+ M: a `$ D
项目下载地址:Spring4 整合MongoDB详细讲解项目源码 9 Y3 m2 }3 m0 J z& L6 X n$ e
7 E+ |7 |5 _9 y5 i+ U 解压码:www.52itstyle.com
/ t3 {+ `0 O: ]9 J7 i3 D2 s
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :