Mybatis的多对一,一对多


1.Xml

Mabatisxml性的核心配置文件











  
    
    
      
      
        
        
        
        
      
    
  
  
   
   
  


 


 
 
 

   
   


 


Grademap.xml


 


 
 


2.注解

一对多

public interface GradeMapper {
/**
 * 根据名称查询所有的班级
 * @param foodName
 * @return
 */
public List queryAllGrade();
/**
 * 通过gid查询班级
 * @param gid
 * @return
 */
@Results({@Result(property="gname1",column="gname"),
//@Many查询多条 @Result(property="studentList",column="gid",javaType=ArrayList.class,many=@Many(select="cn.et.lesson03.returnMap.anno.StudentMapper.queryStudentByGid"))
})
@Select("select * from grade where gid=#{0}")
public Grade queryGrade(String gid);
@Results(@Result(property="gname1",column="gname"))
@Select("select * from grade where gid=#{0}")
public Grade queryGradeByGid(String gid);
}


 

多对一

public interface StudentMapper {
/**
 * 通过编号查询学生
@one查询一条
 */
@Results({
@Result(property="grade",column="gid",one=@One(select="cn.et.lesson03.returnMap.anno.GradeMapper.queryGradeByGid"))
})
@Select("select * from student where sid=#{0}")
public Student queryStudent(String sid);
/**
 * 通过班级ID查询学生
 */
@Select("select * from student where gid=#{0}")
public List queryStudentByGid(String gid);
}


 

你可能感兴趣的:(mybatis)