zoukankan      html  css  js  c++  java
  • mybatis一对多联表查询的两种常见方式

    1.嵌套结果查询(部分代码如下)

    sql语句接上:

                 

     注释:class表(c别名),student表teacher(t别名)teacher_id为class表的字段t_id为teacher表的字段,因为两者有主键关联的原因,c_id为class表的字段。根据c_id查询。_classes为classes实体类(既是表class对应的实体类)前两个property为class表对应实体类的属性,column对应class表的字段,association中teacher为classes实体类属性,column为class表属性,javaType为teacher实体类(既是表teacher的实体类)。后面的id为teacher实体类的id column为teacher表的字段,result 中的name同理。

    Collection中的id,name为student类属性,column中为student表中的字段。

    对比一对一嵌套结果查询区别:*查询语句多出c_id=s.class_id(class_id为student表字段)

                                                     **多个collection其中的字段属性同上,不过没有column因为class表中没设计student表的id。

    2.嵌套语句查询(部分代码如下)

     

        

      注释:同上,其中collection中的column字段值为class表的字段,因为需要为查询student提供id,多个select。注意:当查询的结果需要封装时,select语句后不能再使用*查询,需要使用表别名加字段名

     【小插曲】模糊查询:

      

          Dao中

        

        注释:因为xml配置文件中sql语句只能识别#{name}这样可以在dao中加上%传过去。

  • 相关阅读:
    【LeetCode】006 ZigZag Conversion
    【LeetCode】009 Palindrome Number
    【LeetCode】008 String to Integer (atoi)
    【LeetCode】012 013 Roman Integer
    react-native 入门资源合集
    Thread和ExecutorService(一)
    DrawerLayout、CoordinatorLayout、CollapsingToolbarLayout的使用--AndroidSupportDesign练手
    Valid Number
    When Is Cheryl's Birthday
    【笔试】——常用运算符
  • 原文地址:https://www.cnblogs.com/dslnn/p/8648897.html
Copyright © 2011-2022 走看看