• 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都是查询结果集的别名

  • 相关阅读:
    章节1:SQL语言简易入门
    章节0:MySQl学前知识储备
    iOS 设置导航栏全透明
    IOS修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
    iOS import导入时没有提示的解决办法
    iOSAPP开发项目搭建
    如何搭建iOS项目基本框架
    UIWebView中JS与OC交互 WebViewJavascriptBridge的使用
    iOS概念之KVO(Key-Value Observing)
    oc调javascript方法(evaluateJavaScript:)&&js给oc发通知
  • 原文地址:https://www.cnblogs.com/haimishasha/p/5710574.html
Copyright © 2020-2023  润新知