通常情况下,@One注解主要在一对一的关联查询中使用,而@Many主要在一对多的关联查询中使用。意外的是,本菜鸡通过查看源码,发现两者的源码是相同的。
@One源码:
package org.apache.ibatis.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.ibatis.mapping.FetchType;
/**
* @author Clinton Begin
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface One {
String select() default "";
FetchType fetchType() default FetchType.DEFAULT;
}
@Many源码:
package org.apache.ibatis.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.ibatis.mapping.FetchType;
/**
* @author Clinton Begin
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Many {
String select() default "";
FetchType fetchType() default FetchType.DEFAULT;
}
作为探索,我在一对多查询中将以下代码:
many=@Many(select="com.cr.mapper.BookMapper.getBooksByUserId",fetchType= FetchType.EAGER)
修改为:
one=@One(select="com.cr.mapper.BookMapper.getBooksByUserId",fetchType= FetchType.EAGER)
发现输出结果并无影响。
同样在一对一查询中实现one和many的对换,输出结果也无变化。
此处大胆的猜测,@One和@Many并无本质的区别,其出现两个名字是否仅仅是为了增强代码的可读性呢?求大神赐教。2019.01.03(参考项目:mybatis20190102oneToOne)