MyBatis关联映射

MyBatis关联映射

一、Dao层的接口的内容

复制代码
//一对多关联查询
    public SmbmsRoleEntity getAllByRoleId(Integer id);

    //多对一关联查询 查询所有用户信息  包含角色信息
    public List<User> getUserList();

    //多对多关联查询
    //查询所有学生信息  以及授课教员
    public List<Student> getStudentInfo();


    //自查询
    //查询 河南省下的所有子集
    public City getCityAndChildCitys(Integer cid);
复制代码

二、xml文件的内容

复制代码
<mapper namespace="com.marketsys.dao.ProviderTest">
    <!--由于是关联查询  返回的是多张表中的结果集 ,必须定义resultMap映射-->

    <!--一对多关联查询-->
    <!--查询经理角色以及该角色下的员工集合-->
    <resultMap id="ById" type="com.marketsys.entity.SmbmsRoleEntity">
        <id property="rid" column="rid"></id>
        <result property="roleName" column="roleName"></result>
        <!--此处使用的是collection节点 ,由于Role类中插入的是List集合  ofType:为集合中的泛型-->
        <collection property="getRoleEntitys"  ofType="com.marketsys.entity.User" select="getUser" column="rid">
            <!--在collection中声明Role中的属性与列的映射-->
            <!--<id  property="id" column="id"></id>
            <result property="userName" column="userName"></result>-->
        </collection>
    </resultMap>

   <!-- <select id="getAllByRoleId" parameterType="int" resultMap="ById">
    select r.rid,roleName,u.id,userName from smbms_role r inner join smbms_user u on r.rid=u.userRole where r.rid=#{id}
    </select>-->


    <select id="getAllByRoleId" parameterType="int" resultMap="ById">
        select * from smbms.smbms_role where rid=#{id}
    </select>
    <select id="getUser" resultType="com.marketsys.entity.User">
        select * from smbms.smbms_user where userRole=#{rid};
    </select>


    <!--多对一关联查询-->
    <!--查询所有用户信息  包含用户角色-->
    <resultMap id="userListAndRole" type="com.marketsys.entity.User">
        <id column="id" property="id"></id>
        <result column="userName" property="userName"></result>
        <association property="role" javaType="com.marketsys.entity.SmbmsRoleEntity" select="getRole" column="userRole">
            <id column="rid"  property="rid"></id>
            <result column="roleName" property="roleName"></result>
        </association>
    </resultMap>

    <!--<select id="getUserList" resultMap="userListAndRole">
       select  u.id,u.userName,u.userRole,r.rid,r.roleName from smbms.smbms_user as u ,smbms.smbms_role  as r where u.userRole=r.rid 
    </select>-->
    <select id="getUserList" resultMap="userListAndRole">
       select * from smbms.smbms_user
    </select>
<select id="getRole" resultType="com.marketsys.entity.SmbmsRoleEntity">
    select * from smbms.smbms_role where rid=#{userRole}
</select>


    <!--多对多关联查询-->
    <!--查询所有学生信息 以及授课教员-->
    <resultMap id="studentInfoMapper" type="com.marketsys.entity.Student">
        <id column="stuid" property="stuid"></id>
        <result column="stuname" property="stuname"></result>
        <collection property="teachers" ofType="com.marketsys.entity.Teacher">
            <id column="tid" property="tid"></id>
            <result column="tname" property="tname"></result>
        </collection>
    </resultMap>
    <select id="getStudentInfo" resultMap="studentInfoMapper">
        select * from student,teacher,stu_t where student.stuid=stu_t.stuid and teacher.tid=stu_t.tid;
    </select>



    <!--自关联查询-->
    <!--查询河南省下的所有子集-->
    <resultMap id="cityMapper" type="com.marketsys.entity.City">
        <id column="cid" property="cid"></id>
        <result column="cname" property="cname"></result>
        <result column="pid" property="pid"></result>
        <collection property="childCitys" ofType="com.marketsys.entity.City" select="getCity" column="cid">
            <id column="cid" property="cid"></id>
            <result column="cname" property="cname"></result>
            <result column="pid" property="pid"></result>
        </collection>
    </resultMap>
    <select id="getCityAndChildCitys" resultMap="cityMapper">
        select * from city where cid=#{cid}
    </select>
    <select id="getCity" resultMap="cityMapper">
        select * from city where pid=#{cid};
    </select>
</mapper>
复制代码

三、关联查询测试类的内容

复制代码
//一对一
@Test
public void getUserListTest(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
ISmbmsUserDao mapper = sqlSession.getMapper(ISmbmsUserDao.class);
List<SmbmsUser> userList = mapper.getUserList();
for(SmbmsUser user:userList){
System.out.println("用户:"+user.getUserName()+" 角色:"+user.getRole().getRoleName());
}
}
//一对多
public void getRoleAndUserTest(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
ISmbmsRoleDao mapper = sqlSession.getMapper(ISmbmsRoleDao.class);


SmbmsRole role = mapper.getRoleAndUser(3);
System.out.println("角色:"+role.getRoleName());
for(SmbmsUser user : role.getUserList()){
System.out.print(" 用户:"+user.getUserName());
}
}
//多对一 
@Test
public void getRoleAndUserTest(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
ISmbmsRoleDao mapper = sqlSession.getMapper(ISmbmsRoleDao.class);


SmbmsRole role = mapper.getRoleAndUser(3);
System.out.println("角色:"+role.getRoleName());
for(SmbmsUser user : role.getUserList()){
System.out.print(" 用户:"+user.getUserName());
}
}


//多对多   
  @Test
     public void test3(){
      List<Student> studentInfo = providerTest.getStudentInfo();
         for (Student student:studentInfo){
           System.out.println("学生:"+student.getStuname());
          for (Teacher teacher:student.getTeachers()){ System.out.println(" 教员:"+teacher.getTname());
 }
  }
   }
//自查询
  @Test
  public void test4(){
    City cityAndChildCitys = providerTest.getCityAndChildCitys(410000);
      System.out.println(cityAndChildCitys.toString()); }
复制代码

四、关联查询含义

  1.一对一

   在任意一方引入对方主键作为外键。

  2.一对多关联查询

    一对多关联查询是指,在查询一对象的时候,同时将其所关联的多放对象也都查询出来。

  3.多对一关联查询

    这里的多对一关联查询是指,在查询多方对象的时候,同时将其所关联的一方对象也查询出来。

    由于查询多方对象时也是一个一个查询,所以多对一关联查询,其实就是一对一关联查询。即一对一关联查询的实现方式与多对一的实现方式是相同的。

  4.多对多关联查询

    多对多关联关系,例如一个学生可以选多门课程,而一门课程可以由多个学生选择。多对多关系,其实是由两个互反的一对多关系组成。一般情况下,多对多关系都会通过一个中间表来建立

  5.自关联查询

    所谓自关联查询是指,自己即充当一方,又充当多方,是1:n或n:1的变型。例如,对于新闻栏目NewsLabel,可以充当一方,即父栏目,也可以充当多方,即子栏目。而反映到DB表中,只有一张表,这张表中具有一个外键,用于表示该栏目的父栏目。一级栏目没有父栏目,所以可以将其外键值设为0,而子栏目则具有外键值。

将自关联分为两种情况。一种是当作1:n,即当前类作为一方,其包含多方的集合域属性。一种是当作n:1,即当前类作为多方,其包含一方的域属性

原文地址:https://www.cnblogs.com/rzbwyj/p/11668469.html