标签:安装 lex ref 整合 oop turn find depend dock
docker search mongodb
docker pull mongo
docker images
docker run -itd --name mongo -p 27017:27017 mongo
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
mongo.database=admin
mongo.server.address=127.0.0.1
mongo.server.port=27017
package com.test.config;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
/**
* @author 11699
* @date 20/3/21 - 11:11
*/
@Configuration
@PropertySource("classpath:mongo.properties")
public class MongoConfig extends AbstractMongoConfiguration {
@Autowired
private Environment env;
@Override
protected String getDatabaseName() {
return env.getProperty("mongo.database");
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient(
env.getProperty("mongo.server.address"),
Integer.parseInt(env.getProperty("mongo.server.port")));
}
}
package com.test.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* @author 11699
* @date 20/3/21 - 11:28
*/
@Document
public class Student {
@Id
private String id;
private String name;
@Field("stu_age")
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;
}
}
package com.test;
import com.test.config.MongoConfig;
import com.test.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Unit test for simple App.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class AppTest {
@Autowired
private MongoOperations mongo;
}
@Test
public void testSave(){
Student student = new Student();
student.setName("小明");
student.setAge(11);
mongo.save(student, "stu");
}
@Test
public void testCount(){
long count = mongo.getCollection("stu").count();
System.out.println("数量为:"+ count);
}
@Test
public void testQuery(){
List<Student> list = mongo.findAll(Student.class, "stu");
for (Student student : list) {
System.out.println(student);
}
}
@Test
public void testFindById(){
Student stu = mongo.findById("5e759d94ecc4c0fe0007f312", Student.class, "stu");
System.out.println(stu);
}
@Test
public void testComplexQuery(){
List<Student> list = mongo.find(Query.query(Criteria.where("name").is("小明")), Student.class, "stu");
for (Student student : list) {
System.out.println(student);
}
}
@Test
public void testRemove(){
//先查询
Student stu = mongo.findById("5e759d94ecc4c0fe0007f312", Student.class, "stu");
mongo.remove(stu);
}
标签:安装 lex ref 整合 oop turn find depend dock
原文地址:https://www.cnblogs.com/erkye/p/12539099.html