Mybatis反射工具类

ObjectFactory:构造对象

image.png

Reflector:缓存类的方法

image.png

BeanWrapper(ObjectWrapper):包装对象

可以更新属性
image.png

MetaObject

image.png
image.png

Mybatis配置多数据源

image.png
image.png

  1. @MapperScan(sqlSessionFactoryRef = "sqlSessionFactoryOrder", basePackages = {"jiagoubaiduren.mapper.order"})
  2. @Configuration
  3. public class OrderMybatisAutoConfiguration {
  4. private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
  5. private final String[] MAPPER_XML_PATH = new String[] {"classpath*:ordermapper/*.xml"};
  6. @Bean(name = "dataSourceOrder")
  7. @Primary
  8. @ConfigurationProperties("spring.datasource.order")
  9. public DataSource dataSourceOrder(){
  10. return DruidDataSourceBuilder.create().build();
  11. }
  12. @Bean(name = "sqlSessionFactoryOrder")
  13. @Primary
  14. public SqlSessionFactory sqlSessionFactoryOrder() throws Exception {
  15. SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
  16. factory.setDataSource(dataSourceOrder());
  17. factory.setVfs(SpringBootVFS.class);
  18. factory.setMapperLocations(resolveMapperLocations());
  19. return factory.getObject();
  20. }
  21. @Bean(name = "sqlSessionTemplateOrder")
  22. @Primary
  23. public SqlSessionTemplate sqlSessionTemplateOrder() throws Exception {
  24. return new SqlSessionTemplate(sqlSessionFactoryOrder());
  25. }
  26. public Resource[] resolveMapperLocations() {
  27. return Stream.of(Optional.ofNullable(MAPPER_XML_PATH).orElse(new String[0]))
  28. .flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
  29. }
  30. private Resource[] getResources(String location) {
  31. try {
  32. return resourceResolver.getResources(location);
  33. } catch (IOException e) {
  34. return new Resource[0];
  35. }
  36. }
  37. }