SSM框架下实现MyBatis连接查询,一对多和多对一

一、表

SSM框架下实现MyBatis连接查询,一对多和多对一_第1张图片


二、实体类



1、一个结果类(连接查询)


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 + "]";
	}
	
}




2、Classes

package com.cn.cust.entities;

import java.util.List;

public class Classes {
	
	private int id;
	private String name;
	private List 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 getStudentList() {
		return studentList;
	}
	public void setStudentList(List studentList) {
		this.studentList = studentList;
	}
	
}

3.Student

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;
	}
	
}

三、持久层

1.comboDao
package com.cn.cust.dao;

import java.util.List;

import com.cn.cust.entities.StudentAndClassesName;

// 连接查询测试
public interface comboDao {

	public List findStudentAndClassesName();
	
}

2.ClassesDao

package com.cn.cust.dao;

import com.cn.cust.entities.Classes;

public interface ClassesDao {

	public Classes getStudentsOfClass(int id);
	
}


3.StudentDao

package com.cn.cust.dao;

import com.cn.cust.entities.Student;

public interface StudentDao {

	public Student	getClassOfStudent(int id);
	
}

四、服务层

1、服务层接口
package com.cn.cust.service;

import java.util.List;

import com.cn.cust.entities.StudentAndClassesName;

public interface IStudentAndClassesService {
	// 1、连接查询
	public List getStudentClasses();
	// 2、一对多查询
	public void selectClassesFetchStudent();
	// 3、多对一查询
	public void selectStudentFetchClasses();
}

2、服务层实现
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 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 list = studentAndClassesService.getStudentClasses();
		// 使用迭代器输出list中的内容
		Iterator 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();
	}

}

六、测试结果

1、连接查询
SSM框架下实现MyBatis连接查询,一对多和多对一_第2张图片

2、一对多,查询class(一)级联查询学生student(多)

SSM框架下实现MyBatis连接查询,一对多和多对一_第3张图片

3、多对一,查询student(多)级联查询class(一)



七、源码和数据库文件下载地址

http://download.csdn.net/detail/xiaoxiaoxiaohaozi/9406470









你可能感兴趣的:(Mybatis)