TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 . z, }& X( b$ H- c3 v
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。( N9 D( H- p3 }0 \
. {7 S+ v5 n& @( Y6 R5 h- g+ p
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。) t! q; P4 Z4 l' ^3 R
$ L. _% Y' b; Q: A( K
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。2 _' D; d, j% N
; }+ M6 b7 m6 |2 `& c* W% y 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
- e) m# d' i9 f
/ w3 X g. j! V2 Y: i ; S9 B3 ?+ p- {6 q2 j0 P
项目采用springmvc 结构。
8 v5 ^/ L2 ?' w 所需JAR包:' z( U$ N6 W. l: a# S1 [" T( J8 S
/ o6 Q0 F% n: l
! X* L% [: {4 M% O) a% ]+ Z
2 F- e- z, i& i; F$ e7 h
applicationContext.xml配置:& V6 T# {% `# v
<?xml version="1.0" encoding="UTF-8"?> 4 m1 `! F9 l4 u) r: y" Z. @
<beans xmlns="http://www.springframework.org/schema/beans"
1 |1 U8 Y0 _- x( `+ o% p% Q xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 L2 `- d: m& ]2 E
xmlns:p="http://www.springframework.org/schema/p" 7 M& k: n/ d( M! {
xmlns:context="http://www.springframework.org/schema/context"
" ~1 e2 n/ i, T! `) A xmlns:mvc="http://www.springframework.org/schema/mvc" . t2 Q% R- z( g4 t4 H+ F+ d
xsi:schemaLocation="http://www.springframework.org/schema/mvc
3 p- }, H& q! r& P1 j) f; ` http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 8 D; l! P& o W0 k6 s4 Q
http://www.springframework.org/schema/beans # s' J* \# ~3 f
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd % j. i' l- @/ R" y' E! x, @
http://www.springframework.org/schema/context
( ~" K; x- ?1 }; r" ? http://www.springframework.org/schema/context/spring-context-3.0.xsd">
$ G; w- h0 l" A3 k+ u <context:component-scan base-package="com.mongodb.service" />+ w( l7 `8 a% E% O U, e3 l2 f
<import resource="mongo-config.xml"/>* i. o% u5 H" w/ N- o8 i# M
</beans> 复制代码 mongo-config.xml配置:
* Y) y) }2 i3 w! g/ `6 k7 ? <?xml version="1.0" encoding="UTF-8"?> 0 \' n" k/ ?7 d" u" Q9 b7 [0 I
<beans xmlns="http://www.springframework.org/schema/beans" . g8 o1 M0 U* w6 i# |( o
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 e" {& T. b# P4 D
xmlns:p="http://www.springframework.org/schema/p"
/ c4 l `7 L0 t" M S9 _6 i3 G. ^; k xmlns:mongo="http://www.springframework.org/schema/data/mongo"
) Z5 ~' A5 k$ B9 n/ }' B3 J- T xsi:schemaLocation="http://www.springframework.org/schema/beans & @4 |$ A2 e" F3 [) L `# q
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 i' a) U/ i; z8 m6 \6 z U http://www.springframework.org/schema/data/mongo
% G6 r8 s; E* F3 y5 V, O. E0 Z http://www.springframework.org/schema/data/mongo/spring-mongo.xsd- ~) ?) C$ r: B5 x2 m
http://www.springframework.org/schema/data/repository% k# g* F& t2 G1 J
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
5 ^* h Q; {2 f8 x
+ m; j! y0 [5 R <mongo:mongo host="121.42.26.72" port="12345"/>
9 j$ c0 c* g) m' X4 @: f& o4 \ , G# `- z6 T9 m! V/ P8 ^7 r+ ]5 g. K
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
4 ?& i! X7 a# v A$ q <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
! `1 H) P& g* a$ [) C' J <constructor-arg ref="mongo"/>
2 p; b6 X" n8 I8 w; \ <constructor-arg name="databaseName" value="itstyle"/>
7 b( } ]- t9 ]$ v& |& A' ` </bean>
* H3 g. [6 N) E |# F 1 ~0 i) p& Z$ k+ {
</beans> 复制代码
/ }3 C2 v! Z% h- j$ g y2 o spring-servlet.xml配置:
6 v7 c; @+ B, Z* S " N8 k# G; r! l$ }6 U* s; q
<?xml version="1.0" encoding="UTF-8"?>
% X* A Z9 K2 D% x3 R4 `" Q7 [. | <beans xmlns="http://www.springframework.org/schema/beans"
' L8 j' a( l% [. Q7 P xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 m k" V! ?( F; f6 [6 P7 B& ^ xmlns:p="http://www.springframework.org/schema/p"
2 _; t/ U) [' @; u( { xmlns:mvc="http://www.springframework.org/schema/mvc" ) @; s1 b6 I" J, f
xmlns:context="http://www.springframework.org/schema/context"
; p2 X( e2 D" E5 @; H( I xmlns:mongo="http://www.springframework.org/schema/data/mongo" 7 X# n" G9 z) |4 |
xsi:schemaLocation="http://www.springframework.org/schema/beans 9 Q7 \7 J* P& M5 e
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
, X! T/ q2 B+ ]' `5 k http://www.springframework.org/schema/context
9 x* I% |( Z& a, K x http://www.springframework.org/schema/context/spring-context-3.0.xsd
2 d& _3 [0 B3 @+ L5 b& Z" p http://www.springframework.org/schema/data/mongo
& U8 Q' z' L, \ {" q9 N http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
" k8 S* ?. U4 ]" j, l6 ]( x' W- H http://www.springframework.org/schema/mvc
7 M4 ]5 u0 M4 @6 ?8 R http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">2 A) v1 K' K! z) ~" R1 a/ N* u" F* V
3 C: w* j2 `2 a5 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--> . v2 V7 K0 {7 _8 u
<context:component-scan base-package="com.mongodb.controller" />
6 d6 k7 V( c# a; `% }4 | , _; s4 G- [; S
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
# t( ?. Z: m7 I3 i3 S1 i <mvc:annotation-driven />
, C' Z% }. E, @) q9 J6 q + z9 p0 g* w B' p1 \$ q) N7 ?0 V$ e+ N3 R
<!-- Declare a view resolver --> $ @- u' Q# ~. P- Q7 J, u
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
' |( b- L% e3 l! p0 B p:prefix="/users/" p:suffix=".jsp" /> " v: p& U! x8 \
6 U4 }9 E* U+ m3 r) Q / X, i7 S% r, [) ~
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;4 U8 [# C1 Y0 s4 b$ I) ]
import java .util.List;
5 K7 r! r J B U# X3 W0 T& G import org.springframework.beans.factory.annotation.Autowired;7 h1 v2 l, j; H0 h A' _/ k7 d+ f
import org.springframework.data.mongodb.core.MongoOperations;+ A* Q& A% y- B) D1 ?: b
import org.springframework.data.mongodb.core.query.Criteria;8 J, L" x5 b& A4 d
import org.springframework.data.mongodb.core.query.Query;
. u* ?. f; Z- H | import org.springframework.data.mongodb.core.query.Update;- E9 a) |2 M+ U2 ~$ x& ^$ M
import org.springframework.stereotype.Service;
! M6 X. p) ^ v) {6 s1 m import com.mongodb.domain.Users;
. g5 g" }4 p( ^" O/ @ @Service(value = "userService") # B7 v1 L" F% r7 @, k7 `+ @ c
public class UserServiceImpl implements UserService{ 3 o6 \% W, L) D0 W+ Q0 U" C( ~6 s0 L
private static String USER_COLLECTION = "users"; # q4 ~/ } l. E! n
@Autowired
; c( B0 ^, s1 C" ~' J% t; }6 u MongoOperations mongoTemplate; + Q9 B1 k+ H- }" V
public void saveUser(Users users){
# ?; ?/ |' w5 a/ K( ? mongoTemplate.save(users, USER_COLLECTION); 5 o% J, U/ @7 T% d
} 2 @6 f. q: [/ ~' c/ n
public Users findUserByName(String name){! ~+ A8 |5 Y5 J8 v! j
return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
6 q( u# F3 h6 u7 @ }# s3 z6 Y. h. U9 P! t
public void removeUser(String name) {7 Q9 v5 Q5 |9 x
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
6 f& A) n- S5 c/ @ }. Y" k1 i ^* a
public void updateUser(String name,String key,String value) {6 w$ {1 ?( U( y% P8 N: e- z* c# H$ p [
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
$ p; Q1 m/ V3 y2 {
& r! `' T. h! a } }0 } @1 ?) X5 Q3 Q( Z
public List<Users> listUser() {+ J1 [2 `6 @) C! d" k5 q% k& u6 M
return mongoTemplate.findAll(Users.class);3 }4 ]8 d! H5 Y' }5 |
} E% q0 \$ ]# }) o
}. v. x6 O5 B5 i- p, S
复制代码
, r/ z: z r! M# m 8 K5 z3 g- _/ W: W
演示地址:http://121.42.26.72:8080/SpringMongod ! \- N. ]7 F, {
0 f3 j# u* D2 x: T# w& X0 G- h
项目下载地址:Spring4 整合MongoDB详细讲解项目源码
0 X$ K. W9 H* `) }
# f6 H( K5 {4 O* c7 p, c 解压码:www.52itstyle.com
; E) X. E4 u, @: g# ~
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :