mybatis.一对一,一对多

  • 使用mybatis一对多:
    • 使用collection 标签:
      collection属性:
          property: 指的是集合属性的值.
          ofType:指的是集合中元素的类型.
           <collection property="products" ofType="com.xx.xxx"></collection>
      
      例:在resultMap标签中
      	         <collection property="teachers" ofType="com.xxx.xxxx.products">
      			<id property="tid" column="id"/>
      			<result property="tname" column="tname"/>
      		</collection> 
      注意:查询的时候如果关联表字段重复需要重命名.
         例如:<id property="tid" column="id"/>
      
          <resultMap id="userMap" type="com.xxx.xxx.User">
              <id property="id" column="id"/>
              <result property="username" column="username"/>
              <result property="birthday" column="birthday"/>
              <result property="sex" column="sex"/>
              <result property="address" column="address"/>
              <collection property="roles" ofType="com.example.domain.Role" resultMap="roleMap"/>
          </resultMap>
          <resultMap id="roleMap" type="com.xxx.xxx.Role">
              <id property="roleId" column="rid"/>
              <result property="roleName" column="ROLE_NAME"/>
              <result property="roleDesc" column="ROLE_DESC"/>
          </resultMap>
          <select id="findAll" resultMap="userMap">
             SELECT u.*,r.ID as rid,r.ROLE_DESC,r.ROLE_NAME FROM user u
               LEFT OUTER JOIN user_role ur on u.id = ur.UID
                LEFT OUTER JOIN role r on ur.RID = r.ID
          </select>
      

       

  • 使用mybatis一对一

    • 使用association标签
      property: 指的是集合属性的值.
      javaType:指定关联的类型  
        <association property="People" javaType="com.xxx.xxx.user">
      

       

  • 分页查询 问题解决: https://blog.csdn.net/wszcy199503/article/details/77867990:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值