Mybatis直接写sql与使用association映射性能比较

最近在公司发现前辈写多表查询时使用mybatis并未使用等标签封装数据

这引发我对mybatis=有无使用标签进行数据查询的性能比较产生了兴趣,于是乎~~~做了个小小的测试

首先建立两个表:student 和 teacher

teacher表结构:

在这里插入图片描述

student表结构:
在这里插入图片描述
用java分表为两个表写了个120万的循环插入数据,主要是id不同,whatever,为了有个数据测试罢了
pojo类

@Data
@Accessors(chain = true)
public class Student {
    private Integer id;

    private String name;

    private Integer age;

    private String sex;
}

@Data
@Accessors(chain = true)
public class Teacher {
    private Integer id;

    private String name;

    private Integer studentId;

}

DTO

@Data
public class TS {
    private int id;

    private String name;

    private Student student;

}
@Data
public class ST {
    private int tid;
    private String tname;
    private int sid;
    private int age;
    private String sex;
    private String sname;
}

这是测试使用的xml

<resultMap id="selectMap" type="com.example.demo.pojo.TS">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="student" javaType="com.example.demo.pojo.Student">
            <id column="sid" property="id"/>
            <result column="sname" property="name"/>
            <result column="sex" property="sex"/>
            <result column="age" property="age"/>
        </association>
    </resultMap>
    <select id="select" resultType="com.example.demo.pojo.Student">
        select t.id , t.name,
        s.id as sid, s.name as sname, s.age, s.sex from teacher t
        inner join student s on s.id = t.student_id
    </select>
<resultMap id="selectMap" type="com.example.demo.pojo.ST">
        <id column="id" property="tid"/>
        <result column="sid" property="sid"/>
        <result column="age" property="age"/>
        <result column="sname" property="sname"/>
        <result column="tname" property="tname"/>
        <result column="sex" property="sex"/>
    </resultMap>
    <select id="select" resultMap="selectMap">
        select t.id , t.name as tname,
        s.id as sid, s.name as sname, s.age, s.sex from teacher t
        inner join student s on s.id = t.student_id
    </select>

与上面的ST 和TS类想对应的查询
主要就是想写个直接映射和通过association映射的对比
最后的数据:(单位是毫秒)
在这里插入图片描述
通过数据比较会发现使用了association做映射效率会高23%左右

小菜鸟一枚,对于实验测试可能不太严谨,但是算是第一次对于mybatis性能的测试,这篇博客就当留念吧~~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值