在jhipster 5.7.2项目中使用mybatis ORM添加第二个数据源的问题

时间:2019-03-07 15:32:16

标签: jhipster mybatis

在jhipster 5.7.2项目中使用mybatis ORM添加第二个数据源的问题

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添加mybatis示例xml文件 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"
}

我该如何解决这个问题

0 个答案:

没有答案