zoukankan      html  css  js  c++  java
  • mybatis_SQL映射(3)

    文章摘录自:http://blog.csdn.net/y172158950/article/details/17304645

    1. 表关联

    a) 嵌套查询(传说中的1+N问题)
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <resultMap id="userResult3" type="User">  
    2.     <association property="role" column="role_id" javaType="Role" select="selectRole"/>  
    3. </resultMap>  
    4.       
    5. <select id="selectUser2" parameterType="int" resultMap="userResult3">  
    6.     select _id id, _name name, _password password, _role_id role_id from _user where _id = #{id};  
    7. </select>  
    8. <select id="selectRole" parameterType="int" resultType="Role">  
    9.     select _id id, _name name, _grade grade from _role where _id = #{id};  
    10. </select>  
    i. 是role_id[sleect结果集column别名],不是_role_id[_user表column]。
    b) 嵌套结果
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <resultMap id="userResult" type="User">  
    2.     <constructor>  
    3.         <idArg column="u_id" javaType="int"/>  
    4.     </constructor>  
    5.     <result property="name" column="u_name" />  
    6.         <result property="password" column="u_password" />  
    7.     <association property="role" column="r_id" javaType="Role">  
    8.         <id property="id" column="r_id"/>  
    9.         <result property="name" column="r_name"/>  
    10.         <result property="grade" column="r_grade"/>  
    11.     </association>  
    12. </resultMap>  
    13.   
    14. <select id="selectUser" parameterType="int" resultMap="userResult">  
    15.     select u._id u_id, u._name u_name, u._password u_password,   
    16.     r._id r_id, r._name r_name, r._grade r_grade from _user u   
    17.     left join _role r on u._role_id=r._id where u._id =#{id};  
    18. </select>  
    i. 别名的意义:每个字段名称必须唯一,重名的情况会照成错误的返回。[太土了]
    ii.蛋疼的构造方法:<constructor>定义了javabean的构造方法
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public User(Integer id) {  //此处必须写Integer,写int报错  
    2.     this.id = id;  
    3. }  
    iii. 另外一种写法:resultMap的重用
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <resultMap id="userResult" type="User">  
    2.     <constructor>  
    3.         <idArg column="u_id" javaType="int"/>  
    4.     </constructor>  
    5.     <result property="name" column="u_name" />  
    6.     <result property="password" column="u_password" />  
    7.     <association property="role" column="r_id" resultMap="roleResult">  
    8.     </association>  
    9. </resultMap>  
    10.   
    11. <resultMap id="roleResult" type="Role">  
    12.     <id property="id" column="r_id" />  
    13.     <result property="name" column="r_name"/>  
    14.     <result property="grade" column="r_grade"/>  
    15. </resultMap>  

    2. 集合的用法

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <select id="selectUser4" parameterType="int" resultType="User">  
    2.      select _id id, _name name, _password password, _role_id role_id from _user where _role_id = #{id};  
    3. </select>  
    4. <select id="selectRole4" parameterType="int" resultMap="selectRole4">  
    5.      select _id id, _name name, _grade grade from _role where _id = #{id};  
    6. </select>  
    7. <resultMap id="selectRole4" type="Role">  
    8.      <collection property="users" column="id" ofType="User" select="selectUser4"/>  
    9. </resultMap>  

    i. 仍要注意,resultMap中column都是查询结果集的别名

  • 相关阅读:
    PL/SQL不安装ORACLE客户端
    C#特性的学习(一)
    Centos运行Mysql因为内存不足进程被杀
    ASP.NET Core 新核心对象WebHost(一)
    ASP.NET Core轻松入门之Configure中IHostingEnvironment和IApplicationLifetime的使用
    Asp.Net Core轻松入门之WebHost的配置
    asp.net core轻松入门之MVC中Options读取配置文件
    ASP.NET Core轻松入门Bind读取配置文件到C#实例
    ASP.NET CORE入门之读取Json配置文件
    ASP.NET Core MVC中构建Web API
  • 原文地址:https://www.cnblogs.com/haimishasha/p/5710574.html
Copyright © 2011-2022 走看看