In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Version:
Spring:4.1.3.RELEASE
Mongo-java-driver.version:3.4.1
Spring-data-mongodb.version:1.7.0.RELEASE
Configuration:
1. Add core dependencies to pom.xml (two)
+ + +
Org.mongodb mongo-java-driver 3.4.1 org.springframework.data spring-data-mongodb 1.7.0.RELEASE
+ + +
The complete pom.xml file is as follows:
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 # the maximum number of blocks that a thread becomes available, the number of mongo.connectionsPerHost=8# thread queues The result multiplied by the connectionsPerhost value above is the thread queue maximum mongo.threadsAllowedToBlockForConnectionMultiplier=4# connection timeout (milliseconds) mongo.connectTimeout=1500# maximum wait time mongo.maxWaitTime=1500# automatic reconnection mongo.autoConnectRetry=true#scoket keep active mongo.socketKeepAlive= true#scoket timeout mongo.socketTimeout=1500# read-write separation mongo.slaveOk=true
3 、 applicationContext.xml
Classpath:mongo.properties classpath:log4j.properties
Unit test:
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 / * query all documents * / @ Test public void testList () {Query query = new Query (); List notice = dao.list (query, Notice.class); for (Notice n: notice) {System.out.println (n) }} / * insert document * / @ Test public void testInsert () {Notice notice = new Notice (); notice.setSiteId (7); notice.setCreator ("diaochan"); notice.setTitle ("Today Monday") Notice.setUpdateTime (new Date ()); dao.insert (notice);} / * * Update document * / @ 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);} / * Delete a line of document * / @ Test public void testRemove () {Criteria criteria = new Criteria () Criteria.and ("creator"). Is ("zhaojun"); Query query = new Query (criteria); dao.remove (query, Notice.class);} / / to be continued. }
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);}}
Please check the complete demo here.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.