Mybatis高级映射分步查询之引入延迟加载提高查询性能-----Mybatis框架

分步查询的优点,可以重复利用,可复用性能增强,每一步的复用性较强
通过分步查询可以实现延迟加载和懒加载特性机制
延迟加载的核心原理是,用的时候执行查询语句,不用的时候不查询
作用:提高性能
笛卡尔积:3*4=12表越多,查询效率越低
咱们开启延迟加载呢?
association标签中添加fetchType="lazy"
默认情况下是没有开启延迟加载的
实际开发的程序是把全局的延迟加载打开,如果某一步不需要延迟加载,我们就为它单独设置eager
分步查询的优点,可以重复利用,可复用性能增强,每一步的复用性较强
通过分步查询可以实现延迟加载和懒加载特性机制
延迟加载的核心原理是,用的时候执行查询语句,不用的时候不查询
作用:提高性能
笛卡尔积:3*4=12表越多,查询效率越低
咱们开启延迟加载呢?
association标签中添加fetchType="lazy"
默认情况下是没有开启延迟加载的
实际开发的程序是把全局的延迟加载打开,如果某一步不需要延迟加载,我们就为它单独设置eager

<?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.powernode.mybatis.mappers.StudentMapper">
    <resultMap id="studentResultMapAssociation" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
<!--        association意思是关联,一个Student对象关联上一个clazz对象-->
<!--        javaType标签用来指定要映射的JAVA类型-->
        <association property="clazz" javaType="clazz">
            <id column="cid" property="cid"></id>
            <result property="cname" column="cname"></result>
        </association>
    </resultMap>
    <resultMap id="studentResultMap" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="clazz.cid" column="cid"></result>
        <result property="clazz.cname" column="cname"></result>
    </resultMap>
    <select id="selectById" resultMap="studentResultMap">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </select>
    <select id="selectByAssociation" resultMap="studentResultMapAssociation">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </select>
    <select id="selectByIDStepOne" resultMap="studentResultMapByStep">
        select sid,sname,cid from t_stu where sid = #{sid}
    </select>
<!--    结果映射,两条SQL语句,完成多对一的分步查询-->
<!--    这里是第一步,根据学生的id查询学生的所有信息-->
<!--    这些信息当中含有班级的cid-->
    <resultMap id="studentResultMapByStep" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
<!--        这里需要指定另外一句SQL语句的ID,来实现查询出另一个关联对象-->
<!--        设置完成后,就是这条sql语句的内容被需要时,我们才进行执行兼并加载数据-->
        <association property="clazz"
                     select="com.powernode.mybatis.mappers.ClazzMapper.selectByIdStepTwo"
                     fetchType="eager"
                     column="cid"></association>
    </resultMap>
</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="com.powernode.mybatis.mappers.StudentMapper">
    <resultMap id="studentResultMapAssociation" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
<!--        association意思是关联,一个Student对象关联上一个clazz对象-->
<!--        javaType标签用来指定要映射的JAVA类型-->
        <association property="clazz" javaType="clazz">
            <id column="cid" property="cid"></id>
            <result property="cname" column="cname"></result>
        </association>
    </resultMap>
    <resultMap id="studentResultMap" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="clazz.cid" column="cid"></result>
        <result property="clazz.cname" column="cname"></result>
    </resultMap>
    <select id="selectById" resultMap="studentResultMap">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </select>
    <select id="selectByAssociation" resultMap="studentResultMapAssociation">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </select>
    <select id="selectByIDStepOne" resultMap="studentResultMapByStep">
        select sid,sname,cid from t_stu where sid = #{sid}
    </select>
<!--    结果映射,两条SQL语句,完成多对一的分步查询-->
<!--    这里是第一步,根据学生的id查询学生的所有信息-->
<!--    这些信息当中含有班级的cid-->
    <resultMap id="studentResultMapByStep" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
<!--        这里需要指定另外一句SQL语句的ID,来实现查询出另一个关联对象-->
<!--        设置完成后,就是这条sql语句的内容被需要时,我们才进行执行兼并加载数据-->
        <association property="clazz"
                     select="com.powernode.mybatis.mappers.ClazzMapper.selectByIdStepTwo"
                     fetchType="eager"
                     column="cid"></association>
    </resultMap>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <settings>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="SLF4J"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <settings>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="SLF4J"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>
<?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.powernode.mybatis.mappers.ClazzMapper">
    <select id="selectByIdStepTwo" resultType="clazz">
        select cid,cname from t_clazz where cid = #{cid}
    </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="com.powernode.mybatis.mappers.ClazzMapper">
    <select id="selectByIdStepTwo" resultType="clazz">
        select cid,cname from t_clazz where cid = #{cid}
    </select>
</mapper>
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //分步查询第一步,根据SID查询学生信息
    Student selectByIDStepOne(Integer id);
    //使用Association的方式查询学生信息
    Student selectByAssociation(Integer id);
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //分步查询第一步,根据SID查询学生信息
    Student selectByIDStepOne(Integer id);
    //使用Association的方式查询学生信息
    Student selectByAssociation(Integer id);
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //分步查询第一步,根据SID查询学生信息
    Student selectByIDStepOne(Integer id);
    //使用Association的方式查询学生信息
    Student selectByAssociation(Integer id);
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //分步查询第一步,根据SID查询学生信息
    Student selectByIDStepOne(Integer id);
    //使用Association的方式查询学生信息
    Student selectByAssociation(Integer id);
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StudentMapperTest
{
    private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
    @Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(1);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
    @Test
    public void selectByAssociation()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByAssociation(2);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
    @Test
    public void selectByIdStepTwo()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByIDStepOne(1);
        logger.info(student.getSname().toString());
        //程序执行到这里了,我想看看班级名字
        logger.info(student.getClazz().getCname());
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StudentMapperTest
{
    private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
    @Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(1);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
    @Test
    public void selectByAssociation()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByAssociation(2);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
    @Test
    public void selectByIdStepTwo()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByIDStepOne(1);
        logger.info(student.getSname().toString());
        //程序执行到这里了,我想看看班级名字
        logger.info(student.getClazz().getCname());
        SqlSessionUtils.close(sqlSession);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值