ResultMap的使用

在Mybatis中,可以使用resultMap(结果集映射)作为sql的返回类型

一般用来解决如下问题:

  • 数据库表字段名和实体类属性名不一致的问题;
  • 多对一问题:
    • 例如:多个学生对应同一个老师,查询每个学生信息(包含老师对象属性)
  • 一对多问题:
    • 例如:一个老师教学多个学生,查询某个老师信息及其属下学生(包含学生列表)

1、字段名和属性名不一致问题

数据库表字段名和实体类属性名不一致的问题处理

id	name	pwd	//数据库字段名
id name password //实体类属性名

Mapper中SQL语句编写如下:

<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column为数据库中的字段名,property为实体中的属性-->
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap> <select id="getUserById" resultType="UserMap">
select * from mybatis.user where id=#{id};
</select>

2、多对一处理

获得所有学生及其对应老师(多个学生对应一个老师)

Student类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Student{
int id;
String name;
Teacher teacher;
}

Teacher类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Teacher{
int id;
String name; public Teacher() {
} public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
}

按照结果集嵌套处理

联表查询

<select id="getStudents" resultMap="StudentMap">
select s.id sid, s.name sname, t.name tname
from student s
join teacher t
on s.tid = t.id
</select>
<resultMap id="StudentMap" type="Student">
<!--这里的column是与查询结果的字段名对应,字段重命名了则对应命名后的字段-->
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--复杂的属性需要单独处理 对象:association 集合:collection-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>

按照查询嵌套处理

相当于子查询:

<select id="getStudent2" resultMap="StudentMap2">
select * from student;
</select> <select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid};
</select> <!--这里将学生和对应老师分开查询,将查询结果组合-->
<resultMap id="StudentMap2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>

查询结果:

Student{id=1, name='zhangsan', teacher=Teacher{id=1, name='spong'}}
Student{id=2, name='lisi', teacher=Teacher{id=1, name='spong'}}

3、一对多处理

按指定ID查询老师及其所管理的学生(一个老师对应多个学生)

Student类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Student {
int id;
String name;
int tid; public Student() {
} public Student(int id, String name, int tid) {
this.id = id;
this.name = name;
this.tid = tid;
}
}

Teacher类

@Data                              //get,set
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Teacher {
int id;
String name;
List<Student> students;
}

按照结果嵌套处理

<select id="getTeacherById" resultMap="TeacherById">
select t.id id, t.name tname, s.id sid,s.name sname,s.tid tid
from teacher t
join student s
on t.id = s.tid;
</select> <resultMap id="TeacherById" type="Teacher">
<result property="id" column="id"/>
<result property="name" column="tname"/>
<!--获取List<Student>中的泛型使用 ofType-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>

查询结果:

[Teacher{id=1, name='spong', students=[Student{id=1, name='zhangsan', tid=1}, Student{id=2, name='lisi', tid=1}]}]

小结:

  • 关联:association【多对一】
  • 集合:collection【一对多】
  • javaType & ofType
    • javaType 用来指定实体类中属性的类型
    • ofType 用来指定映射到List或者集合中pojo类型,即泛型的类型

最新文章

  1. Spark Shuffle原理、Shuffle操作问题解决和参数调优
  2. Nagios监控ganglia的指标
  3. python(26)查看文件的大小
  4. GoF--命令设计模式
  5. 【HDOJ】1050 Moving Tables
  6. 写一个jq插件
  7. Codeforces 833E Caramel Clouds
  8. 微信小程序开发工具中快捷键
  9. 20175208 实验二 《Java面向对象程序设计》实验报告
  10. oracle更改字符集为zhs16GBK
  11. mysq存储金额的数值类型选择
  12. Unity3D中的生命周期函数
  13. 解决IDEA、Pycharm连接数据库乱码的问题
  14. Luogu 1613 跑路(最短路径,倍增)
  15. weblogic11G 修改密码
  16. WebJars are client-side web libraries (e.g. jQuery &amp; Bootstrap) packaged into JAR (Java Archive) files
  17. QC质量管理七大手法
  18. CSS滚动条样式设置
  19. 2013年6月编程语言排行榜,C语言位据第一位
  20. SCI 美国《科学引文索引》(Science Citation Index, 简称 SCI )

热门文章

  1. 太实用了!自己动手写软件——我们的密码PJ器终于完成了
  2. Azure Traffic Manager(二) 基于权重与基于优先级的路由策略为我们的Web项目提供负载均衡
  3. .Net Core in Docker极简入门(上篇)
  4. java基础(二)--main方法讲解
  5. python从放弃到放弃
  6. shell变量子串
  7. Java 线程池记录
  8. 最长不下降代码dp
  9. stringsream用法
  10. It还是高薪行业不?—软件测试