[Mybatis]@Mapper注解
/*** 使用JavaConfig时,使用此注释注册MyBatis mapper接口*/@Import(MapperScannerRegistrar.class)public @interface MapperScan {}
·
简介
Mybatis支持的映射方式有基于xml的mapper.xml文件、基于java的使用Mapper接口类。接口方法注解主要是四个:@Insert、@Delete、@Update、@Select
从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件。
在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类。
@Mapper
public interface UserMapper {
@Insert("insert into user(name) values(#{name})")
@Options(keyProperty="id",keyColumn="id",useGeneratedKeys=true)
public void save(User user);
}
使用@Mapper可以不在包扫描而创建dao接口实现类对象。如果再搭配上注解式sql可以实现无mapper.xml。
老的xml方式
spring.xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包 -->
<property name="basePackage" value="com.hd.**.dao" />
<!-- 如果你的项目中只有一个SqlSessionFactory(或SqlSessionTemplate),就可以不用指定,因为MapperScannerConfigurer会自动装配SqlSessionFactory(或 SqlSessionTemplate);否则就必须指定 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 注意:若同时配置了SqlSessionFactory和SqlSessionTemplate,则系统会选择SqlSessionTemplate,作废SqlSessionFactory -->
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"></property>
<!-- 含有特定注解的接口,一般设置为Spring提供的@Repository或Mybatis提供的@Mapper,当然也可以自定义注解 -->
<property name="annotationClass" value="org.springframework.stereotype.Repository"></property>
</bean>
MapperScannerConfigurerpostProcessBeanDefinitionRegistry()方法(继承自BeanDefinitionRegistryPostProcessor),根据自己的各属性值创建一个ClassPathMapperScanner对象,并执行其scan()方法。注:接口不用标注 @Mapper/@Component注解(除非指定了annotationClass属性)
ClassPathMapperScannerdoScan()方法,对指定的mapper包进行扫描,解析每个Mapper接口,然后封装成BeanDefinition,但是这里BeanDefinition对应的class类型并不是Mapper接口对应的类型,而是MapperFactoryBean类型,即Mapper接口注册到IOC容器的都是MapperFactoryBean类型。
MapperFactoryBean- 也是一个工厂bean,当我们通过IOC容器获取一个Mpper类型的时候,会找到工厂bean为Mapper类型的bean,然后调用其
getObject()方法。
- 也是一个工厂bean,当我们通过IOC容器获取一个Mpper类型的时候,会找到工厂bean为Mapper类型的bean,然后调用其
javaConf方式- 使用@Bean+@MapperScannerConfigurer
这种方式与老的xml方式的原理是完全一样的。
javaConf方式- 使用@MapperScan
/**
* 使用JavaConfig时,使用此注释注册MyBatis mapper接口
*/
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {
}
@MapperScan("com.xx.yy.mapper")- 有元注解:
@Import(MapperScannerRegistrar.class)
- 有元注解:
MapperScannerRegistrar- 实现了
ImportBeanDefinitionRegistrar接口,重写registerBeanDefinitions(),方法内获取注解元数据的@MapperScan注解的属性值,根据这些属性值注入一个MapperScannerConfigurer-Bean, - 注入的这个bean的
beanDefinition:根据@MapperScan注解的属性值
- 实现了
MapperScannerConfigurer- 同上,所以接口也不用标注 @Mapper/@Component注解(除非指定了annotationClass属性)
ClassPathMapperScanner- 同上
MapperFactoryBean- 同上
总结:需要在启动类上使用
@MapperScan,并指定扫描Mapper的base package,还可以指定标有某个注解的接口(若不指定,则接口上不需要任何注解)。
javaConf方式- 使用mybatis-spring-boot-autoconfigure
- 该jar包的
META-INF/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
MybatisAutoConfiguration- pom需要引入mybatis和mybatis-spring这两个依赖,
MybatisAutoConfiguration才会起作用 - 若当前IOC容器没有
SqlSessionFactory-Bean·、SqlSessionTemplate-Bean,会分别创建一个 - 如果当前IOC容器没有
MapperFactoryBean-Bean,MapperScannerConfigurer-Bean,则会导入AutoConfiguredMapperScannerRegistrar
- pom需要引入mybatis和mybatis-spring这两个依赖,
AutoConfiguredMapperScannerRegistrar- 其通过实现
registerBeanDefinitions()方法,将注入一个MapperScannerConfigurer-Bean到了IOC容器中,注入的这个bean的beanDefinition:扫描Spring Boot的base package下(所以不需要再指定扫描Mapper的base package)的带有@Mapper注解(所以接口需要标注 @Mapper注解)的class
- 其通过实现
MapperScannerConfigurer- 见javaConf方式- 使用
@MapperScan
- 见javaConf方式- 使用
@Mapper- 将接口标记为
MyBatis mapper
- 将接口标记为
总结:不需要在启动类上使用
@MapperScan,只需要确保Mapper接口在Spring Boot的base package下(一般情况下都在),且接口上标有@Mapper注解即可!
更多推荐



所有评论(0)