这两天一直在学习mongodb,由于我的博客网站想把mysql替换成mongodb,为什么会有这样的冲动,我通过收集一些资料,关于mongodb 跟 mysql 的对比...发现性能上mongodb比上mysql是高出很多倍...无论是增,删,修,查的操作.....都比mysql效率好...但是,我也看到,mongodb是文档型数据库...做一些高级查询的时候就可能产生麻烦了..特别是做银行的数据操作时,如果出错..没得事务回滚...这些都是比较严重的问题...
分析过博客网站这整个系统,觉得还是比较适合用mongodb...对于一些表与表的关联查询,我可以用程序上去解决(这部分可能就麻烦了些,不过没关系)...所以我决定使用mongodb
下面:spring MongoDB 集成crud操作(简单封装)
pom.xml
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mkfree</groupId><artifactId>Mongodb</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>1.0.3.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.6</version></dependency></dependencies><repositories><repository><id>spring-release</id><name>Spring Maven Release Repository</name><url>http://repo.springsource.org/libs-release</url></repository></repositories></project>
spring 配置文件:
mongodb.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mongo="http://www.springframework.org/schema/data/mongo"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- Default bean name is 'mongo' --><mongo:mongohost="127.0.0.1"port="27017"/><mongo:db-factorydbname="database"mongo-ref="mongo"/><beanid="mongoTemplate"class="org.springframework.data.mongodb.core.MongoTemplate"><constructor-argname="mongoDbFactory"ref="mongoDbFactory"/><!-- <constructor-arg name="mongoConverter" ref="mappingConverter" /> --></bean></beans>
framework-context.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 扫描注解Bean --><context:component-scanbase-package="com.mkfree.**.service"><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><context:component-scanbase-package="com.mkfree.**.dao"><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>
这样就配置成功了..看下面的java代码
MongodbBaseDao.java
package com.mkfree.framework.common.mongodb;import java.util.List;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;publicabstractclassMongodbBaseDao{/**
* spring mongodb 集成操作类
*/protectedMongoTemplate mongoTemplate;/**
* 通过条件查询实体(集合)
*
* @param query
*/publicListfind(Query query){return mongoTemplate.find(query,this.getEntityClass());}/**
* 通过一定的条件查询一个实体
*
* @param query
* @return
*/public T findOne(Query query){return mongoTemplate.findOne(query,this.getEntityClass());}/**
* 通过条件查询更新数据
*
* @param query
* @param update
* @return
*/publicvoid update(Query query,Update update){
mongoTemplate.upsert(query, update,this.getEntityClass());}/**
* 保存一个对象到mongodb
*
* @param bean
* @return
*/public T save(T bean){
mongoTemplate.save(bean);return bean;}/**
* 通过ID获取记录
*
* @param id
* @return
*/public T get(String id){return mongoTemplate.findById(id,this.getEntityClass());}/**
* 通过ID获取记录,并且指定了集合名(表的意思)
*
* @param id
* @param collectionName
* 集合名
* @return
*/public T get(String id,String collectionName){return mongoTemplate.findById(id,this.getEntityClass(), collectionName);}/**
* 获取需要操作的实体类class
*
* @return
*/protectedabstractClassgetEntityClass();/**
* 注入mongodbTemplate
*
* @param mongoTemplate
*/protectedabstractvoid setMongoTemplate(MongoTemplate mongoTemplate);}
ArticleDao.java
package com.mkfree.blog.dao;importstatic org.springframework.data.mongodb.core.query.Criteria.where;importstatic org.springframework.data.mongodb.core.query.Query.query;importstatic org.springframework.data.mongodb.core.query.Update.update;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.stereotype.Repository;import com.mkfree.blog.domain.Article;import com.mkfree.framework.common.mongodb.MongodbBaseDao;/**
* DAO层操作类
*
* @author oyhk
*
* 2013-1-21下午1:57:14
*/@SuppressWarnings("static-access")@Repository("ArticleDao")publicclassArticleDaoextendsMongodbBaseDao{/**
* 通过条件去查询
*
* @return
*/publicArticle findOne(Map<string, string=""> params){Query query =newQuery();Criteria criteria =newCriteria();
criteria.where("id").is(params.get("id"));
query.addCriteria(criteria);returnsuper.findOne(query);}/**
* 暂时通过ID去修改title
*
* @param id
* @param params
*/publicvoid updateEntity(String id,Map<string, string=""> params){super.updateEntity(query(where("id").is(id)), update("title", params.get("title")));}@Autowired@Qualifier("mongoTemplate")@Overrideprotectedvoid setMongoTemplate(MongoTemplate mongoTemplate){super.mongoTemplate = mongoTemplate;}@OverrideprotectedClassgetEntityClass(){returnArticle.class;}}
Article.java
package com.mkfree.blog.domain;/**
* 博客实体类
*
* @author hk 2012-11-1 下午10:55:38
*/publicclassPosts{privateString id;// 博客IDprivateString title;// 博客标题privateString content;// 博客内容publicString getId(){return id;}publicvoid setId(String id){this.id = id;}publicString getTitle(){return title;}publicvoid setTitle(String title){this.title = title;}publicString getContent(){return content;}publicvoid setContent(String content){this.content = content;}}
ArticleService.java
package com.mkfree.blog.service;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.mkfree.blog.dao.ArticleDao;import com.mkfree.blog.domain.Article;@Service("articleService")publicclassArticleService{@AutowiredprivateArticleDao articleDao;publicArticle findByid(String id){return(Article) articleDao.get(id);}publicArticle findOne(Map<string, string=""> params){returnthis.articleDao.findOne(params);}publicArticle save(Article bean){return(Article) articleDao.save(bean);}/**
* 暂时只是固定去修改,会有下一篇博客,写高级修改...
*/publicvoid update(String id,Map<string, string=""> params){this.articleDao.updateEntity(id, params);}}
ArticleController.java
package com.mkfree.blog.action;import java.util.HashMap;import java.util.Map;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mkfree.blog.domain.Article;import com.mkfree.blog.service.ArticleService;publicclassArticleController{privatestaticApplicationContext app;privatestaticArticleService articleService;@Testpublicvoid save(){Article a =newArticle();
a.setTitle("mongodb开始实战");
a.setContent("mongodb开始实战..内容");
articleService.save(a);System.out.println(a);}@Testpublicvoid findArticle(){Article a = articleService.findByid("50fd0c36bc40ceec1a44308b");System.out.println(a);}@Testpublicvoid update(){Map<string, string=""> params =newHashMap<string, string="">();
params.put("title","修改内容...");
articleService.update("50fe23e6bc402ee4051f90b8", params);}@BeforeClasspublicstaticvoid initSpring(){
app =newClassPathXmlApplicationContext(newString[]{"classpath:spring/framework-context.xml","classpath:spring/mongodb.xml"});
articleService =(ArticleService) app.getBean("articleService");}}
源代码下载:見附件
come from internet
标签:return,mongodb,org,crud,MongoDB,query,spring,import,id From: https://blog.51cto.com/u_2650279/6161807