mybatis关联查询

嵌套查询(一条sql,多表联查):

<!-- 一对一嵌套查询 -->
<association property="abc" javaType="xxx.Abc">
    <id property="id" column="id" />
    <result property="name" column="name" />
</association>

<!-- 一对多嵌套查询 -->
<collection property="xxxList" ofType="xxx.AbcBean">
    <id property="id" column="id" />
    <result property="name" column="name" />
</collection>

                 

嵌套结果(查询中调用另一条sql):

<!-- 一对一嵌套结果 fetchType默认eager-->
<association property="abc" javaType="xxx.AbcBean" column="abc_id" select="xxx.AbcMapper.selectById" fetchType="lazy"/>

<!-- 一对多嵌套结果 -->
<!-- ofType属性与assocaiation中javaType属性对应,用于指定实体对象中集合类属性所包含的元素类型 -->
<collection property="abcList" ofType="xxx.Abc" column="abc_id" select="xxx.AbcMapper.selectById" />

分布查询涉及多个字段column="{c1=xx,c2=xx}", c1 c2为第二步查询参数 xx为第一步查询结果列名称

可以配置fetchType="lazy"懒加载, 分步查询时在不使用关联表信息的时候不进行查询的目的

ResultMap可以配置extend属性

bug:

java.lang.IllegalArgumentException: At least one base package must be specified

启动类添加注解@MapperScan("...")

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zhu.mapper.SysUserMapper.selectByPrimaryKey

SysUserMapper.xml文件在发布时不会编译,没有发布到target中,导致项目运行时找不到mapper.xml文件。

<!--打包时fu复制Mybatis的映射文件-->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/mapper/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiha_zhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值