Mybatis反射工具类
ObjectFactory:构造对象
Reflector:缓存类的方法
BeanWrapper(ObjectWrapper):包装对象
可以更新属性
MetaObject
Mybatis配置多数据源
@MapperScan(sqlSessionFactoryRef = "sqlSessionFactoryOrder", basePackages = {"jiagoubaiduren.mapper.order"})
@Configuration
public class OrderMybatisAutoConfiguration {
private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
private final String[] MAPPER_XML_PATH = new String[] {"classpath*:ordermapper/*.xml"};
@Bean(name = "dataSourceOrder")
@Primary
@ConfigurationProperties("spring.datasource.order")
public DataSource dataSourceOrder(){
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryOrder")
@Primary
public SqlSessionFactory sqlSessionFactoryOrder() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSourceOrder());
factory.setVfs(SpringBootVFS.class);
factory.setMapperLocations(resolveMapperLocations());
return factory.getObject();
}
@Bean(name = "sqlSessionTemplateOrder")
@Primary
public SqlSessionTemplate sqlSessionTemplateOrder() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryOrder());
}
public Resource[] resolveMapperLocations() {
return Stream.of(Optional.ofNullable(MAPPER_XML_PATH).orElse(new String[0]))
.flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
}
private Resource[] getResources(String location) {
try {
return resourceResolver.getResources(location);
} catch (IOException e) {
return new Resource[0];
}
}
}