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:mongo= "Http://www.springframework.org/schema/data/mongo"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/ Spring-mongo.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <Context:property-placeholder Location= "Classpath*:mongodb.properties" /> <!--username= "${mongo.username}" - <!--password= "${database.password}" - <mongo:db-factoryID= "Mongodbfactory"Host= "${mongo.host}"Port= "${mongo.port}"dbname= "${mongo.dbname}" /> <BeanID= "Mongotemplate"class= "Org.springframework.data.mongodb.core.MongoTemplate"> <Constructor-argname= "Mongodbfactory"ref= "Mongodbfactory"/> </Bean></Beans>
Mongodb.properties
Mongo.host=127.0.0.1mongo.port=27017mongo.dbname=test#mongo.username= #database. password=
Test methods for manipulating MongoDB
@ResourceMongoOperations mongooperations; @Test Public void Testmongodb () { new User (); User.setname ("Testmongodb"); User.setage (); " CollectionName "); = Mongooperations.findone (new Query (Criteria.where ("name"). Is ("Testmongodb")), User. Class, "CollectionName"); System.out.println (Usergetfrommdb);}
Using Spring-data-mongodb to manipulate MongoDB