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中加上%传过去。

  • 相关阅读:
    规约先行-(六)并发处理
    MySQL选择合适的方式存储时间
    规约先行-(五)集合处理
    规约先行-(四)OOP 规约
    12.20-LaTex git workflow
    6.25-ROS 软件度量
    6.19-rosdoc_lite and 文档构建工具
    12.27-ros-decision making
    12.3-分级并发有限状态机-SMACH
    12.07-rostest学习
  • 原文地址:https://www.cnblogs.com/dslnn/p/8648897.html
Copyright © 2011-2022 走看看