我们还可以给实体类起一个别名,以简化Mapper的编写:
在mybatis-config中起别名
<!-- 需要在environments的上方 -->
<typeAliases>
<typeAlias type="com.test.entity.Student" alias="Student"/>
</typeAliases>
现在Mapper就可以直接使用别名了:
<mapper namespace="com.test.mapper.TestMapper">
<select id="selectStudent" resultType="Student">
select * from student
</select>
</mapper>
也可以直接让Mybatis去扫描一个包,并将包下的所有类自动起别名(别名为首字母小写的类名)
<typeAliases>
<package name="com.test.entity"/>
</typeAliases>
现在Mapper就可以直接使用首字母小写的类名了:
<mapper namespace="com.test.mapper.TestMapper">
<select id="selectStudent" resultType="student">
select * from student
</select>
</mapper>
在实体类上起别名
可以在指定实体类添加一个注解,来指定别名:
@Alias("zzp")
public class Student {
private int sid;
private String name;
private String sex;
}