mybatis的查询效率问题

<div class="preview"><h1 class="story_title" style="margin-top: 1em; margin-right: 0; margin-bottom: 1em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.6; color: inherit; text-rendering: optimizelegibility; font-size: 38.5px;">mybatis的查询效率问题</h1><div class="story_tags"><div class="tag blue label" style="display: inline-block; vertical-align: baseline; line-height: 1; margin-top: 0em; margin-right: 0.125em; margin-bottom: 0em; margin-left: 0.125em; background-color: #3b83c0 !important; border-color: #3b83c0 !important; background-image: none; padding-top: 0.6em; padding-right: 0.8em; padding-bottom: 0.6em; padding-left: 0.8em; color: #ffffff !important; text-transform: none; font-weight: bold; border-radius: 0.2857rem; box-sizing: border-box; font-size: 0.7428rem;">mybatis</div><div class="tag blue label" style="display: inline-block; vertical-align: baseline; line-height: 1; margin-top: 0em; margin-right: 0.125em; margin-bottom: 0em; margin-left: 0.125em; background-color: #3b83c0 !important; border-color: #3b83c0 !important; background-image: none; padding-top: 0.6em; padding-right: 0.8em; padding-bottom: 0.6em; padding-left: 0.8em; color: #ffffff !important; text-transform: none; font-weight: bold; border-radius: 0.2857rem; box-sizing: border-box; font-size: 0.7428rem;">查询效率</div></div><h3 id="1e9858de7bdaee4bfa1e681af" style="margin-top: 0.5em; margin-right: 0; margin-bottom: 0.5em; margin-left: 0; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; text-rendering: optimizelegibility; font-size: 24.5px;">1.配置信息</h3>

现在有学生和老师两张表老师的Mapper

1.老师Mapper

 

<mapper namespace="com.abc.mapper.TeacherMapper">
    <resultMap type="com.abc.domian.Teacher" id="supervisorResultMap">
        <id property="id" column="oid"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
        <result property="researchArea" column="research_area"/>
        <result property="title" column="title"/>
         
        <collection property="supStudent" column="id" select="com.abc.mapper.StudentMapper.getStudentByTeacherId" resultMap="com.abc.mapper.StudentMapper.StudentResultMap"> collection>
     resultMap>
    <select id="getById" parameterType="int" resultMap="supervisorResultMap">
        select t.id as oid ,t.name,t.gender,t.research_area,t.title,s.id,s.name as s_name,s.gender,s.major,s.grade,s.supervisor_id from student s , teacher t where s.supervisor_id = t.id
        and t.id=#{id}
     select>
    <select id="getAllTeacher" parameterType="map" resultMap="supervisorResultMap">
        select * from teacher
     select>
 mapper>

2. 学生mapper

 

<mapper namespace="com.abc.mapper.StudentMapper">
    <resultMap type="com.abc.domian.Student" id="StudentResultMap">
        <id property="id" column="id"/>
        <result property="name" column="s_name"/>
        <result property="gender" column="gender"/>
        <result property="major" column="major"/>
        <result property="grade" column="grade"/>
        <association property="supervisor" resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap">
         association>
     resultMap>
    <select id="getById" parameterType="int" resultMap="StudentResultMap">
        select * from student where id = #{id}
     select>
     
    <insert id="add" parameterType="com.abc.domian.Student" useGeneratedKeys="true" keyProperty="id">
        insert into student(name,gender,major,grade) values(#{name},#{gender},#{major},#{grade})
     insert>
    <update id="update" parameterType="com.abc.domian.Student" >
        update student set gender = #{gender} where id= #{id}
     update>
     
    <delete id="delete" parameterType="com.abc.domian.Student">
        delete from student where id = #{id}
     delete>

    <select id="getStudnetAndTeacher" parameterType="int" resultMap="StudentResultMap">
        select * from student s ,teacher t where s.supervisor_id = t.id and s.id = #{id}
     select>   
    <insert id="addStudent" parameterType="com.abc.domian.Student" useGeneratedKeys="true" keyProperty="id">
        insert into student(name,gender,major,grade,supervisor_id)
        values(#{name},#{gender},#{major},#{grade},#{supervisor.id})
     insert>
    <select id="getStudentByTeacherId" parameterType="string" resultMap="StudentResultMap">
        select * from student where supervisor_id = #{id}
     select>
 mapper>

2. 问题

2.1、关联查询时效率的问题---通过老师查找学生getById的形式

配置文件:

2.1.1老师:
<resultMap type="com.abc.domian.Teacher" id="supervisorResultMap">
        <id property="id" column="oid"/>
        ...
        <collection property="supStudent" column="id" select="com.abc.mapper.StudentMapper.getStudentByTeacherId" resultMap="com.abc.mapper.StudentMapper.StudentResultMap"> collection>
     resultMap>
    <select id="getById" parameterType="int" resultMap="supervisorResultMap">
        select t.id as oid ,t.name,t.gender,t.research_area,t.title,s.id,s.name as s_name,s.gender,s.major,s.grade,s.supervisor_id from student s , teacher t where s.supervisor_id = t.id
        and t.id=#{id}
     select>
2.1.2 学生
<resultMap type="com.abc.domian.Student" id="StudentResultMap">
        <id property="id" column="id"/>
        ...
        <association property="supervisor" resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap">
         association>
     resultMap>

通过上面的配置我们可以看出来;老师查询学生的时候,使用通过select的配置进行查询的,如果我们通过这种方式来查询学生是会导致sql语句的增多;我们查看控制台后,发现输出的内容为:

select t.id as oid ,t.name,t.gender,t.research_area,t.title,s.id,s.name as s_name,s.gender,s.major,s.grade,s.supervisor_id from student s , teacher t where s.supervisor_id = t.id and t.id=? select * from student where supervisor_id = ? select * from student where supervisor_id = ? select * from student where supervisor_id = ? select * from student where supervisor_id = ? 

sql语句的个数明显很多,因此不建议通过该方式进行查询;
解决办法:
如果我们将老师的mapper映射文件:

<collection property="supStudent" column="id" select="com.abc.mapper.StudentMapper.getStudentByTeacherId" resultMap="com.abc.mapper.StudentMapper.StudentResultMap"> collection>

修改为:

<collection property="supStudent" resultMap="com.abc.mapper.StudentMapper.StudentResultMap"> collection> 

在进行查询的时候我们发现会只输出一句话;

select t.id as oid ,t.name,t.gender,t.research_area,t.title,s.id,s.name as s_name,s.gender,s.major,s.grade,s.supervisor_id from student s , teacher t where s.supervisor_id = t.id and t.id=? 

在进行关联映射的时候尽量不要使用collection中的select语句,这样会导致查询语句增多的问题;可以自己书写sql语句,让mybatis进行映射

2.2 关联查询别称起到的作用

根据输出的内容我么还可以发现另外一些问题:
1、通过自定义的sql语句进行查询时管理的查询出的对象没有null
2、通过select的配置查询的记录是由null只出现的;
mybatis的查询效率问题_第1张图片


你可能感兴趣的:(mybatis的查询效率问题)