简单映射
结果映射是将结果集中映射到java对象中
当我们数据库中的字段和实体类的属性名字不一致的时候,我们就需要使用映射来将数据库中的字段与实体类中的属性一一对应起来,方便我们之后的查找数据以及相关操作
resultMap标签是用来声明映射的,其中type指向的是对应的实体类。
property的值为实体类的属性,column为数据库中与之对应的字段
<resultMap type="jee.pk2.User" id="userResultMap">
<id property="id" column="UserId"/>
<result property="name" column="userName"/>
<result property="age" column="userage"/>
</resultMap>
映射继承
当两个映射中有大量的重复映射关系,这个时候我们就可以使用映射之间的继承关系。 从而减少不必要的代码。注意id为这个表的主键
<resultMap type="jee.pk2.User" id="userResultMap">
<id property="id" column="UserId"/>
<result property="name" column="userName"/>
<result property="age" column="userage"/>
</resultMap>
<resultMap type="jee.pk2.UserDetail" id="userDetailResultMap">
<id property="id" column="UserId"/>
<result property="name" column="userName"/>
<result property="age" column="userage"/>
<result property="gender" column="userGender"/>
</resultMap>
上面两个映射中有三个映射关系是重复的,这时候下面的映射只需要将上面的映射继承下来写成如下形式即可
<resultMap type="jee.pk2.UserDetail" id="userDetailResultMap" extends="userResultMap">
<result property="gender" column="userGender"/>
</resultMap>
一对一映射
一般表和表之间都是由联系的,表与表之间的联系一般都是通过外键来建立链接。当我们查询的结果需要两个表的信息的时候,我们就需要整合信息,这时候我们的映射关系如下所示。
每一个表都有一个映射关系,第一个映射对应user表
第二个映射对应Address表,两个表倚靠id(外键)链接
当我们需要将user信息与Address(住址信息)信息整合到一起的时候。
我们首先写出两个表的查询方法。
然后再user的映射关系的Address字段设置select属性,值为查询address信息的select标签的id。column属性的值为外键字段
<?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="jee.pk3.UserMapper">
<resultMap type="jee.pk3.User" id="userResultMap">
<id property="id" column="u_id"/>
<result property="name" column="u_name"/>
<result property="age" column="u_age"/>
<!--当查询该属性的时候,调用id为getUserAddress的select标签的查询方法,查询参数为id-->
<association property="address" select="getUserAddress" column="a_id"></association>
</resultMap>
<resultMap type="jee.pk3.Address" id="userAddressResultMap">
<id property ="id" column="a_id"/>
<result property="province" column="a_provincen"/>
<result property="city" column="a_city"/>
<result property="street" column="a_street"/>
<result property="num" column="a_num"/>
</resultMap>
<select id="get" parameterType="int" resultMap="userResultMap">
select * from t_users where u_id=#{id}
</select>
<select id="getUserAddress" resultMap="userAddressResultMap">
select * from t_address where u_id=#{id}
</select>
</select>
</mapper>
一对多映射
一对多映射是将一个映射关系,作为参数传递给另外一射映射。
比如下面这个两个映射关系。
<?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="jee.pk1.ClazzMapper">
<resultMap type="jee.pk1.Clazz" id="clazzResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="students" resultMap="studentResultMap"></association>
</resultMap>
<resultMap type="jee.pk1.Student" id="studentResultMap">
<id property="id" column="S_id" ></id>
<result property="name" column="s_name"/>
<result property="sno" column="s_no"/>
<result property="gender" column="s_gender"/>
</resultMap>
<!--当调用该查询的时候使用clazzResultMap映射,但是students信息使用studentResultMap映射关系。
<select id="get" resultMap="clazzResultMap" parameterType="int">
select c.*,s.s_id,s.s_name,s.s_no,s.s_gender from t_class c inner join t_student s on c.c_id=s.c_id where c.c_id=#{id}
</select>
</mapper>