Mybatis一对多映射采用延迟加载提高查询性能-----Mybatis框架

<?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>
    <select id="selectByCidStepTwo" resultType="student">
        select * from t_stu 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.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>
    <select id="selectByCidStepTwo" resultType="student">
        select * from t_stu 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>
    <select id="selectByCollection" resultMap="clazzResultMap">
        select c.cid,c.cname,s.sid,s.sname from t_clazz as c left join t_stu as s on c.cid = s.cid where c.cid = #{cid}
    </select>
    <resultMap id="clazzResultMap" type="Clazz">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
<!--        ofType用来指定集合中元素的类型-->
        <collection property="studentList" ofType="student">
            <id property="sid" column="sid"></id>
            <result property="sname" column="sname"></result>
        </collection>
    </resultMap>
    <resultMap id="ResultByStepOne" type="clazz">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
        <collection property="stus"
        select="com.powernode.mybatis.mappers.StudentMapper.selectByCidStepTwo"
        column="cid"></collection>
    </resultMap>
    <select id="selectByStepOne" resultMap="selectByStepOne">
        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>
    <select id="selectByCollection" resultMap="clazzResultMap">
        select c.cid,c.cname,s.sid,s.sname from t_clazz as c left join t_stu as s on c.cid = s.cid where c.cid = #{cid}
    </select>
    <resultMap id="clazzResultMap" type="Clazz">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
<!--        ofType用来指定集合中元素的类型-->
        <collection property="studentList" ofType="student">
            <id property="sid" column="sid"></id>
            <result property="sname" column="sname"></result>
        </collection>
    </resultMap>
    <resultMap id="ResultByStepOne" type="clazz">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
        <collection property="stus"
        select="com.powernode.mybatis.mappers.StudentMapper.selectByCidStepTwo"
        column="cid"></collection>
    </resultMap>
    <select id="selectByStepOne" resultMap="selectByStepOne">
        select cid,cname from t_clazz where cid = #{cid}
    </select>
</mapper>
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

import java.util.List;

public interface StudentMapper
{
    List<Student> selectByCidStepTwo(Integer cid);
    //分步查询第一步,根据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;

import java.util.List;

public interface StudentMapper
{
    List<Student> selectByCidStepTwo(Integer cid);
    //分步查询第一步,根据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.Clazz;

public interface ClazzMapper
{
    Clazz selectByStepOne(Integer id);
    //分步查询第二步,根据cid查询班级信息
    Clazz selectByIdStepTwo(Integer cid);
    Clazz selectByCollection(Integer cid);
}

package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Clazz;

public interface ClazzMapper
{
    Clazz selectByStepOne(Integer id);
    //分步查询第二步,根据cid查询班级信息
    Clazz selectByIdStepTwo(Integer cid);
    Clazz selectByCollection(Integer cid);
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.ClazzMapper;
import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Clazz;
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);
    }
    @Test
    public void clazzResultMap()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
        Clazz clazz = mapper.selectByCollection(1000);
        logger.info(clazz.toString());
    }
    @Test
    public void TestselectByCidStepTwo()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
        Clazz clazz = mapper.selectByStepOne(1000);
        logger.info(clazz.toString());
        SqlSessionUtils.close(sqlSession);
    }
}

package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.ClazzMapper;
import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Clazz;
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);
    }
    @Test
    public void clazzResultMap()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
        Clazz clazz = mapper.selectByCollection(1000);
        logger.info(clazz.toString());
    }
    @Test
    public void TestselectByCidStepTwo()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
        Clazz clazz = mapper.selectByStepOne(1000);
        logger.info(clazz.toString());
        SqlSessionUtils.close(sqlSession);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值