package com.cn.cust.entities; //连接查询测试 public class StudentAndClassesName { private String sName; private String cName; public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getcName() { return cName; } public void setcName(String cName) { this.cName = cName; } @Override public String toString() { return "StudentAndClassesName [sName=" + sName + ", cName=" + cName + "]"; } }
package com.cn.cust.entities; import java.util.List; public class Classes { private int id; private String name; private List<Student> studentList; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Classes [id=" + id + ", name=" + name + "]"; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
package com.cn.cust.entities; public class Student { private int id; private String name; private Classes classes; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
package com.cn.cust.dao; import java.util.List; import com.cn.cust.entities.StudentAndClassesName; // 连接查询测试 public interface comboDao { public List<StudentAndClassesName> findStudentAndClassesName(); }
2.ClassesDao
package com.cn.cust.dao; import com.cn.cust.entities.Classes; public interface ClassesDao { public Classes getStudentsOfClass(int id); }
package com.cn.cust.dao; import com.cn.cust.entities.Student; public interface StudentDao { public Student getClassOfStudent(int id); }
package com.cn.cust.service; import java.util.List; import com.cn.cust.entities.StudentAndClassesName; public interface IStudentAndClassesService { // 1、连接查询 public List<StudentAndClassesName> getStudentClasses(); // 2、一对多查询 public void selectClassesFetchStudent(); // 3、多对一查询 public void selectStudentFetchClasses(); }
package com.cn.cust.serviceImpl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.cn.cust.dao.ClassesDao; import com.cn.cust.dao.StudentDao; import com.cn.cust.dao.comboDao; import com.cn.cust.entities.Classes; import com.cn.cust.entities.Student; import com.cn.cust.entities.StudentAndClassesName; import com.cn.cust.service.IStudentAndClassesService; @Service("studentAndClassesService") public class StudentAndClassesServiceImpl implements IStudentAndClassesService { @Resource private comboDao my_combaDao; @Resource private ClassesDao classesDao; @Resource private StudentDao studentDao; // 1、连接查询 @Override public List<StudentAndClassesName> getStudentClasses() { return this.my_combaDao.findStudentAndClassesName(); } // 2、一对多查询 @Override public void selectClassesFetchStudent() { Classes classes = this.classesDao.getStudentsOfClass(11); System.out.println(classes); System.out.println(classes.getStudentList().size()); for(Student students : classes.getStudentList()) { System.out.println(students); } } // 3、多对一查询 @Override public void selectStudentFetchClasses() { Student student = this.studentDao.getClassOfStudent(1); System.out.println(student); System.out.println(student.getClasses()); } }
package com.cn.cust.test; import java.util.Iterator; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.cn.cust.entities.StudentAndClassesName; import com.cn.cust.service.IStudentAndClassesService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mybatis.xml") public class TestStudentClasses { @Resource private IStudentAndClassesService studentAndClassesService; // 1、连接查询测试类 @Test public void testLianJie() { List<StudentAndClassesName> list = studentAndClassesService.getStudentClasses(); // 使用迭代器输出list中的内容 Iterator<StudentAndClassesName> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } System.out.println("成功!!"); } // 2、一对多查询 @Test public void testOneToMore() { studentAndClassesService.selectClassesFetchStudent(); } // 3、多对一查询 @Test public void testMoreToOne() { studentAndClassesService.selectStudentFetchClasses(); } }
http://download.csdn.net/detail/xiaoxiaoxiaohaozi/9406470