TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 7 C( I. Y& Z" l" O
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。7 m* e( D' Q, }- C1 @8 Y0 e
, E5 {/ q# v3 }/ A M" M
NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。9 C: L# C4 `9 q5 R8 ~
\9 i2 Z9 _; p: e" R3 x1 v& g
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。, u0 b+ P/ M5 v
' p7 l0 l0 U, h% G( G, V 环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6
6 W- ]1 l5 ?5 N 8 s1 f e; j3 R
% T. V" t6 P9 S- x
项目采用springmvc 结构。
# H' ?- F5 _8 w" l, |- K 所需JAR包:3 q/ K! w$ I% \0 Y
# a! V% f9 X, g2 ^8 O0 t
2 ?, }! C( E! D & o5 |# o: t: N9 o) h: [& X' q
applicationContext.xml配置:/ q/ p7 ~% }! A+ S+ P
<?xml version="1.0" encoding="UTF-8"?>
3 [! T7 k2 T# [2 W <beans xmlns="http://www.springframework.org/schema/beans"
Y; r2 W2 w, h2 Q7 Z4 m4 o; U7 F xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" : ^5 y) I8 Y! @
xmlns:p="http://www.springframework.org/schema/p" 2 ~( X5 U, }/ V' n& a6 |. l( Q
xmlns:context="http://www.springframework.org/schema/context" $ [" B: L1 H) y5 i3 r
xmlns:mvc="http://www.springframework.org/schema/mvc" 9 G# {; h k7 o% \7 m: T- e# a
xsi:schemaLocation="http://www.springframework.org/schema/mvc
; B3 k/ ^) ] _5 Z" W b! c http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
1 S1 C! u2 M, H: }, b http://www.springframework.org/schema/beans ; R6 N* Q8 r( u; x, \$ f
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd & D2 b2 ^- {% `9 ?( n$ c- D6 P
http://www.springframework.org/schema/context
2 p; h: N Y5 O* j http://www.springframework.org/schema/context/spring-context-3.0.xsd"> ' _; F7 ]( L; _$ ^
<context:component-scan base-package="com.mongodb.service" />
. N& \- L# v! N* x% |" b% @/ h <import resource="mongo-config.xml"/>
2 ?, [5 |; l. }* L# X </beans> 复制代码 mongo-config.xml配置:
2 j3 N- x! ?# S: o <?xml version="1.0" encoding="UTF-8"?> ' n( M' u; l$ ]
<beans xmlns="http://www.springframework.org/schema/beans" 5 k5 j O+ J( Q; Z9 @2 O
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
" {7 G: A" t. C' v( x xmlns:p="http://www.springframework.org/schema/p" 6 L4 v+ N5 j* ^' x( H9 P% j) d* S: v5 B
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5 \0 t# R( q) k: S* S7 e) ?: g xsi:schemaLocation="http://www.springframework.org/schema/beans 8 S; B. N( M, ^0 w% W3 S
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
! x, P( e4 A" [! J1 }. J http://www.springframework.org/schema/data/mongo
' p% s; Q# S( r; k http://www.springframework.org/schema/data/mongo/spring-mongo.xsd* n- Z2 ]% q5 I' y
http://www.springframework.org/schema/data/repository3 W5 } k. s6 W1 }
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> : n; O% I {4 A* q; B
. B& u" b5 p; N$ t7 T4 R" | <mongo:mongo host="121.42.26.72" port="12345"/> - B, v- r5 e# K/ F
, i; ?/ I5 J Q% Z: T( @7 R
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
; l' k( ]% R) `* {- N: S <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
: M9 q6 m$ Z/ n6 f8 P <constructor-arg ref="mongo"/>
% i2 Y6 L$ m: \' _2 M/ M$ J# J <constructor-arg name="databaseName" value="itstyle"/> 1 l* B4 M% A: A8 k/ {3 q* S
</bean>
7 a$ e# Z9 N0 ]4 B: q; ~
+ g: e, O* I) E. D' y" y </beans> 复制代码 # [: H z! b; O& v5 g6 d
spring-servlet.xml配置:
+ u: i- |5 I3 m& s v3 Y # Z; g# y. v7 Y8 N: l2 o( e
<?xml version="1.0" encoding="UTF-8"?> + d. E/ m* T p3 ?2 w* p9 X _
<beans xmlns="http://www.springframework.org/schema/beans"
+ g K6 }# K* p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + \- Q; f2 V5 y$ k1 _) L' t
xmlns:p="http://www.springframework.org/schema/p"
. {( j' L6 {9 H" Q& ]7 h, J xmlns:mvc="http://www.springframework.org/schema/mvc"
1 ~/ w* y1 T+ U/ i xmlns:context="http://www.springframework.org/schema/context"
! S" Q% Y' O- Y* ]2 B xmlns:mongo="http://www.springframework.org/schema/data/mongo" 7 W% }& c y. T+ k
xsi:schemaLocation="http://www.springframework.org/schema/beans
! j2 v8 b: T' \' f http://www.springframework.org/schema/beans/spring-beans-3.0.xsd D) |. G' }1 w2 x) J/ s3 Q0 H* |
http://www.springframework.org/schema/context
1 i+ f6 S* t# _! {2 \$ i8 P http://www.springframework.org/schema/context/spring-context-3.0.xsd
- y7 x; |8 V) F* F; }' K# S6 H http://www.springframework.org/schema/data/mongo 4 q2 |: }! `0 d# Y
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd1 m. D) K$ z P7 t
http://www.springframework.org/schema/mvc 5 E. ^( G- U- ^; Q& [* W0 U
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">0 t/ F' b) T+ x! Q1 S5 Y' g
6 \, R( k( n2 z( q
<!-- 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-->
; K" o* i) P3 z w <context:component-scan base-package="com.mongodb.controller" />
2 ]0 C+ X' J8 K" Q$ n ; a N4 J9 q7 O ] u! G
<!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
! c3 H- _. w4 H <mvc:annotation-driven /> % \$ V& L6 M' v2 b$ L' Z
u4 X3 E' W4 Y6 K: m
<!-- Declare a view resolver --> 4 M8 J5 g m G' |8 w
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
3 h* L- a! w+ N p:prefix="/users/" p:suffix=".jsp" />
+ L9 M' g6 S J1 x6 q. p: ]" G" U
6 Z- u Q2 _( L% l& g! N, ]0 R3 L; ` # y3 H% }/ {: e9 l: C
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;
% \1 v3 [4 h/ I/ k import java .util.List;. b. ~- z* n6 y0 q/ O6 R
import org.springframework.beans.factory.annotation.Autowired;7 u- @9 d2 s" x' O( o
import org.springframework.data.mongodb.core.MongoOperations;
! H# z3 I/ b( S$ H4 \1 k/ U import org.springframework.data.mongodb.core.query.Criteria;
' t8 c" Z* l4 v N. ]: `. J7 a3 ` import org.springframework.data.mongodb.core.query.Query;9 |8 ~. `1 p) K/ J6 k6 Z1 t
import org.springframework.data.mongodb.core.query.Update;" c8 Q5 i" |7 d
import org.springframework.stereotype.Service;
1 G5 B% f% D( H5 H, Z" q import com.mongodb.domain.Users;" h4 u+ h9 M/ m
@Service(value = "userService")
' a2 j, \2 {$ b o1 F public class UserServiceImpl implements UserService{ 4 B& G1 r' ^& J0 O
private static String USER_COLLECTION = "users";
' n- F( i! d7 b/ P @Autowired
$ m2 I' }+ ]6 E1 X% V MongoOperations mongoTemplate; 2 b! s1 s8 Q ^; I7 N5 x
public void saveUser(Users users){ & u, q0 P) T; ]( I+ @
mongoTemplate.save(users, USER_COLLECTION);
, a4 f& S, j8 b, t4 f+ d' A8 V }
4 e+ X/ ?( ]: U, l) K9 H$ ` public Users findUserByName(String name){0 q$ J& u' @4 M) S! R$ L, H
return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
' M j( X) \, G9 ?9 A+ ] }/ L' F7 [: X& J3 a
public void removeUser(String name) {
! l P8 F$ m. K' [9 T0 M mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);; H- R; Y' e2 V! p# G2 c! e
}6 G0 s/ V/ V) c' L7 o
public void updateUser(String name,String key,String value) {
' E& A' N6 @+ I0 k0 d mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
8 v1 C2 G8 ]/ y e+ ] a" G
) L: `' W% \5 u }
1 l4 w7 h1 ^1 i- F' h public List<Users> listUser() {
+ h1 o' y3 k* ~/ C' s6 u6 N return mongoTemplate.findAll(Users.class);
# s' [0 D0 }: f# L7 o7 d }* }2 f7 O& R" D( q1 u5 C
}* L6 k0 h# ~) X( ^
复制代码 + ~9 N' F; _) q; f0 l
& d& t; d/ K( p" r 演示地址:http://121.42.26.72:8080/SpringMongod & w6 G5 T5 _ J) I3 O- M
1 K) X* [1 H$ B5 U o& H% T+ v 项目下载地址:Spring4 整合MongoDB详细讲解项目源码
# T4 u9 P! d3 M V7 f4 a 4 T2 w, r* i0 {2 }2 B$ c. i
解压码:www.52itstyle.com
; d. p9 i4 J- n: A+ W5 [, ^; s: j
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :