分步查询的优点,可以重复利用,可复用性能增强,每一步的复用性较强
通过分步查询可以实现延迟加载和懒加载特性机制
延迟加载的核心原理是,用的时候执行查询语句,不用的时候不查询
作用:提高性能
笛卡尔积: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);
}
}