CREATE TABLE `classes` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`class_name` varchar(255) DEFAULT NULL,
KEY `class_id` (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1003 DEFAULT CHARSET=utf8;
insert into `classes`(`class_id`,`class_name`) values (1001,'一班'),(1002,'二班');
CREATE TABLE `student` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
KEY `student_id` (`student_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10004 DEFAULT CHARSET=utf8;
insert into `student`(`student_id`,`name`,`age`,`class_id`) values (10001,'张三',20,1001),(10002,'李四',21,1001),(10003,'王五',22,1002);
package com.example.model;
/**
* @author wrp
* @Description com.example.model.Student
* @Date 2017/1/4
*/
public class Student {
private Integer studentId;
private String name;
private Integer age;
private Integer classId;
private Classes classes;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
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;
}
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
}
package com.example.model;
/**
* @author wrp
* @Description com.example.model.Class
* @Date 2016/12/30
*/
public class Class {
private Integer classId;
private String className;
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
package com.example.dao;
import com.example.model.Classes;
/**
* @author wrp
* @Description com.example.dao.Classes
* @Date 2017/1/4
*/
public interface ClassesDao {
Classes getClasses(Integer classId);
}
package com.example.dao;
import com.example.model.Student;
import java.util.List;
/**
* @author wrp
* @Description com.example.dao.StudentDao
* @Date 2017/1/4
*/
public interface StudentDao {
List selectAllStudent1();
List selectAllStudent2();
List selectAllStudent3();
}
package com.example.dao;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.model.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author wrp
* @Description com.example.dao.StudentDaoTest
* @Date 2017/1/4
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentDaoTest {
@Autowired
private StudentDao studentDao;
@Test
public void selectAllStudent1() {
List students = studentDao.selectAllStudent1();
System.out.println(JSON.toJSONString(students));
//[{"age":20,"classId":1001,"classes":{"classId":1001,"className":"一班"},"name":"张三","studentId":10001},{"age":21,"classId":1001,"classes":{"classId":1001,"className":"一班"},"name":"李四","studentId":10002},{"age":22,"classId":1002,"classes":{"classId":1002,"className":"二班"},"name":"王五","studentId":10003}]
}
@Test
public void selectAllStudent2() {
List students = studentDao.selectAllStudent2();
System.out.println(JSON.toJSONString(students));
}
@Test
public void selectAllStudent3() {
List students = studentDao.selectAllStudent3();
// 禁用FastJson的“循环引用检测”特性。
System.out.println(JSON.toJSONString(students, SerializerFeature.DisableCircularReferenceDetect));
}
}