文章目錄
  1. 1. Question
    1. 1.1. question
  2. 2. Method
    1. 2.1. the first way
    2. 2.2. the second way(TODO)

当我们对两个实体进行关联的时候例如我们上一篇文章中提到的teacher与student实体中提到的@ManyToMany的时候,就会遇到一个问题,当我们获取给前台传送数据的时候就会遇到对象被jackson转化为json格式的数据的时候,就会有循环加载的问题,及teacher里面student, student里面有teacher……这样就会有四循环的问题。

Question

question

让我们看一下问题吧!实体关系及代码参见上一片博客

src/main/java/com.mengyunzhi/controller/TeacherController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mengyunzhi.controller;
import com.mengyunzhi.repository.Teacher;
import com.mengyunzhi.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by liming on 17-6-7.
* Teacher实体的C层
*/
@RestController
@RequestMapping("/Teacher")
public class TeacherController {
//当然这里我们不应该直接调用Repository,按照软件工程的角度来说我们应该建立Service层,然后将Repository封装在service层中
@Autowired
private TeacherRepository teacherRepository;
@GetMapping("/getAll")
public List<Teacher> getAll() {
return (List<Teacher>) teacherRepository.findAll();
}
}

让我来看一下我们准备的数据:
teacher sql语句:select *from teacher;

1
2
3
4
5
+----+------------+
| id | name |
+----+------------+
| 1 | TeacherPan |
+----+------------+

student sql语句: select *from student;

1
2
3
4
5
+----+-----------+--------+
| id | klass | name |
+----+-----------+--------+
| 1 | 软件152 | liming |
+----+-----------+--------+

student_teacher sql语句: select *from student_teacher;

1
2
3
4
5
+------------+------------+
| teacher_id | student_id |
+------------+------------+
| 1 | 1 |
+------------+------------+

curl测试, 在终端输入以下命令:
curl http://127.0.0.1:8080/Teacher/getAll

1
[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":[{"id":1,"name":"liming","klass":"软件152","teachers":[{"id":1,"name":"TeacherPan","students":...............

就会发现数据出现了死循环。

Method

the first way

当然了,这一种解决办法很简单,加@JsonIgnore注解,这句话的意思就是我们在将其转化为json对象的时候忽略这个属性,但是这个方法会存在一个问题就是我们前台想用这个属性的时候就会发现因为没有而报错。想哭。来让我们测试一下吧!
src/main/java/com.mengyunzhi/repository/Teacher.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Entity
public class Teacher {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//姓名
private String name;
@ManyToMany(cascade = CascadeType.REMOVE)
@JoinTable(
name = "student_teacher",
joinColumns = @JoinColumn(name = "teacher_id"),
inverseJoinColumns = @JoinColumn(name = "student_id")
)
@JsonIgnore @Lazy //增加的代码
private Set<Student> students = new HashSet<>();
public Teacher() {
}
public Teacher(String name, Set<Student> students) {
this.name = name;
this.students = students;
}
//getter and setter
}

然后写测试用例

src/test/java/com.mengyunzhi/controller/TeacherController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc //在测试的时候实现MVC的测试
public class TeacherControllerTest {
@Autowired
private TeacherRepository teacherRepository;
@Autowired
private StudentRepository studentRepository;
@Autowired
private MockMvc mockMvc; //实现MVC测试
static private Logger logger = Logger.getLogger(TeacherControllerTest.class.getName());
@Test
public void getAllTest() throws Exception {
logger.info("--------------------TeacherController getAllTest---------------------");
logger.info("新建一个Teacher实体");
Teacher teacher = new Teacher();
logger.info("保存Teacher实体");
teacherRepository.save(teacher);
logger.info("新建一个Student实体");
Student student = new Student();
logger.info("建立Teacher集合");
Set<Teacher> teachers = new HashSet<>();
teachers.add(teacher);
logger.info("保存Student实体");
studentRepository.save(student);
logger.info("模拟发送数据");
MvcResult result = this.mockMvc.perform(get("/Teacher/getAll")
.contentType("application/json"))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
logger.info("获取返回数据");
String content = result.getResponse().getContentAsString();
logger.info("将获取的内容转化为jsonArray对象, 并断言数组长度不为0");
JSONArray jsonArray = JSONArray.fromObject(content);
assertThat(jsonArray.size()).isNotEqualTo(0);
}
}

然后我们发现测试成功了

the second way(TODO)

文章目錄
  1. 1. Question
    1. 1.1. question
  2. 2. Method
    1. 2.1. the first way
    2. 2.2. the second way(TODO)