MyBatis关联查询

一.一对多

1.entity 实体类

public class SmbmsRole {
    private Integer rid;
    private String roleCode;
    private String roleName;
    private BigInteger createdBy;
    private Date creationDate;
    private BigInteger modifyBy;
    private Date modifyDate;
    //植入多的一方            集合
    private List<SmbmsUser> userList;
}
public class SmbmsUser {
    private Integer id;
    private String userCode;
    private String userName;
    private String userPwd;
    private Integer gender;
    private Date birthday;
    private String phone;
    private String address;
    private Integer userRole;
    private Integer createdBy;
    private Date creationDate;
    private Integer modifyBy;
    private Date modifyDate;
}

二.dao层

 三.小配置.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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.my.dao.ISmbmsRoleDao">
    <resultMap id="roleAndUserMapper" type="com.my.entity.SmbmsRole">
        <!--property代表实体类当中的属性    column代表数据库当中的字段-->
        <id column="rid" property="rid"/> <!--主键-->
        <result property="roleName" column="roleName"/>
        <!--映射多的一方  property代表实体当中多的一方的属性名  ofType代表和当中泛型类型-->
        <collection property="userList" ofType="SmbmsUser" select="getRoleAndUserMutilSQL" column="rid">
            <!--<id column="id" property="id"/>
            <result column="userName" property="userName"/>-->
        </collection>
    </resultMap>

    <!--<select id="'getRoleAndUser" resultMap="roleAndUserMap">
        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 and r.rid=#{id}
    </select>-->
    <select id="getRoleAndUser" resultMap="roleAndUserMapper">
        select * from smbms.smbms_role where rid=#{id}
    </select>
    <select id="getRoleAndUserMutilSQL" resultType="com.my.entity.SmbmsUser">
        select * from smbms.smbms_user where userRole=#{rid}
    </select>
</mapper>

四、test测试类

 二.多对一

1.entity实体类

 2.dao层

3.小配置.xml

4.测试类

三.多对多

1.entity实体类

 2.dao层

 3.小配置.xml

4.测试类

四.自关联

1.entity实体类

2.dao层

3.小配置.xml

4.测试类

 

posted @ 2019-10-12 17:31  林染plus  阅读(278)  评论(0编辑  收藏  举报