• mybatis 一对一 映射实体类、嵌套查询


    一对一

    在SysUser 类中增加SysRole字段。
    1、sql语句将role.role_name映射到role.roleName上。


    2、还可以在XML 映射文件中配置结果映射。
    <resultMap id="userRoleMap" type ="SysUser"〉
    <id property="id" column="id"/>
    <result property="userName" column="use_name"/>
    <result property="userPassword" column="user_password" />
    <result property="role.id" column ="role_id"/>
    <result property="role.roleName" column="role_name"/>
    <result property="role.createTime" column="role_create_time" jdbcType="TIMESTAMP"/>
    </resultMap>
    为了避免不同表中存在相同的列,如create_time, 在它前面增加了"role_"前缀。


    3、MyBatis是支持resultMap继承,因此可以简化上面的resultMap配置
    <resultMap id= "userRoleMap" extends= "userMap" type= "SysUser">
    <result property= "role.id" column="role_id "/>
    <result property= "role.roleName" column= "role_name"/>
    <result property= "role.createTime" column ="role_create_time" jdbcType= "TIMESTAMP"/>
    </resultMap>


    4、使用resultMap的association标签配置
    <resultMap id= "userRoleMap" extends= "userMap" type= "SysUser">
    <association property="role" columnPrefix="role_" javaType="SysRole">
    <result property= "id" column="role_id "/>
    <result property= "roleName" column= "role_name"/>
    <result property= "createTime" column ="create_time" jdbcType= "TIMESTAMP"/>
    </association>
    </resultMap>
    association标签的属性property对应实体类中的属性名,必填项。另外我们还配置了columnPrefix="role_",在写SQL的时候,和sys_role表相关的查询列的别名都要有"role_"前缀,在内部result配置column时,需要去掉前缀。sql:r.id role_id, r.role_name role_role_name, r.create_time role_create_time。


    5、使用association 配置时还可以使用resultMap 属性配置成一个已经存在的resultMap
    <resultMap id= "roleMap" type = "SysRole">
    <id property="id" column="id"/>
    <result property="roleName" column= "role_name"/>
    <result property="createTime" column="create_time" jdbcType = "TIMESTAMP"/>
    </resultMap>
    <resultMap id="userRoleMap" extends= "userMap" type="SysUser">
    <association property="role" columnPrefix="role_" resultMap="roleMap"/>
    </resultMap>
    MyBatis 默认会给 roleMap 添加当前命名空间的前缀,代码如下,test.mybatis.simple.mapper.UserMapper.roleMap。目前的 roleMap 是写在UserMapper.xml中的,其实更合理的位置应该是在RoleMapper.xml中。将roleMap移动到RoleMapper.xml中后,这里的userRoleMap就不能简单地指定为roleMap了,而是要修改为以下的样子。
    <resultMap id="userRoleMap" extends= "userMap" type="SysUser">
    <association property="role" columnPrefix="role_" resultMap="test.mybatis.simple.mapper.RoleMapper.roleMap"/>
    </resultMap>
    上面的情况都是一种情况,这种方式的好处是减少数据库查询次数,减轻数据库的压力。缺点是由于要在应用服务器上将结果映射到不同的类上,因此也会增加应用服务器的压力。当一定会使用到嵌套结果,并且整个复杂的SQL执行速度很快时,建议使用这种方法。


    还可以利用简单的SQL 通过多次查询转换为我们需要的结果,最后会将结果组合成一个对象。

    <resultMap id = "userRoleMapSelect" extends = "userMap" type= "SysUser">
    <association property="role" column="{id=role_id}" select="test.mybatis.simple.mapper.RoleMapper.selectRoleById" />
    </resultMap>
    <select id="selectUserAndRoleByIdSelect" resultMap="userRoleMapSelect">
    select u.id, u.user_name, u.user_password, u.user_email, u.user_info,u.head_img, u.create_time,
    ur.role_id
    from sys_user u
    join sys_user_role ur on u.id = ur.user_id
    where u.id= #{id}
    </select>
    association 属性 select :另一个查询的id, MyBatis 会额外执行这个查询。
    column :列名(或别名),将主查询中列的结果作为嵌套查询的参数,配置方式如 column = {propl=coll , prop2=col2}。
    fetchType :数据加载方式,可选值为lazy 和eager,分别为延迟加载和积极加载,这个配置会覆盖全局的lazyLoadingEnabled 配置。

    问题:如果主查询结果不是1条数据,而是N条数据,那就会出现N+1问题。主SQL 会查询一次,查询出N 条结果,这N条结果要各自执行一次查询,那就需要进行N次查询。


    解决办法:fetchType="lazy"
    <resultMap id = "userRoleMapSelect" extends = "userMap" type= "SysUser">
    <association property="role" column="{id=role_id}"
      select="test.mybatis.simple.mapper.RoleMapper.selectRoleById" fetchType="lazy"/>
    </resultMap>
    <select id="selectUserAndRoleByIdSelect" resultMap="userRoleMapSelect">
      select u.id, u.user_name, u.user_password, u.user_email, u.user_info,u.head_img, u.create_time,
      ur.role_id
    from sys_user u
      join sys_user_role ur on u.id = ur.user_id
      where u.id= #{id}
    </select >

  • 相关阅读:
    核心编程笔记8——用户模式下的线程同步
    核心编程随笔7——线程调度和优先级
    深入浅出mfc随笔——MFc程序的生死因果
    opengl
    核心编程6——线程
    深入浅出mfc学习笔记——六大关键技术之仿真_运行时和动态创建
    深入浅出mfc学习笔记——六大关键技术仿真_Persistence(永久保存)
    Gdi+编程
    深入浅出mfc学习笔记1
    file open等待事件
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9349344.html
Copyright © 2020-2023  润新知