MyBatis(6) 结果映射(resultMap)

为什么要使用结果映射??             --> 关联对象查询->多表查询要使用

 

关联映射处理方式

嵌套结果:使用嵌套查询把所有结果一次查出

嵌套查询 : 多次查询,合并结果


先放代码分析流程:

多对一嵌套查询方式:

多对一嵌套结果方式:

一对多 嵌套查询方式:

一对多 嵌套结果方式:


注意:这里bean类就统一不放了...    【注意要在MyBatis的核心配置文件中引入beanMapper映射文件哦~~】


多对一: 嵌套查询

beanMapper接口:

public interface EmployeeMapper {
    List<Employee> findAll();
}

beanMapper映射文件:

<?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.zhengqing.mybatis._04manytoone01.EmployeeMapper"> <!--全限定mapper名-->
    <!-- 多对一: 嵌套查询  -》 1+n条sql【即发送1条sql查询employee+n条sql查询department 映射文件Mapper结果自动封装ResultMap】 -->
    <select id="findAll" resultMap="EmployeeMap">
        select e.id ,e.username,e.hobby,e.dept_id from t_employee e
    </select>

    <!-- id:唯一标识   type:resultMap的封装结果 -》 就是把数据库的列封装到一个对象的属性上去对应
         resultMap:使用在数据库的列和bean类的属性不一致的时候 -->
    <resultMap id="EmployeeMap" type="com.zhengqing.mybatis._04manytoone01.Employee">
        <!-- ①先封装主键 column:数据库的列 property:bean类的属性 目的:让它们互相对应,一一关系 -->
        <id column="id" property="id" />
        <!-- ②再封装非主键 -->
        <result column="username" property="username" />
        <result column="hobby" property="hobby" />
        <!-- ③封装关联属性   association:封装一个property 封装成javaType -->
        <association property="department" javaType="com.zhengqing.mybatis._04manytoone01.Department"
        select="findOneById" column="dept_id"></association>
    </resultMap>

    <select id="findOneById" parameterType="long" resultType="com.zhengqing.mybatis._04manytoone01.Department">
        select * from t_department where id = #{id}
    </select>

</mapper>

测试代码:

public class EmployeeMapperTest {
    @Test
    public void findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MybatisUtil.getSqlSession();
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            System.out.println(employeeMapper.findAll());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

运行结果:


多对一:  嵌套结果

beanMapper接口:

public interface EmployeeMapper {
    List<Employee> findAll();
}

beanMapper映射文件:

<?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.zhengqing.mybatis._05manytoone02.EmployeeMapper">
    <!-- 多对一:  嵌套结果:只发送1条关联sql 映射文件Mapper结果手动封装ResultMap -->
    <select id="findAll" resultMap="EmployeeMap">
        select
            e.id,e.username,e.dept_id,e.hobby,d.id did,d.name dname
        from
            t_employee e
        left join t_department d on e.dept_id = d.id
    </select>

    <!-- id:唯一标识   type:resultMap的封装结果 -》 就是把数据库的列封装到一个对象的属性上去对应 -->
    <resultMap id="EmployeeMap" type="com.zhengqing.mybatis._05manytoone02.Employee">
        <!-- ①先封装主键 column:数据库的列 property:bean类的属性 目的:让它们互相对应,一一关系 -->
        <id column="id" property="id" />
        <!-- ②再封装非主键 -->
        <result column="username" property="username" />
        <result column="hobby" property="hobby" />
        <!-- ③封装关联属性   association:封装一个property 封装成javaType -->
        <association property="department" javaType="com.zhengqing.mybatis._05manytoone02.Department">
            <id column="did" property="id" />
            <result column="dname" property="name" />
        </association>
    </resultMap>
</mapper>

测试代码:

public class EmployeeMapperTest {
    @Test
    public void findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MybatisUtil.getSqlSession();
            EmployeeMapper employeeMapeper = sqlSession.getMapper(EmployeeMapper.class);
            System.out.println(employeeMapeper.findAll());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

运行结果:


一对多: 嵌套查询

beanMapper接口:

public interface DepartmentMapper {
    List<Department> findAll();
}

beanMapper映射文件:

<?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.zhengqing.mybatis._06onetomany01.DepartmentMapper"> <!--全限定mapper名-->
    <!-- 一对多: 嵌套查询  -》 1+n条sql【即发送1条sql查询employee+n条sql查询department 映射文件Mapper结果自动封装ResultMap】 -->
    <select id="findAll" resultMap="DepartmentMap">
        select d.id,d.name from t_department d
    </select>

    <!-- id:唯一标识   type:resultMap的封装结果 -》 就是把数据库的列封装到一个对象的属性上去对应
         resultMap:使用在数据库的列和bean类的属性不一致的时候 -->
    <resultMap id="DepartmentMap" type="com.zhengqing.mybatis._06onetomany01.Department">
        <!-- ①先封装主键 column:数据库的列 property:bean类的属性 目的:让它们互相对应,一一关系 -->
        <id column="id" property="id" />
        <!-- ②再封装非主键 -->
        <result column="name" property="name" />
        <!-- ③封装关联属性 property:集合数据 ofType:集合中元素类型 -->
        <collection property="list" javaType="list" ofType="com.zhengqing.mybatis._06onetomany01.Employee"
                    select="getEmpsByDeptId" column="id"></collection>
    </resultMap>

    <select id="getEmpsByDeptId" parameterType="long" resultType="com.zhengqing.mybatis._06onetomany01.Employee">
         select * from t_employee where dept_id = #{id}
    </select>

</mapper>

测试代码:

public class DepartmentMapperTest {
    @Test
    public void findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MybatisUtil.getSqlSession();
            DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
            System.out.println(departmentMapper.findAll());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

运行结果:


一对多: 嵌套结果

beanMapper接口:

public interface DepartmentMapper {
    List<Department> findAll();
}

beanMapper映射文件:

<?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.zhengqing.mybatis._07onetomany02.DepartmentMapper">
    <!-- 一对多: 嵌套结果:只发送1条关联sql 映射文件Mapper结果手动封装ResultMap -->
    <select id="findAll" resultMap="DepartmentMap">
         select d.id,d.name,e.id eid ,e.username eusername ,e.hobby ehobby,e.dept_id
         from t_department d
         left join t_employee e on e.dept_id = d.id
    </select>

    <!-- id:唯一标识   type:resultMap的封装结果 -》 就是把数据库的列封装到一个对象的属性上去对应 -->
    <resultMap id="DepartmentMap" type="com.zhengqing.mybatis._07onetomany02.Department">
        <!-- ①先封装主键 column:数据库的列 property:bean类的属性 目的:让它们互相对应,一一关系 -->
        <id column="id" property="id" />
        <!-- ②再封装非主键 -->
        <result column="name" property="name" />
        <!-- ③封装关联属性 property:集合数据 ofType:集合中元素类型 -->
        <collection property="list" javaType="list" ofType="com.zhengqing.mybatis._07onetomany02.Employee">
            <id column="eid" property="id"/>
            <result column="eusername" property="username"/>
            <result column="ehobby" property="hobby"/>
        </collection>
    </resultMap>
</mapper>

测试代码:

public class DepartmentMapperTest {
    @Test
    public void findAll() {
        SqlSession sqlSession=null;
        try {
            sqlSession= MybatisUtil.getSqlSession();
            DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
            System.out.println(departmentMapper.findAll());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

运行结果:

最后温馨小提示:掌握了这些代码流程,能写之后,就可以去了解MyBatis的代码生成器了,这个入门,主要是想让我们能在之后的代码生成器中看懂代码,怎么只能使用,才能让我们的学习更高效,其实有时候我们学习java,整理之后,可能就是一张图,需要我们站在巨人的肩膀上去拼接一张属于自己的图~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值