千家信息网

Spring整合MongoDB

发表于:2025-01-28 作者:千家信息网编辑
千家信息网最后更新 2025年01月28日,版本:spring:4.1.3.RELEASEmongo-java-driver.version:3.4.1spring-data-mongodb.version:1.7.0.RELEASE配置:1、
千家信息网最后更新 2025年01月28日Spring整合MongoDB

版本:

spring:4.1.3.RELEASE

mongo-java-driver.version:3.4.1

spring-data-mongodb.version:1.7.0.RELEASE


配置:

1、pom.xml中添加核心依赖(两个)

+++++++++++++++++++++++++++++++++++++++++++++++++++++

                                                        org.mongodb                        mongo-java-driver                        3.4.1                                                                        org.springframework.data                        spring-data-mongodb                        1.7.0.RELEASE                

++++++++++++++++++++++++++++++++++++++++++++++++++++++

完整pom.xml文件内容如下:

        4.0.0        com.fwmagic.mongo        fwmagic-mongo        1.0.0-SNAPSHOT                        4.12                4.1.3.RELEASE                1.2.15                1.6.4                2.5                3.3.2                1.3.2                3.4.1                1.7.0.RELEASE                dev                UTF-8                UTF-8                                                                        junit                        junit                        ${junit.version}                                                                        com.alibaba                        fastjson                        ${fastjson.version}                                                                        org.springframework                        spring-context                        ${spring.version}                                                        org.springframework                        spring-context-support                        ${spring.version}                                                        org.springframework                        spring-beans                        ${spring.version}                                                        org.springframework                        spring-webmvc                        ${spring.version}                                                        org.springframework                        spring-aspects                        ${spring.version}                                                        org.springframework                        spring-test                        ${spring.version}                                                                        org.slf4j                        slf4j-log4j12                        ${slf4j.version}                                                                        org.apache.commons                        commons-lang3                        ${commons-lang3.version}                                                        org.apache.commons                        commons-io                        ${commons-io.version}                                                                        joda-time                        joda-time                        ${joda-time.version}                                                        org.apache.commons                        commons-pool2                        2.0                                                                        org.mongodb                        mongo-java-driver                        ${mongo-java-driver.version}                                                                        org.springframework.data                        spring-data-mongodb                        ${spring-data-mongodb.version}                                                                                                slf4j-api                                        org.slf4j                                                                                                                                                                                        org.apache.maven.plugins                                maven-compiler-plugin                                3.2                                                                        1.7                                        1.7                                        UTF-8                                                                                                                                        org.apache.maven.plugins                                maven-war-plugin                                2.6                                                                        true                                        WEB-INF/classes/**                                                                                                                                                src/main/webapp/WEB-INF                                                                                                                        applicationContext.xml                                                                                                                true                                                        WEB-INF                                                                                                                                                                                                        org.apache.maven.plugins                                maven-surefire-plugin                                2.7.1                                                


2、mongo.properties

#######################################################MongoDB configuration####################################################mongo.host=localhostmongo.port=27017mongo.dbname=demodbmongo.username=adminmongo.password=123456#一个线程变为可用的最大阻塞数mongo.connectionsPerHost=8#线程队列数,它与以上面connectionsPerHost值相乘的结果就是线程队列最大值mongo.threadsAllowedToBlockForConnectionMultiplier=4#连接超时时间(毫秒) mongo.connectTimeout=1500#最大等待时间mongo.maxWaitTime=1500#自动重连mongo.autoConnectRetry=true#scoket保持活动mongo.socketKeepAlive= true#scoket超时时间mongo.socketTimeout=1500#读写分离mongo.slaveOk=true


3、applicationContext.xml

                                                                                                                                                                                                classpath:mongo.properties                                classpath:log4j.properties                                                                                                                                                


单元测试:

package com.fwmagic.dao;import java.util.Date;import java.util.List;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.CriteriaDefinition;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;import com.fwmagic.pojo.Notice;@RunWith(SpringJUnit4Cla***unner.class)@ContextConfiguration("classpath:spring/applicationContext*.xml")public class NoticeDAOTest {        @Autowired        private NoticeDAO dao;                /**         * 查询所有文档         */        @Test        public void testList() {                Query query = new Query();                List notice = dao.list(query, Notice.class);                for (Notice n : notice) {                        System.out.println(n);                }        }        /**         * 插入文档         */        @Test        public void testInsert(){                Notice notice = new Notice();                notice.setSiteId(7);                notice.setCreator("diaochan");                notice.setTitle("今天星期一");                notice.setUpdateTime(new Date());                dao.insert(notice);        }                /**         * 更新文档         */        @Test        public void testUpdate(){                Criteria criteria = new Criteria();                criteria.and("creator").is("zhaojun");                Query query = new Query(criteria);                dao.update(query, Update.update("createTime", new Date()), Notice.class);        }                /**         * 删除一行文档         */        @Test        public void testRemove(){                Criteria criteria = new Criteria();                criteria.and("creator").is("zhaojun");                Query query = new Query(criteria);                dao.remove(query, Notice.class);        }                //未完待续……}


NoticeDAO.java

package com.fwmagic.dao;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import org.springframework.stereotype.Repository;import com.fwmagic.pojo.Notice;import com.mongodb.WriteResult;@Repositorypublic class NoticeDAO implements BaseDAO{        @Autowired        private MongoTemplate mt;                @Override        public List list(Query query, Class entityClass) {                return mt.find(query, entityClass);        }        @Override        public Notice findOne(Query query, Class entityClass) {                return mt.findOne(query, entityClass);        }                @Override        public void insert(Notice notice){                mt.insert(notice);        }                @Override        public WriteResult update(Query query, Update update, Class entityClass){                return mt.updateFirst(query, update, entityClass);        }                @Override        public void remove(Query query, Class entityClass){                mt.remove(query, entityClass);        }}


完整demo请移步此处查看.

0