天天看点

mybatis 注解方式多对多

类型表(type)字段:id,name

年级表(grade)字段:id,name

类型年级:typeid,gradeid

实体类(type):

public class ScTypeEntity {

    private String id;
    private String name;
     public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    private List<GradeEntity> grades;

    public List<GradeEntity> getGrades() {
        return grades;
    }

    public void setGrades(List<GradeEntity> grades) {
        this.grades = grades;
    }
}
      

实体类(grade)

public class GradeEntity {
  private String id;
    private String name;
     public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

mapper类(type):

public interface ScTypeMapper {
    @Select("select *,id typeid from jc_sctype where isdel<>1 order by id")
    @Results({
            @Result(property="grades",column = "typeid",
                    many = @Many(select = "com.jiapeng.mainsvr.mapper.GradeMapper.listBySctype"))
    })
    List<ScTypeEntity> listAll();
}      
public interface GradeMapper {
  @Select("select a.* from jc_grade a\n" +
            "right join jc_sc_grade b\n" +
            "on a.id=b.gradeid\n" +
            "where b.typeid=#{id}\n" +
            "\n")
    List<GradeEntity> listBySctype(String id);
}