MyBatis中实现多表查询

一、

1、Mybatis是实现多表查询方式

  1.1  业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果合并

  1.2  使用Auto Mapping 特性,在实现两表联合查询时通过别名完成映射

  1.3  使用MyBatis<resultMap>属性进行实现

2、多表查询时,类中包含另一个类的对象的分类

  2.1 单个对象

  2.2 集合对象

二、resultMap属性

  1、<resultMap>标签写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系。

    1.2 默认MyBatis使用Auto Mapping特性

  2、使用<resultMap> 标签时,<select>标签不写resultType属性,而是使用resultMap属性 引用<resultMap>标签

  3、使用resultMap实现单表映射关系

    3.1 数据库设计

  

     3.2 实体类设计

    

      3.3 xxxmapper.xml代码

 1  <mapper namespace="com.bjsxt.mapper.TeacherMapper">
 2      <resultMap type="Teacher" id="mymap">
 3          <!-- 主键使用id标签配置映射关系-->
 4          <id column="id" property="id1"/>
 5          <!-- 其他列使用result标签配置映射关系 -->
 6          <result  column="name" property="name1"/>
 7      </resultMap>
 8      <select id="selall"  resultMap="mymap">
 9          select * from teacher
10      </select>
11  </mapper>

猜你喜欢

转载自www.cnblogs.com/axu521/p/10109766.html