1 | |
SqlSessionFactoryBean
1 | |

mapperLocations:支持通配符,比如:classpath*:sqlmap/*-mapper.xml"
一般情况下,mybatis的配置文件可以不需要(mybatis-config.xml),如下情况可能需要(configLocation):
- 需要定制
<settings>,其实configurationProperties属性可以支持 - 需要定制
<typeAliases>,其实typeHandlersPackage属性可以支持 - mapper xml和mapper class不在相同的classpath下,其实
mapperLocation属性可以支持
mybatis配置文件中
SqlSessionTemplate
SqlSession的一种实现,使用了代理模式,线程安全,由于每次都获取新的SQLSession,因而不支持一级缓存
1 | |
1 | |
SqlSessionDaoSupport
基于SqlSessionTemplate的一种扩展
1 | |
1 | |
MapperFactoryBean
Spring中定义Mapper,线程安全,每次都获取新的SQLSession
1 | |
MapperFactoryBean默认添加了依赖注入注解@Autowired
1 | |
1 | |
Mapper Scan
基于扫描自动生产Mapper
base-package:扫描Mapper的基础包,支持Ant匹配
factory-ref or template-ref:
annotation:被此注解的mapper接口,比如@Mapper
marker-interface:实现此接口的mapper接口,比如MyMapper
NOTE:<context:component-scan/> won’t be able to scan and register mappers. Mappers are interfaces and, in order to register them to Spring, the scanner must know how to create a MapperFactoryBean for each interface it finds.
<mybatis:scan/>
类似<context:component-scan/>
1 | |
@MapperScan
1 | |
MapperScannerConfigurer
推荐使用sqlSessionFactoryBean和sqlSessionTemplateBean,如果没有显示注入,由于开启了按类型自动注入,则会自动注入,参见如下源代码:
org.mybatis.spring.mapper.ClassPathMapperScanner.processBeanDefinitions(Set<BeanDefinitionHolder>)
1 | |