//关系表 Mapper接口
public interface RelationMapper {
@Select("select id,name from TEACHER")
@Results({@Result(property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "students",javaType = List.class,column ="id",
many = @Many(select = "com.example.mapper.RelationMapper.findStudents"))})
List<TeacherVo> findTeacherAndStudents();
@Select("select s.name from RELATION r,STUDENT s where s.id = r.sid and r.tid = #{tid}")
List<Student> findStudents(Long tid);
}
@Select("select id,name from TEACHER")中的id 通过 @Result(property = "students",javaType = List.class,column ="id",传到 many = @Many(select = "com.example.mapper.RelationMapper.findStudents"))})
在 List findStudents(Long tid) 中接到传递过来的id 并赋值给sql语句中 #{tid}
这样就完成了,老师学生通过关系表完成1对多。
在对应的实现类中,实现findTeacherAndStudents()方法即可,控制层同理~
layui对后台传来的的roles集合进行遍历展示职位:
userlimit.html部分代码
{field: 'id', title: '序号', sort: true, fixed: 'left'}
, {field: 'no', title: '工号'}
, {field: 'name', title: '姓名', sort: true}
, {field: 'roles', title: '职位', sort: true, templet:function(info){
var rlist = info.roles;
var infos = "";
for(var i = 0; i < rlist.length; i++){
infos = infos + " "+rlist[i].info;
}
return infos;
}}