一 解决属性和表的字段不一致的两种方法
1. select xx as yy
2. resultMap column property
resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的数千行代码。ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。
之前你已经见过简单映射语句的示例,它们没有显式指定 resultMap。比如:
<select id="selectUsers" resultType="map">
select id, username, hashedPassword
from some_table
where id = #{id}
</select>
上述语句只是简单地将所有的列映射到 HashMap 的键上,这由 resultType 属性指定。虽然在大部分情况下都够用,但是 HashMap 并不是一个很好的领域模型。你的程序更可能会使用 JavaBean 或 POJO(Plain Old Java Objects,普通老式 Java 对象)作为领域模型
测试样例
注意(重要!!!):这里的参数id 因为是基本数据类型,并且只有一个,所以
select * from user where id = #{id}
能找到传入的参数 和id匹配。
问题: 如果传入的参数是id和age怎么办?都是int类型,怎么区分呢?
答:
//这里的标签@Param指定了xml的sql去找哪个变量,如果@Param("uid"),那么xml是where id = #{uid} 否则找不到
public User getUserByIdAndAge(@Param("id") int id,@Param("age") int age);
高级映射多表查询 两种情况:多对一 (association) 一对多(collection)
数据表
studnet
teacher
多对一
说明:
1 表之间的依赖关系,student通过tid依赖teacher
2 实体类,student通过Teacher teacher对象依赖teacher
1、创建pojo
Student.java
public class Student {
private int id;
private String name;
private Teacher teacher;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + teacher +
'}';
}
}
Teacher.java
package com.mybatis.pojo;
public class Teacher {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
StudentMapper.xml(两种方式查询,1 子查询,2 结果嵌套查询)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.StudentMapper">
<!--多对一 对象association-->
<!--方式1 子查询-->
<select id="getStudentList" resultMap="studentMap">
select * from student
</select>
<resultMap id="studentMap" type="Student">
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
<!--方式2 按照结果嵌套查询-->
<select id="getStudentList2" resultMap="studentMap2">
select s.id sid, s.name sname, t.id tid,t.name tname from student s,teacher t where t.id=s.tid
</select>
<resultMap id="studentMap2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
一对多
说明:
1 表之间的依赖关系,student通过tid依赖teacher
2 实体类,teacher通过List studentList集合依赖student
1、创建pojo
Student.java
package com.mybatis.pojo;
public class Student {
private int id;
private String name;
private int tid;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Teacher.java
package com.mybatis.pojo;
import java.util.List;
public class Teacher {
private int id;
private String name;
private List<Student> studentList;
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", studentList=" + studentList +
'}';
}
}
TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.TeacherMapper">
<select id="getTeacherList" resultType="Teacher">
select * from teacher;
</select>
<!--一对多 获取某老师下的所有学生集合collection-->
<select id="getTeacherById" resultMap="TeacherMap">
select t.id tid,t.name tname,s.id sid,s.name sname from teacher t,student s where s.tid=t.id and tid=#{id}
</select>
<resultMap id="TeacherMap" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="studentList" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
</collection>
</resultMap>
</mapper>