Mybatis和springboot集成
mybaits和springboot集成即可以写在application.yml里配置
mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.entity
也可以写在java类里
使用@Bean的方式住入sqlSessionFactory
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSession(@Qualifier("dynamicDataSource")DataSource dynamicDataSource) {
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
SqlSessionFactoryBean sqlSession = new SqlSessionFactoryBean();
sqlSession.setDataSource(dynamicDataSource);
sqlSession.setVfs(SpringBootVFS.class);
ibatis.session.Configuration conf = new ibatis.session.Configuration();
//查询结果map为空值时不忽略空值
conf.setCallSettersOnNulls(true);
//开启驼峰命名模式
conf.setMapUnerscoreToCamelCase(true);
sqlSession.setConfiguration(conf);
sqlSession.setMapperLocations(patternResolver.getSources(mapperLocation));
}
@Configuration
@MapperScan("")
@AutoConfigureAfter(DataSourceConfiguration.class)
MapperScannerConfigurer的使用