0
点赞
收藏
分享

微信扫一扫

Mybatis中实体类映射时用collection进行分表查询和对象属性映射

天天天蓝loveyou 2022-01-22 阅读 65

我们在用Mybatis开发时会遇到类似于对象内包含对象属性的情况,我们编辑班级时需要将本班教师封装为List一并返回,诸如类似情况我们可以使用collection进行分表操作

现有表如下:

1.创建实体类

//Class.java
package com.demo.domain;

import java.util.List;

public class Class {
    private Long id;
    private String className;
    private List<Teacher> teacherList;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public List<Teacher> getTeacherList() {
        return teacherList;
    }

    public void setTeacherList(List<Teacher> teacherList) {
        this.teacherList = teacherList;
    }
}
//Teacher.java
package com.demo.domain;

public class Teacher {
    private Long id;
    private String name;
    private String sex;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

2.编辑接口

//ClassService.java
package com.demo.service;

import java.util.List;
import com.demo.domain.Class;
public interface ClassService {
    List<Class> listAll();
}

//ClassServiceImp.java
package com.demo.service.imp;

import com.demo.domain.Class;
import com.demo.mapper.ClassMapper;
import com.demo.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ClassServiceImp implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Override
    public List<Class> listAll() {
        return classMapper.listAll();
    }
}

//ClassMapper.java
package com.demo.mapper;

import com.demo.domain.Class;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ClassMapper {
    List<Class> listAll();
}

  ClassMapper.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="com.demo.mapper.ClassMapper">
    <resultMap id="BaseResultMap" type="com.demo.domain.Class">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="class_name" jdbcType="VARCHAR" property="className"/>
        <collection property="teacherList" select="getTeacherListByClassId" column="id" javaType="ArrayList">
        </collection>
    </resultMap>
    <select id="listAll" resultMap="BaseResultMap" parameterType="int">
        select *
        from class;
    </select>
    <select id="getTeacherListByClassId" parameterType="int" resultType="com.demo.domain.Teacher">
        select t.*
        from teacher t,
             ref_class_teacher r
        where t.id = r.class_id
          and t.id = #{id};
    </select>
</mapper>

3.测试接口

[
    {
        "id": 1,
        "className": "一班",
        "teacherList": [
            {
                "id": 1,
                "name": "赵老师",
                "sex": "男"
            },
            {
                "id": 1,
                "name": "赵老师",
                "sex": "男"
            },
            {
                "id": 1,
                "name": "赵老师",
                "sex": "男"
            }
        ]
    },
    {
        "id": 2,
        "className": "二班",
        "teacherList": [
            {
                "id": 2,
                "name": "钱老师",
                "sex": "女"
            },
            {
                "id": 2,
                "name": "钱老师",
                "sex": "女"
            }
        ]
    },
    {
        "id": 3,
        "className": "三班",
        "teacherList": [
            {
                "id": 3,
                "name": "孙老师",
                "sex": "男"
            },
            {
                "id": 3,
                "name": "孙老师",
                "sex": "男"
            },
            {
                "id": 3,
                "name": "孙老师",
                "sex": "男"
            }
        ]
    }
]

 

举报

相关推荐

0 条评论