最近在公司发现前辈写多表查询时使用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性能的测试,这篇博客就当留念吧~~