package com.hyh.core.test;
import com.hyh.core.test.base.HyhTest;
import com.hyh.core.test.po.Person;
import com.hyh.mongodb.helper.MongoDBHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.TextCriteria;
import java.util.List;
/**
* MongoDb Test
*
* @Author: heyuhua
* @Date: 2021/1/19 10:28
*/
public class MongoDBTest extends HyhTest {
@Autowired
private MongoDBHelper mongoDBHelper;
@Test
public void testSave() {
Person person = new Person();
person.setName("heyuhua");
person.setAge(25);
mongoDBHelper.save(person);
Person person2 = new Person();
person2.setName("hyh");
person2.setAge(52);
mongoDBHelper.save(person2);
}
@Test
public void testSaveCollection() {
Person person = new Person();
person.setName("heyuhua");
person.setAge(25);
mongoDBHelper.save(person, "personCollection");
Person person2 = new Person();
person2.setName("hyh");
person2.setAge(52);
mongoDBHelper.save(person2, "personCollection");
}
@Test
public void testFindAll() {
List<Person> list = mongoDBHelper.findAll(Person.class);
for (Person person : list) {
System.out.println("name=" + person.getName() + ",age=" + person.getAge());
}
}
@Test
public void testFind() {
Criteria criteria = new Criteria();
criteria.and("age").gte("25");
Query query = new Query(criteria);
List<Person> list = mongoDBHelper.find(query,Person.class);
for (Person person : list) {
System.out.println("name=" + person.getName() + ",age=" + person.getAge());
}
}
@Test
@Override
public void test() {
System.out.println("---MongoDb Test---");
}
}
评论