TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring 4 整合MongoDB详细讲解项目源码 . B+ s" z/ S7 l
Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。
' s1 A, J. W, j9 Y
" r! j( s c. L( z( F9 |5 i, A NoSQL数据库与传统的关系型数据库相比,它具有操作简单、完全免费、源码公开、随时下载等特点,并可以用于各种商业目的。这使NoSQL产品广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。; {$ ?' L7 c3 \2 R x' @
1 q& \5 h- ]4 ?
Mongo DB 数据库自行搭建、也可以暂用地址见mongo-config.xml配置 可以进行增删查改。
( S, i% d! R: Z, z' X! s + B; O2 {' M' o/ A; x4 Z0 |7 `' B
环境配置: myeclipse 8.6 、mongodb 3.0 、spring 4.0.6 S$ u3 w+ n( ]3 _% Y
3 G0 W; m9 }7 z! z
3 h3 M: q, ~" N( r. L f
项目采用springmvc 结构。+ p5 n) g/ @8 i+ p* t" M! a
所需JAR包:2 q& K( O5 V) x$ w) ^% s
0 E; `- g9 l4 N k
( o) L( G: a/ p& Z" x
$ d; P6 |' e; t- E* T8 W' a applicationContext.xml配置:
3 J+ Y' G, X. q0 F7 k <?xml version="1.0" encoding="UTF-8"?> ' ^7 ?0 w( ]0 f E3 d
<beans xmlns="http://www.springframework.org/schema/beans"
9 e8 `: B* M! L' Q) h+ } xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
$ k f5 W6 Y9 Q) i& u+ X. |) N8 _ xmlns:p="http://www.springframework.org/schema/p" ( `1 `. S5 x. ~6 b$ o: Y( C
xmlns:context="http://www.springframework.org/schema/context" 2 h2 s' n* G- i& O/ \; @7 D
xmlns:mvc="http://www.springframework.org/schema/mvc"
# ]% e7 c) T3 J' l/ B$ N3 S xsi:schemaLocation="http://www.springframework.org/schema/mvc
; T. I8 n$ `% \* T http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd : H! l ~2 h& I
http://www.springframework.org/schema/beans 7 a9 S* V! o3 D# H& c% n
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
T. v; c1 F& Z http://www.springframework.org/schema/context
8 ^4 R7 d1 ~% T! B! b6 z5 q4 `2 t/ O http://www.springframework.org/schema/context/spring-context-3.0.xsd">
) U/ `/ P% v6 R$ z/ s7 ]* q3 @/ O( k3 j! Z <context:component-scan base-package="com.mongodb.service" />. U- l6 ~5 `$ E& W7 y
<import resource="mongo-config.xml"/>
7 r% B; |5 Z) d7 d* c+ w. r </beans> 复制代码 mongo-config.xml配置:' L# C( t) h" V+ d) U9 Z3 [# u
<?xml version="1.0" encoding="UTF-8"?>
9 x2 O4 l& [; Z( {1 [! F <beans xmlns="http://www.springframework.org/schema/beans"
+ F5 D6 W$ [3 R& f; P: m$ R& } xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " k+ V0 k) v5 d/ N# [
xmlns:p="http://www.springframework.org/schema/p" : b+ \6 \$ N/ T7 u5 T& |
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
& R! J* B9 E- ] xsi:schemaLocation="http://www.springframework.org/schema/beans
4 B; J$ [- B4 v+ S1 w5 H http://www.springframework.org/schema/beans/spring-beans-3.0.xsd $ T) B0 l8 g* T% g
http://www.springframework.org/schema/data/mongo 4 b. C, n% q" D# L9 _
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd- l% z# L& p, P6 U. |
http://www.springframework.org/schema/data/repository
% A2 g( `, S5 F7 j* D http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd"> 0 i4 |+ m6 H+ {0 h
' n* F2 h" s9 E* c" W7 Y1 @" V0 \) H <mongo:mongo host="121.42.26.72" port="12345"/>
) U w9 |. O& T% m; V( B5 Z5 c6 k ' o& o3 v1 p. s
<!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
1 i4 }2 q, z" @5 w7 ] <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> - b T4 d* \" B- p% o9 o
<constructor-arg ref="mongo"/>
! A( E" I, o+ {: ` B s( Q <constructor-arg name="databaseName" value="itstyle"/> + m& G) u( Y9 E! ^0 O M0 [$ Y5 v
</bean>
* d/ u; f) U( |9 S- G" f- J
, b: F4 Y) y2 O" w6 K3 \ </beans> 复制代码
' ^5 V6 W; Y' [1 { spring-servlet.xml配置:
% G/ h0 L! X( ~! b5 B
! d6 ~5 F' r. t5 i3 h3 L& q% ?& _ <?xml version="1.0" encoding="UTF-8"?>
4 C0 `) p. l* Z6 R3 z% y: l& B <beans xmlns="http://www.springframework.org/schema/beans" 1 K, q9 K( ]9 S# Y3 U* x
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 x, }/ u9 r$ ~- \: ?1 \
xmlns:p="http://www.springframework.org/schema/p" ! Q4 a# U) g" R& C
xmlns:mvc="http://www.springframework.org/schema/mvc" # A( c9 C% O Y) q
xmlns:context="http://www.springframework.org/schema/context" % \5 E" l/ r' e" J' F/ ^( `
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
+ A1 C! b9 Z* V2 m6 N" f8 z xsi:schemaLocation="http://www.springframework.org/schema/beans
) H! \. G& y+ e http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 w# c) x& k9 L. N! r& B- B
http://www.springframework.org/schema/context % ~- Z$ @8 K4 c8 \5 z1 E. r4 ^
http://www.springframework.org/schema/context/spring-context-3.0.xsd
0 c' U U( ~ L0 T+ I1 v* w http://www.springframework.org/schema/data/mongo
+ A8 b! H( O+ U- Y" x1 O2 M http://www.springframework.org/schema/data/mongo/spring-mongo.xsd. n! V( H1 |1 ]) A6 o6 F6 L5 K
http://www.springframework.org/schema/mvc
, m0 V3 g% c& c- s* A* M3 C/ R http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">2 x" e7 Q/ q* m
0 L/ f) ^9 @4 q2 [$ \
<!-- 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-->
0 t! g+ b/ ]& V& P% i& T; C <context:component-scan base-package="com.mongodb.controller" /> & O) T$ M# J; f! _/ C: M/ c- r
# R( a* R" [7 ?* q5 u* R. x <!-- Configures the annotation-driven Spring MVC Controller programming model.Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
8 [5 ^6 l. d% B& r C2 V; s <mvc:annotation-driven /> : p0 p+ ^/ c$ ^7 \+ G P7 V( L
$ d; i4 J2 ], [5 t <!-- Declare a view resolver -->
/ C+ B! g, M- \, r <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" " p U: U6 M: W1 K6 |; j( L+ S
p:prefix="/users/" p:suffix=".jsp" />
3 t3 B, u* |3 @ B8 B# k% E$ y7 \
/ A. p1 A' u# k 6 m7 n" C0 M3 F- G f9 x
</beans> 复制代码 UserServiceImpl 基本实现代码:package com.mongodb.service;& l7 r% c$ f" I9 H" F* C6 g# `8 O
import java .util.List;
9 Y' Y3 i9 U7 T0 e import org.springframework.beans.factory.annotation.Autowired;
5 k& M9 _/ ~: f; g import org.springframework.data.mongodb.core.MongoOperations;
! N: c3 T4 Q F8 C( p import org.springframework.data.mongodb.core.query.Criteria;
+ |4 N, a7 Z6 u5 f; o& @" { import org.springframework.data.mongodb.core.query.Query;
" f1 h6 Z9 g. P0 U r* ? import org.springframework.data.mongodb.core.query.Update;! ?( I2 v5 o' f1 n& i) C7 a
import org.springframework.stereotype.Service;
9 `. v$ x! B Z import com.mongodb.domain.Users;
, F/ g$ m1 o' F7 d+ S @Service(value = "userService")
0 l3 d1 L* l' `, y% B public class UserServiceImpl implements UserService{
z7 w3 X# |' Z) j% U private static String USER_COLLECTION = "users";
6 m* F- \, K3 U, X% S4 X; j5 m2 S @Autowired + F+ D+ R' E+ b) w1 w
MongoOperations mongoTemplate;
- O1 b3 }2 m' E4 `4 g, G1 N0 z public void saveUser(Users users){
$ ^; x2 B! A ^: X mongoTemplate.save(users, USER_COLLECTION);
4 \4 ]( R. e1 t } 3 ]& w! a* E5 \# Z" g0 x
public Users findUserByName(String name){
; c% D4 i7 R o6 j0 |% _ return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Users.class, USER_COLLECTION);
$ y3 t$ y' Z( g2 L+ A. ?. @ }" F8 u' b. V2 D* T2 w: }) ~
public void removeUser(String name) {
" \4 M# p0 z ` mongoTemplate.remove(new Query(Criteria.where("name").is(name)),Users.class,USER_COLLECTION);
B4 @2 ?( ~' D, H4 x }
9 e) E% W% U/ m) K. m- E public void updateUser(String name,String key,String value) {
( ^) W- f8 j- z: b/ L mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)), Update.update(key, value), Users.class);
. A) Z; U! n4 \ S2 A8 T: ~ / ~6 }6 t; J, {( k$ {1 G3 O
}, j3 T% ]* P& f. j0 |: c
public List<Users> listUser() {
+ |% ~! z# [6 X* M! m" F return mongoTemplate.findAll(Users.class);
, `' h' c5 c( p7 _$ W$ L4 h }
9 ~; ^/ x5 Y$ |2 \. Z& ?4 n: w) j }% V& Q) F( b' F' s; {; X/ s
复制代码
- [- i* x2 i# _0 d
8 q3 j' N1 S q; ?% c" e 演示地址:http://121.42.26.72:8080/SpringMongod
1 F7 s4 j6 c" p# H
5 `' ^4 b$ U" S: B" u9 W 项目下载地址:Spring4 整合MongoDB详细讲解项目源码
9 O+ i: L8 X- y" ], @5 ]% f1 {( i
2 {4 l2 f [& W7 a& U" d- M9 P 解压码:www.52itstyle.com 8 I. q N; ~' G, w4 R
科帮网-Java论坛、Java社区、JavaWeb毕业设计 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网-Java论坛、Java社区、JavaWeb毕业设计 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网-Java论坛、Java社区、JavaWeb毕业设计 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网-Java论坛、Java社区、JavaWeb毕业设计 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :