目录
一、resultMap处理字段和属性的映射关系
如果字段名和实体类中的属性名不一致的情况下,可以通过resultMap设置自定义映射。
1、可以通过为字段起别名的方式,别名起成和属性名一致。保证字段名和实体类中的属性名一致
<select id="getEmpByEmpId" resultType="emp">
select emp_id empId,emp_name empName,age,sex from emp where emp_id = #{empId}
</select>
2、如果字段名和实体类中的属性名不一致的情况下,但是字段名符合数据库的规则(使用_),实体类中使用的属性名符合java的规则(使用驼峰命名),可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中的数据时,自动将带下划线“_”的字段名转为驼峰命名
user_name:userName
emp_id:empId
<settings>
<!--将数据库字段名的下划线映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--Emp getEmpByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpByEmpId" resultType="emp">
select * from emp where emp_id = #{empId}
</select>
⑶使用resutlMap自定义映射处理
<!--
resultMap:设置自定义的映射关系
id:唯一标识
type:处理映射关系的实体类的类型 一般使用MyBatis的别名
常用的标签
id:处理主键和实体类中属性的映射关系
result:处理普通字段和实体类中属性的映射关系
column:设置映射关系中的字段名,必须是sql查询出的某个字段
property:设置映射关系中的属性的属性名,必须是处理的实体类型中的属性名
-->
<resultMap id="empReslutMap" type="emp">
<id property="empId" column="emp_id"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</resultMap>
二、一对一映射处理
1、级联方式处理
在select标签中添加自定义映射,标签内写连表查询语句,在select标签下创建resultMap标签,id名和select中的resultMap对应,类型写需要指定映射的对象类型
resultMap标签下可以创建六种标签,<id>用于添加数据库表中主键的映射关系,<result>用于添加除了主键之外的其他列名的映射关系,此外还有<association>、<collection>、<discriminator>、<consturctor>四种标签,之后会讲解它们的作用,在级联方式中,Teacher类型的属性可能会和Student类型属性重复,所以需要以对象名.属性名的方式创建Teacher表的映射关系(数据库表的列名不需要)创建完成后调用对应的方法,代码如下:
<select id="findStudentTeacherByStudentId" resultMap="StudentIdTeacher">
select student.*, teacher.Tname
from teacher,student
where student.t_id = teacher.id and student.id = #{id};
</select>
<resultMap id="StudentIdTeacher" type="com.qcby.entity.Student">
<id property="id" column="id"></id>
<result property="Sname" column="Sname"></result>
<result property="sex" column="sex"></result>
<result property="age" column="age"></result>
<result property="teacher.id" column="id"></result>
<result property="teacher.Tname" column="Tname"></result>
</resultMap>
Student findStudentTeacherByStudentId(@Param("id") Integer id);
@Test
public void findStudentTeacherByStudentId(){
Student studentTeacher = studentDao.findStudentTeacherByStudentId(2);
System.out.println(studentTeacher.toString());
}
2、association 方式处理
对于学生属性的映射关系处理,association方式和级联没有区别
<association>用来处理对象类型的属性,property放属性名,javaType放属性所对应的类型
在association标签内直接对应相关的属性和数据库表的列名即可
<select id="findStudentTeacherByAssociation" resultMap="AssociationStudentTeacher">
select student.*, teacher.Tname
from teacher,student
where student.t_id = teacher.id and student.id = #{id};
</select>
<resultMap id="AssociationStudentTeacher" type="com.qcby.entity.Student">
<id property="id" column="id"></id>
<result property="Sname" column="Sname"></result>
<result property="sex" column="sex"></result>
<result property="age" column="age"></result>
<result property="t_id" column="t_id"></result>
<association property="teacher" javaType="com.qcby.entity.Teacher">
<result property="id" column="id"></result>
<result property="Tname" column="Tname"></result>
</association>
</resultMap>
Student findStudentTeacherByAssociation(@Param("id") Integer id);
@Test
public void findStudentTeacherByAssociation(){
Student studentTeacherByAssociation = studentDao.findStudentTeacherByAssociation(2);
System.out.println(studentTeacherByAssociation.toString());
}
3、分布查询方式处理
- 分步查询原理:先执行
StudentMapper
中的findStudentTeacherByTwoStep
查询,获取学生信息,同时得到 t_id。然后根据 t_id调用TeacherMapper中的findTeacherById
方法查询教师信息。 association
标签:用于处理一对一关联。property
表示Student
类中的特殊属性;column
是将查询出的 t_id作为参数传递给 TeacherMapper的方法;select
指定要调用的TeacherMapper中的方法。
<select id="findTeacherById" resultType="com.qcby.entity.Teacher" parameterType="java.lang.Integer">
select * from teacher where id = #{id}
</select>
<select id="findStudentTeacherByTwoStep" resultMap="TwoStepStudentTeacher">
select * from student where student.id = #{id};
</select>
<resultMap id="TwoStepStudentTeacher" type="com.qcby.entity.Student">
<id property="id" column="id"></id>
<result property="Sname" column="Sname"></result>
<result property="sex" column="sex"></result>
<result property="age" column="age"></result>
<result property="t_id" column="t_id"></result>
<association property="teacher" javaType="com.qcby.entity.Student" column="t_id" select="com.qcby.dao.TeacherDao.findTeacherById"></association>
</resultMap>
Student findStudentTeacherByTwoStep(@Param("id") Integer id);
@Test
public void TwoStepStudentTeacherByTwoStep(){
Student studentTeacherByTwoStep = studentDao.findStudentTeacherByTwoStep(2);
System.out.println(studentTeacherByTwoStep.toString());
}