UserRepository.java
package com.example.sample;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByName(String name);
}
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SampleApplicationTests {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Test
void testMongDB() {
userRepository.findByName("홍길동").forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@SpringBootTest
class SampleApplicationTests {
private MongoTemplate mongoTemplate;
@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Test
void testMongDB() {
Query query = new Query();
query.addCriteria(Criteria.where("name").is("홍길동"));
mongoTemplate.find(query, User.class).forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]
UserRepository.java
package com.example.sample;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByNameAndGender(String name, String gender);
}
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SampleApplicationTests {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Test
void testMongDB() {
userRepository.findByNameAndGender("홍길동","남자").forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@SpringBootTest
class SampleApplicationTests {
private MongoTemplate mongoTemplate;
@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Test
void testMongDB() {
Query query = new Query();
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("name").is("홍길동"), Criteria.where("gender").is("남자"));
query.addCriteria(criteria);
mongoTemplate.find(query, User.class).forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]
UserRepository.java
package com.example.sample;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByAgeGreaterThan(int age);
}
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SampleApplicationTests {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Test
void testMongDB() {
userRepository.findByAgeGreaterThan(25).forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]
SampleApplicationTests.java
package com.example.sample;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@SpringBootTest
class SampleApplicationTests {
private MongoTemplate mongoTemplate;
@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Test
void testMongDB() {
Query query = new Query();
query.addCriteria(Criteria.where("age").gt(25));
mongoTemplate.find(query, User.class).forEach(System.out::println);
}
}
출력화면
User [id=5ee2d7bc4171c76d4851ea04, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]