mybatis一對多(對象和列表)

舉例,一個班級裏有多個學生
1.實體類

//班級
public class Class1 {

	private int id;
	private String name;
	private List<Student> stList;//list屬性
	//private Student st;//對象屬性(對象屬性在resultMap中使用 association 標籤)
	public List<Student> getStList() {
		return stList;
	}
	public void setStList(List<Student> stList) {
		this.stList = stList;
	}
	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;
	}
	
}
public class Student {

	private int id;
	private String name;
	private String classid;
	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;
	}
	public String getClassid() {
		return classid;
	}
	public void setClassid(String classid) {
		this.classid = classid;
	}
	
}

2.controller

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdemo.entity.Class1;
import com.bdemo.entity.Student;
import com.bdemo.service.StudentService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class StudentController {

	@Autowired
	private StudentService stService;
	
	@RequestMapping("getStudentList")
	@ResponseBody
	public synchronized JSONArray getStudentList(){
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		List<Student> stList = stService.getStudentList();
		System.out.println("stList=="+stList.size());
		jsonObject.put("data", stList);
		jsonArray.add(jsonObject);
		return jsonArray;
	}
	
	@RequestMapping("getCList")
	@ResponseBody
	public synchronized JSONArray getCList(){
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		List<Class1> cList = stService.getCList();
		System.out.println("cList=="+cList.size());
		jsonObject.put("data", cList);
		jsonArray.add(jsonObject);
		return jsonArray;
	}
}

3.service

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bdemo.entity.Class1;
import com.bdemo.entity.Student;
import com.bdemo.mapper.StudentMapper;

@Service
public class StudentService implements StudentMapper {

	@Autowired
	private StudentMapper stMapper;
	
	@Override
	public List<Student> getStudentList() {
		return stMapper.getStudentList();
	}

	@Override
	public List<Class1> getCList(){
		return stMapper.getCList();
	}
}

4.Mapper

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.bdemo.entity.Class1;
import com.bdemo.entity.Student;

@Mapper
public interface StudentMapper {

	List<Student> getStudentList();
	
	List<Class1> getCList();
	
}

5.Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<!-- 爲這個mapper指定一個唯一的namespace,namespace的值習慣上設置成包名+sql映射文件名,這樣就能夠保證namespace的值是唯一的 -->
<mapper namespace="com.bdemo.mapper.StudentMapper">

    <select id="getStudentList" resultMap="getStudentMap">
		select class1.id as class_id,class1.name as classname,student.name as stuname
        from class1,student
        where class1.id=student.classid;
    </select>
   	<resultMap id="getStudentMap" type="com.bdemo.entity.Student">
    	<result property="name" column="stuname"></result>
    	<result property="classid" column="class_id"></result>
    	<association property="cl" javaType="com.bdemo.entity.Class1">
    		<result property="name" column="classname"></result>
    	</association>
    </resultMap>
    
    <select id="getCList" resultMap="getCMap">
    	select class1.id as class_id,class1.name as classname,student.name as stuname
        from class1,student
        where class1.id=student.classid
    </select>
    <!-- class1類中,包含student對象屬性  -->
    <!--resultMap id="getCMap" type="com.bdemo.entity.Class1">
    	<result property="name" column="classname"></result>
    	<association property="st" javaType="com.bdemo.entity.Student">
    		<result property="name" column="stuname"></result>
    	</association>
    </resultMap-->
    <!-- class1類中,包含studentList屬性  -->
    <resultMap id="getCMap" type="com.bdemo.entity.Class1">
    	<result property="name" column="classname"></result>
    	<collection property="stList" ofType="com.bdemo.entity.Student">
    		<result property="name" column="stuname"></result>
    	</collection>
    </resultMap>
    
</mapper>

以上代碼說明:association 標籤是對象,collection 標籤是list, property對應實體類中的字段屬性名稱,column是別名,即select xx.x=xxx(別名)。 resultMap中的id,對應crud中的resultMap的值,type是指主實體類,association 中的javaType指主實體類中屬性爲對象的對象;collection 中的ofType指主實體類中屬性爲list的list
6.ajax前端迭代查詢結果

<script type="text/javascript">
$(function(){
	//getResultMap();
	getClassMap();//查詢班級和班級中所有的學生
})

function getResultMap(){
	$.ajax({
		url:"/getStudentList",
		type:"post",
		data:{id:1},
		success: function(result){
			$.each(result[0].data, function (i, n){
				console.log(result[0].data);
				console.log(n.name+"--"+n.classid+"--"+n.cl.name);
			})
		}
	})
}

function getClassMap(){
	$.ajax({
		url:"/getCList",
		type:"post",
		success: function(result){
			//console.log(result[0].data);
			//console.log(result[0].data[0].stList[0]);
			$.each(result[0].data, function (i, n){//外層循環是獲取班級
				//console.log(n.name+"--"+n.stList[0].name);
				$.each(result[0].data[i].stList, function (j, m){
				//內存循環是獲取每個班級中對應的所有學生,stList對應班級類中的學生list
					console.log(n.name+"--"+m.name);
				})
			})
			
		}
	})
}
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章