举例,一个班级里有多个学生
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>