1024programmer Mongodb Spring support for MongoDB

Spring support for MongoDB

Spring’s support for mongoDB includes the following features:

The configuration of mongodb in spring supports the way of annotation @Configuration and xml

MongoTemplate helper class extends Mongo
The production efficiency of operations provides conversion between document and pojo types

Add dependencies to pom.xml

the

the
org.springframework.data

the
spring-data-mongodb

1.1.0.RELEASE

You can set the log level to debug to view more log information

log4j.category.org.springframework.data.document.mOngodb=DEBUG

log4j.appender.stdout.layout.COnversionPattern=%d{ABSOLUTE} %5p
%40.40c:%4L – %m%n

app-mongodb.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

the
xmlns:cOntext=”http://www.springframework.org/schema/context”
xmlns:mOngo=”http://www.springframework.org/schema/data/mongo”

the
xsi:schemaLocation=”http://www.springframework.org/schema/context

 
http://www.springframework.org/schema/context/spring-context-3.0.xsd

 
http://www.springframework.org/schema/data/mongo

 
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd

 
http://www.springframework.org/schema/beans

 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

the

<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”
/>

the

the

Person.java

package com.dempe.summer.person.model;

import org.springframework.data.annotation.Id;

import
org.springframework.data.mongodb.core.mapping.Document;

/**

* @author: Zheng Dongping

* @version 1.0 date: 2013-12-20

*/

@Document(collection = “person”)

public class Person {

@Id

private String id;

private String name;

private Integer age;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

How the ‘_id’ field is handled in the mapping layer

MongoDB will generate _id fields for all documents by default. The MongoMappingConverter class has certain rules to manage the mapping from java classes to “_id” fields.

When a field is marked with @Id annotation, or the name of this field is id, then this field will be mapped to “_id”

PersonRepository.javapackage
com.dempe.summer.person.persist;

import java.util.List;

import org.bson.types.ObjectId;

import
org.springframework.data.mongodb.repository.MongoRepository;

import org.springframework.data.mongodb.repository.Query;

import com.dempe.summer.person.model.Person;

/**

* @author: Zheng Dongping

* @version 1.0 date: 2013-12-20

*/

public interface PersonRepository extends
MongoRepository {

// The ?0 here represents the first parameter in the method

@Query(“{ ‘name’:?0}”)

public List findByName(String
name);

}

PersonController.java

package com.dempe.summer.person.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;

import
org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.dempe.summer.person.model.Person;

import com.dempe.summer.person.persist.PersonRepository;

/**

* @author: Zheng Dongping

* @version 1.0 date: 2013-12-20

*/

@Controller

@RequestMapping(value = “/person”)

public class PersonController {

@Inject

 PersonRepository repository;

@RequestMapping(value = “/test”)

@ResponseBody

public String testMongo() {

// repository. deleteAll();

Person person = new Person();

person.setId(“12234499”);

person.setName(“dempe”);

person.setAge(3);

repository.save(person);

Person p = new Person();

p.setId(“2222”);

p.setName(“dempe”);

p.setAge(4);

repository.save(p);

List person2List =
repository.findByName(“dempe”);

System.out.println(“person2:” +
person2List.get(0).getName() + person2List.size());

return “success”;

}

}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/spring-support-for-mongodb/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索