Java Spring boot jpa通过实体管理器访问辅助数据源

Java Spring boot jpa通过实体管理器访问辅助数据源,java,spring,spring-boot,spring-data-jpa,entitymanager,Java,Spring,Spring Boot,Spring Data Jpa,Entitymanager,我有两个数据源。主数据库编写得很好,所以我将它与JPA一起用于多查询。相反,辅助数据源使用一个非常丑陋的数据库,但我只需要执行一个大查询(而不需要其他操作)。 接下来,我能够设置辅助数据源(在weblogic上),所以现在我的目标是调用辅助数据源上的本机查询 这是我的代码: 应用程序属性 spring.datasource.jiano.jndi-name=jdbc/JianoDS spring.datasource.jiano.driver-class-oracle.jdbc.driver.Or

我有两个数据源。主数据库编写得很好,所以我将它与JPA一起用于多查询。相反,辅助数据源使用一个非常丑陋的数据库,但我只需要执行一个大查询(而不需要其他操作)。 接下来,我能够设置辅助数据源(在weblogic上),所以现在我的目标是调用辅助数据源上的本机查询

这是我的代码:

应用程序属性

spring.datasource.jiano.jndi-name=jdbc/JianoDS
spring.datasource.jiano.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.jiano.hikari.connection-timeout=60000
spring.datasource.jiano.hikari.maximum-pool-size=5

spring.datasource.sgu.jndi-name=jdbc/sguDatasource
spring.datasource.sgu.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.sgu.hikari.connection-timeout=60000
spring.datasource.sgu.hikari.maximum-pool-size=5    
弹簧靴主:

@ComponentScan   
@SpringBootApplication
public class BemonitorcaaApplication extends SpringBootServletInitializer implements WebApplicationInitializer {

    public static void main(String[] args) {
        SpringApplication.run(BemonitorcaaApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(BemonitorcaaApplication.class);
    }
}   
我在下面添加了一个类来处理多个:

控制员:

    @RestController
    @RequestMapping("/test")
    public class IapaController {
    
        @Autowired
        IapaService iapaService;
        
        @PersistenceContext
        EntityManager em;
    
    
        @RequestMapping("/v1/the-only-query-on-2nd-datasource")
        public List<String> test2ndDS() {
            List<String> itemList = em.createQuery("Select a.text ......." )
                    .getResultList();   //Not working   
            return itemList ;
        }
    
        @RequestMapping("/v1/primary-ds-is-working-fine")
        public List<IapaTipiAndamenti> test1stDS() {
            return iapaService.test1stDS(); //This is working, here for example I will use a typical jpa findAll
        }

//...other jpa methods for the primary datasource
    }
@RestController
@请求映射(“/test”)
公共类IAPAC控制器{
@自动连线
IapaService IapaService;
@持久上下文
实体管理器;
@RequestMapping(“/v1/the-only-query-on-2nd-datasource”)
公共列表test2ndDS(){
List itemList=em.createQuery(“选择a.text……”)
.getResultList();//不工作
返回项目列表;
}
@RequestMapping(“/v1/主ds工作正常”)
公共列表test1stDS(){
return iapaService.test1stDS();//这是可行的,例如,这里我将使用一个典型的jpa findAll
}
//…主数据源的其他jpa方法
}
实体管理器不工作,我试图在
DatasourceConfig
中添加实体管理器配置,但它不工作。(例如,我没有要扫描的包,因为我只对辅助数据源执行本机查询,该查询返回基本类型,因此没有域或存储库类。)


如何修复实体管理器?(我使用的是Spring boot 1.5.17.RELEASE)

您好,您可以在第二个数据源中使用一个简单的JdbcTemplate对象,如

@Configuration
public class DatasourceConfig {
    @Value("${spring.datasource.jiano.jndi-name}")
    private String primaryJndiName;

    @Value("${spring.datasource.sgu.jndi-name}")
    private String secondaryJndiName;

    private JndiDataSourceLookup lookup = new JndiDataSourceLookup();

    @Primary
    @Bean(destroyMethod = "") // destroy method is disabled for Weblogic update app ability
    public DataSource primaryDs() {
        return lookup.getDataSource(primaryJndiName);
    }

    @Bean(name = "sguDs", destroyMethod = "") // destroy method is disabled for Weblogic update app ability
    public DataSource secondaryDs() {
        return lookup.getDataSource(secondaryJndiName);
    }    

    @Bean
    public JdbcTemplate jdbcTemplate(){
      return new JdbcTemplate(secondaryDs());
    }
}
然后在控制器中尝试:

@RestController
@RequestMapping("/test")
public class IapaController {

    @Autowired
    IapaService iapaService;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/v1/the-only-query-on-2nd-datasource")
    public List<String> test2ndDS() {
        String query = "Select * ....";
        List<String> itemList = (List<String>) jdbcTemplate.queryForList(query, String.class);   
        return itemList ;
    }

    @RequestMapping("/v1/primary-ds-is-working-fine")
    public List<IapaTipiAndamenti> test1stDS() {
        return iapaService.test1stDS(); //This is working, here for example I will use a typical jpa findAll
    }

//...other jpa methods for the primary datasource
    }
@RestController
@请求映射(“/test”)
公共类IAPAC控制器{
@自动连线
IapaService IapaService;
@自动连线
jdbc模板jdbc模板;
@RequestMapping(“/v1/the-only-query-on-2nd-datasource”)
公共列表test2ndDS(){
String query=“选择*…”;
List itemList=(List)jdbcTemplate.queryForList(query,String.class);
返回项目列表;
}
@RequestMapping(“/v1/主ds工作正常”)
公共列表test1stDS(){
return iapaService.test1stDS();//这是可行的,例如,这里我将使用一个典型的jpa findAll
}
//…主数据源的其他jpa方法
}

我没有persistence.xml,因此我得到了以下错误:
创建名为“entityManagerFactorySecondaryDs”的bean时出错,该bean在类路径资源[it/mycompany/DatasourceConfig.class]中定义:调用init方法失败;嵌套异常是java.lang.IllegalStateException:No persistence units parsed from{classpath*:META-INF/persistence.xml}
hello我已经用EntityManagerFactory配置的示例更新了我的示例。我真的希望它能帮助你。如果值得,请随时更新。我真的需要ENTITYMANAGER_包到_扫描?正如我在回答中所说,我没有要扫描的包,因为我没有域类。我从什么时候开始获得所有其他属性的名称?很抱歉,因为您在请求中使用了entityManager,所以我集中精力使用entityManager。但您需要的是使用JdbcTemplate。这个目标对于你想要实现的目标来说是完美的。我更新了我的答案,向您展示了如何使用它。我认为使用JPA可以将实体管理器作为jdbcTemplate处理。但我想你是对的,如果我没有可以扫描域类的包,我就不需要实体管理器。
@RestController
@RequestMapping("/test")
public class IapaController {

    @Autowired
    IapaService iapaService;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/v1/the-only-query-on-2nd-datasource")
    public List<String> test2ndDS() {
        String query = "Select * ....";
        List<String> itemList = (List<String>) jdbcTemplate.queryForList(query, String.class);   
        return itemList ;
    }

    @RequestMapping("/v1/primary-ds-is-working-fine")
    public List<IapaTipiAndamenti> test1stDS() {
        return iapaService.test1stDS(); //This is working, here for example I will use a typical jpa findAll
    }

//...other jpa methods for the primary datasource
    }