嵌套查询(一条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>