Проблема с добавлением 2-го источника данных с mybatis ORM в проекте jhipster 5.7.2 - PullRequest
1 голос
/ 07 марта 2019

Проблема с добавлением 2-го источника данных с mybatis ORM в проекте jhipster 5.7.2

1 、 добавление конфигурации MatchingDatabaseConfiguration.java

@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = "sample.mybatis.domain")
@MapperScan(basePackages = "sample.mybatis.dao", sqlSessionTemplateRef = "matchingSqlSessionTemplate")
public class MatchingDatabaseConfiguration {
    private String localMapper = "classpath:mappers/*.xml";

    @Bean
    @ConfigurationProperties("application.datasource")
    public DataSourceProperties matchingDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "matchingDataSource")
    @ConfigurationProperties("application.datasource")
    public DataSource matchingDataSource() {
        return matchingDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean(name = "matchingTransactionManager")
    public DataSourceTransactionManager matchingTransactionManager() {
        return new DataSourceTransactionManager(matchingDataSource());
    }

    @Bean(name = "matchingSqlSessionFactory")
    public SqlSessionFactory matchingSqlSessionFactory() throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(matchingDataSource());
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
            getResources(localMapper));
        return sessionFactory.getObject();
    }

    @Bean(name = "matchingSqlSessionTemplate")
    public SqlSessionTemplate buildSqlSessionTemplate(@Qualifier("matchingSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "matchingTransaction")
    public PlatformTransactionManager matchingTransactionManager(@Qualifier("matchingDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

2 application-dev.yml

application:
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8&useSSL=false
        username: root
        password:

3 ApplicationProperties.java Добавить поле свойств

https://github.com/jnuc093/xpdjh/blob/master/src/main/java/com/isunland/app/config/ApplicationProperties.java

4 добавить образцы XML-файлов mybatis https://github.com/jnuc093/xpdjh/tree/master/src/main/resources/mappers

5 добавить пакет sample.mybatis https://github.com/jnuc093/xpdjh/tree/master/src/main/java/sample/mybatis

6 добавить контроллер

@RestController
@RequestMapping("/api")
public class CityResource {

    @Autowired
    private CityDao cityDao;

    @Autowired
    private HotelDao hotelDao;

    @GetMapping("/test-mybatis/{cityId}")
    public Hotel run(@PathVariable int cityId) {
        return hotelDao.selectByCityId(cityId);
    }
}

7 Официальный образец mybatis здесь

https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples/mybatis-spring-boot-sample-xml

Когда я получаю доступ http://localhost:9000/api/test-mybatis/123

Response Body
{
  "type": "https://www.jhipster.tech/problem/problem-with-message",
  "title": "Internal Server Error",
  "status": 500,
  "detail": "Could not find result map sample.mybatis.dao.CityDao.City",
  "path": "/api/test-mybatis/123",
  "message": "error.http.500"
}

Как я могу решить эту проблему

...