首页 > 其他 > 详细

MyBatis关联查询

时间:2019-10-12 18:12:51      阅读:174      评论:0      收藏:0      [点我收藏+]

一.一对多

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.测试类

 技术分享图片

MyBatis关联查询

原文:https://www.cnblogs.com/mayuan01/p/11662905.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!