1.使用association标签
由select里的resultMap来调用resultMap标签的方法,调用如图
<?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="cn.itsource._02_many2one.DomainMapper">
<!-- 自动匹配不上,进行手动匹配 -->
<resultMap type="cn.itsource._02_many2one.User" id="userMap">
<!-- 手动配置属性值 -->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<!--
association:配置对象关联的多列(当使用这个标签时,所有默认映射全部失效,需要手动配置)
javaType:这个属性对应的类型 property:对象的属性名称
-->
<association property="dept" javaType="cn.itsource._02_many2one.Dept">
<!-- 填写关联对象的属性
property="id"/name 是指dept里的对象的ID与name
column="id"/name 是指sql的结果集中的ID与name
-->
<id property="id" column="did" />
<result property="name" column="dname" />
</association>
</resultMap>
<select id="getUsers" resultMap="userMap">
select u.id,u.name,u.password,d.id did ,d.name dname from t_user u left join t_dept d on u.dept_id= d.id
</select>
<insert id="saveDept" parameterType="cn.itsource._02_many2one.Dept" useGeneratedKeys="true" keyProperty="id">
insert into t_dept (name) values(#{name})
</insert>
<insert id="saveUser" parameterType="cn.itsource._02_many2one.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user (name,password,dept_id) values (#{name},#{password},#{dept.id})
</insert>
</mapper>
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="cn.itsource._02_many2one.DomainMapper">
<!-- 这是嵌套结果 -->
<!-- 自动匹配不上,进行手动匹配 -->
<resultMap type="cn.itsource._02_many2one.User" id="userMap">
<!-- 手动配置属性值 -->
<result property="dept.id" column="did"/>
<result property="dept.name" column="dname"/>
</resultMap>
<select id="getUsers" resultMap="userMap">
select u.id,u.name,u.password,d.id did ,d.name dname from t_user u left join t_dept d on u.dept_id= d.id
</select>
<insert id="saveDept" parameterType="cn.itsource._02_many2one.Dept" useGeneratedKeys="true" keyProperty="id">
insert into t_dept (name) values(#{name})
</insert>
<insert id="saveUser" parameterType="cn.itsource._02_many2one.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user (name,password,dept_id) values (#{name},#{password},#{dept.id})
</insert>
</mapper>
3.使用嵌套查询
原理跟图一一样
<?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="cn.itsource._02_many2one.DomainMapper">
<!-- 这是嵌套查询 -->
<!-- 自动匹配不上,进行手动匹配 -->
<resultMap type="cn.itsource._02_many2one.User" id="userMap">
<!-- 手动配置属性值 -->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<!-- property 对应的是user里的dept column 对应数据库里的列名-->
<association property="dept" column="dept_id" select="getDeptById"/>
</resultMap>
<!-- 根据ID查询到一个部门 -->
<select id="getDeptById" parameterType="int" resultType="cn.itsource._02_many2one.Dept">
select * from t_dept where id = #{dept_id}
</select>
<select id="getUsers" resultMap="userMap">
select * from t_user
</select>
<insert id="saveDept" parameterType="cn.itsource._02_many2one.Dept" useGeneratedKeys="true" keyProperty="id">
insert into t_dept (name) values(#{name})
</insert>
<insert id="saveUser" parameterType="cn.itsource._02_many2one.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user (name,password,dept_id) values (#{name},#{password},#{dept.id})
</insert>
</mapper>