<dependencies> <!-- other dependency elements omitted --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.1.0.RELEASE</version> </dependency> </dependencies>
<!--指定配置文件地址--> <context:property-placeholder location="classpath:/mongo.properties"/> <mongo:mongo id="mongo" host="${${mongo.host}}" port="${${mongo.port}}"> <mongo:options connections-per-host="${${mongo.connectionsPerHost}}" threads-allowed-to-block-for-connection-multiplier="${${mongo.threadsAllowedToBlockForConnectionMultiplier}}" connect-timeout="${${mongo.connectTimeout}}" max-wait-time="${${mongo.maxWaitTime}}" auto-connect-retry="${${mongo.autoConnectRetry}}" socket-keep-alive="${${mongo.socketKeepAlive}}" socket-timeout="${${mongo.socketTimeout}}" slave-ok="${${mongo.slaveOk}}" write-number="1" write-timeout="0" write-fsync="true" /> </mongo:mongo> <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> <description>认证口令</description> <constructor-arg name="username" value="${${mongo.username}}"/> <constructor-arg name="password" value="${${mongo.password}}"/> </bean> <bean name="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongo" ref="mongo"/> <constructor-arg name="userCredentials" ref="userCredentials"/> <constructor-arg name="databaseName" value="nq-s2s"/> </bean>
mongo.host=192.168.0.251 mongo.port=27017 mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=4 mongo.connectTimeout=1000 mongo.maxWaitTime=1500 mongo.autoConnectRetry=true mongo.socketKeepAlive=true mongo.socketTimeout=1500 mongo.slaveOk=true mongo.username=*** mongo.password=*** mongo.dbname=***
AdCenter.java是一个标准的javaBean,采用注解的方式实现ORM映射。@Document、@Id是必要的两个注解,@Document映射Mongodb中的Collection,@Id映射指定Collection中的标识_id。
其他注解标签有:@Indexed索引、@Transient不映射此属性
import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "adCenterDeduction") public class AdCenter { @Id private String id; private String business; private String adCenter; private int deductNum; private int requestNum; public String getBusiness() { return business; } public void setBusiness(String business) { this.business = business; } public String getAdCenter() { return adCenter; } public void setAdCenter(String adCenter) { this.adCenter = adCenter; } public int getDeductNum() { return deductNum; } public void setDeductNum(int deductNum) { this.deductNum = deductNum; } public int getRequestNum() { return requestNum; } public void setRequestNum(int requestNum) { this.requestNum = requestNum; } public String getId() { return id; } public void setId(String id) { this.id = id; } public AdCenter() { } public AdCenter(String business, String adCenter, int deductNum, int requestNum) { this.business = business; this.adCenter = adCenter; this.deductNum = deductNum; this.requestNum = requestNum; } @Override public String toString() { return "AdCenter [id=" + id + "business=" + business + ", adCenter=" + adCenter + ", deductNum=" + deductNum + ", requestNum=" + requestNum + "]"; } }
@Before public void setUp() throws Exception { System.setProperty("NetQinServerType", "REL"); System.setProperty("NetQinServerLocation", "C"); } @Test public void testAdCenter() throws UnknownHostException { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml文件"); MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); AdCenter adCenter = new AdCenter("ms","test",99,98); mongoOperation.insert(adCenter); assert adCenter.getId()!=null; System.out.println("newAdCenter:"+adCenter.toString()); Query query = new Query(Criteria.where("business").is(adCenter.getBusiness())); query.addCriteria(Criteria.where("adCenter").is(adCenter.getAdCenter())); //查找 AdCenter exsitAdCenter = mongoOperation.findOne(query,AdCenter.class); assert exsitAdCenter!=null; System.out.println("exsitAdCenter:"+exsitAdCenter.toString()); //修改 Update update = new Update().set("deductNum", 0); AdCenter modifyAd = mongoOperation.findAndModify(query, update, AdCenter.class); assert modifyAd.getDeductNum()==exsitAdCenter.getDeductNum(); System.out.println("beforeModifyAdCenter:"+exsitAdCenter.toString()); exsitAdCenter = mongoOperation.findOne(query,AdCenter.class); assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==0; System.out.println("AfterModifyAdCenter:"+exsitAdCenter.toString()); //自增 Update incUpdate = new Update().inc("deductNum", 1); mongoOperation.findAndModify(query, incUpdate, AdCenter.class); exsitAdCenter = mongoOperation.findOne(query,AdCenter.class); assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==1; System.out.println("AfterIncAdCenter:"+exsitAdCenter.toString()); //删除 mongoOperation.findAndRemove(query,AdCenter.class); }
newAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98] exsitAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98] beforeModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98] AfterModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=0, requestNum=98] AfterIncAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=1, requestNum=98]