文章目录
三种方式主动,一种被动
//第一种
@ConstructorArgs({
@Arg(column = "id", javaType = Integer.class, id = true),
@Arg(column = "userName", javaType = String.class, id = false),
@Arg(column = "password", javaType = String.class, id = false)}
)
@Select("select id, userName,password from user")
List<User> selectAll1();
//第二种
@Results({
@Result(property = "id", column = "id", id = true),
@Result(property = "userName", column = "userName", id = false),
@Result(property = "password", column = "id", id = false),
})
@Select("select id, userName,password from user")
List<User> selectAll2();
//第三种
<mapper namespace="com.mybatis.demo.mapper.UserMapper">
<resultMap id="commonResultMap" type="com.mybatis.demo.entity.User">
<result column="id" property="id"></result>
<result column="userName" property="userName"></result>
<result column="password" property="password"></result>
</resultMap>
</mapper>
一:三种方式主动
1. @ConstructorArgs
生成的ResultMap
参见mybatis源码
private String parseResultMap(Method method) {
//获取方法的放回类型
Class<?> returnType = getReturnType(method);
//获取方法上的@Arg注解数组 参见@ConstructorArgs注解
Arg[] args = method.getAnnotationsByType(Arg.class);
//获取方法上的@Result注解数组 参见@Results
Result[] results = method.getAnnotationsByType(Result.class);
//获取方法上的@TypeDiscriminator注解 参见@TypeDiscriminator
TypeDiscriminator typeDiscriminator = method.getAnnotation(TypeDiscriminator.class);
//生成resultMap的唯一标识
String resultMapId = generateResultMapName(method);
//生成ResultMap
applyResultMap(resultMapId, returnType, args, results, typeDiscriminator);
return resultMapId;
}
generateResultMapName方法中生成默认的ResultMap
private String generateResultMapName(Method method) {
Results results = method.getAnnotation(Results.class);
if (results != null && !results.id().isEmpty()) {
return type.getName() + "." + results.id();
}
StringBuilder suffix = new StringBuilder();
for (Class<?> c : method.getParameterTypes()) {
suffix.append("-");
suffix.append(c.getSimpleName());
}
if (suffix.length() < 1) {
suffix.append("-void");
}
return type.getName() + "." + method.getName() + suffix;
}
可以得知ResultMap名称是 type.getName() + "." + method.getName() + suffix
,其实就是方法的全限定名加上参数类型的getSimpleName()
简单类名加-
隔开
这里上文 @ConstructorArgs生成的ResultMap的id就是com.mybatis.demo.mapper.UserMapper.selectAll1-void
2. @Results
生成的ResultMap
调用的也是generateResultMapName方法中生成默认的ResultMap
private String generateResultMapName(Method method) {
Results results = method.getAnnotation(Results.class);
if (results != null && !results.id().isEmpty()) {
return type.getName() + "." + results.id();
}
StringBuilder suffix = new StringBuilder();
for (Class<?> c : method.getParameterTypes()) {
suffix.append("-");
suffix.append(c.getSimpleName());
}
if (suffix.length() < 1) {
suffix.append("-void");
}
return type.getName() + "." + method.getName() + suffix;
}
可以得出在@Results注解的Id属性缺省ResultMap的id是与 @ConstructorArgs一致的,是com.mybatis.demo.mapper.UserMapper.selectAll2-void
,如果存在@Results注解的Id属性就是type.getName() + "." + results.id()
;本例的ResultMap的id就会是com.mybatis.demo.mapper.UserMapper.selectAll2.${id}
注
:${id}为@Results注解的Id属性设置的值
3. <ResultMap>
标签生成的ResultMap
第三种比较常见 ResultMap的id就是标签设置的
<mapper namespace="com.mybatis.demo.mapper.UserMapper">
<resultMap id="commonResultMap" type="com.mybatis.demo.entity.User">
<result column="id" property="id"></result>
<result column="userName" property="userName"></result>
<result column="password" property="password"></result>
</resultMap>
</mapper>
二:一种被动
4 . @AutomapConstructor和无任何ResultMap指定
在构造函数中@AutomapConstructor
@AutomapConstructor
public User(Integer id, String userName, String password) {
this.id = id;
this.userName = userName;
this.password = password;
}
将会在代码没有ResultMap时,返回的类型为构造函数类或者构造函数类集合的时候被动生成ResultMap
无任何ResultMap指定
:不写ResultMap系统也会自动生成一个该方法的ResultMap,id与@Results上文缺省一样。
5. 验证
5. 1验证 @ConstructorArgs
@ResultMap("com.mybatis.demo.mapper.UserMapper.selectAll1-void")
@Select("select id, userName,password from user")
List<User> selectAll4();
单元测试
结果
通过
5. 2验证 @Results
@ResultMap("com.mybatis.demo.mapper.UserMapper.selectAll2-void")
@Select("select id, userName,password from user")
List<User> selectAll4();
单元测试
结果
通过
5. 3验证 <ResultMap> 标签
@ResultMap("com.mybatis.demo.mapper.UserMapper.commonResultMap")
@Select("select id, userName,password from user")
List<User> selectAll4();
单元测试
结果
通过
5. 4验证 @AutomapConstructor无任何ResultMap指定
@Select("select id, userName,password from user")
List<User> selectAll1();
单元测试 随便打出有啥ResultMap
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ResultMapApplication.class)
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Autowired
private SqlSession sqlSession;
@org.junit.Test
public void selectAll1() {
System.out.println(userMapper.selectAll1());
sqlSession.getConfiguration().getResultMapNames().forEach(System.out::println);
}
}
缺省selectALL1方法缺省ResultMap情况下
系统自动为他生成了ResultMap
无任何ResultMap指定( 无@AtomapConstructor注解同样也会生成)
去掉 @AutomapConstructor
public User(Integer id, String userName, String password) {
this.id = id;
this.userName = userName;
this.password = password;
}
同样系统生成默认ResultMap
方法加上ResultMapMap指定
@ResultMap("com.mybatis.demo.mapper.UserMapper.commonResultMap")
@Select("select id, userName,password from user")
List<User> selectAll1();
注
:com.mybatis.demo.mapper.UserMapper.commonResultMap
为xml文件中生成的ResultMap
结果就不生产默认的ResultMap
可以见得这是一种被动的ResultMap生成方式