User.java
package com.example.sample;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "user")
public class User {
@Id private String id;
private String name;
private String gender;
private int age;
private Tel tel;
private List<Cert> cert;
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Tel getTel() {
return tel;
}
public void setTel(Tel tel) {
this.tel = tel;
}
public List<Cert> getCert() {
return cert;
}
public void setCert(List<Cert> cert) {
this.cert = cert;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", tel=" + tel + ", cert="
+ cert + "]";
}
}
Tel.java
package com.example.sample;
public class Tel {
private String home;
private String company;
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "Tel [home=" + home + ", company=" + company + "]";
}
}
Cert.java
package com.example.sample;
public class Cert {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cert [name=" + name + "]";
}
}
UserRepository.java
package com.example.sample;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByName(String name);
@Query("{'tel.home': ?0}")
List<User> findByHomeTel(String home);
}
userService.java
package com.example.sample;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SampleService {
UserRepository userRepository;
@Autowired
public void setPersonRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void insertUserData(User user) {
userRepository.insert(user);
}
public List<User> selectUserDataByName(String name) {
List<User> user = userRepository.findByName(name);
return user;
}
public List<User> selectUserDataHomeTel(String tel) {
List<User> user = userRepository.findByHomeTel(tel);
return user;
}
}
SampleApplicationTests.java
package com.example.sample;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SampleApplicationTests {
private SampleService sampleService;
@Autowired
public void setSampleService(SampleService sampleService) {
this.sampleService = sampleService;
}
@Test
void testMongDB() {
Tel tel = new Tel();
tel.setHome("02-123-1234");
tel.setCompany("02-456-4567");
List<Cert> cert = new ArrayList<Cert>();
Cert cert1 = new Cert();
cert1.setName("정보처리기사");
cert.add(cert1);
Cert cert2 = new Cert();
cert2.setName("OCP");
cert.add(cert2);
User user = new User();
user.setName("홍길동");
user.setGender("남자");
user.setAge(30);
user.setTel(tel);
user.setCert(cert);
sampleService.insertUserData(user);
tel = new Tel();
tel.setHome("02-123-1234");
tel.setCompany("02-789-7890");
cert = new ArrayList<Cert>();
cert1 = new Cert();
cert1.setName("TOEIC");
cert.add(cert1);
cert2 = new Cert();
cert2.setName("TOEFL");
cert.add(cert2);
user = new User();
user.setName("홍길순");
user.setGender("여자");
user.setAge(25);
user.setTel(tel);
user.setCert(cert);
sampleService.insertUserData(user);
List<User> user1 = sampleService.selectUserDataByName("홍길동");
System.out.println("selectUserDataByName(\"홍길동\")");
System.out.println(user1);
List<User> user2 = sampleService.selectUserDataHomeTel("02-123-1234");
System.out.println("selectUserDataHomeTel(\"02-123-1234\")");
System.out.println(user2);
}
}
출력화면
selectUserDataByName("홍길동")
[User [id=5ee18a3670c0ff148eb8827d, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]]]
selectUserDataHomeTel("02-123-1234")
[User [id=5ee18a3670c0ff148eb8827d, name=홍길동, gender=남자, age=30, tel=Tel [home=02-123-1234, company=02-456-4567], cert=[Cert [name=정보처리기사], Cert [name=OCP]]], User [id=5ee18a3670c0ff148eb8827e, name=홍길순, gender=여자, age=25, tel=Tel [home=02-123-1234, company=02-789-7890], cert=[Cert [name=TOEIC], Cert [name=TOEFL]]]]
user Collection Data
/* 1 */
{
"_id" : ObjectId("5ee08658a3147ba09ddbad0a"),
"name" : "홍길동",
"gender" : "남자",
"age" : 30,
"tel" : {
"home" : "02-123-1234",
"company" : "02-456-4567"
},
"cert" : [
{
"name" : "정보처리기사"
},
{
"name" : "OCP"
}
],
"_class" : "com.example.sample.User"
}
/* 2 */
{
"_id" : ObjectId("5ee08666a3147ba09ddbad12"),
"name" : "홍길순",
"gender" : "여자",
"age" : 25,
"tel" : {
"home" : "02-123-1234",
"company" : "02-789-7890"
},
"cert" : [
{
"name" : "TOEIC"
},
{
"name" : "TOEFL"
}
],
"_class" : "com.example.sample.User"
}