|
| 1 | +package org.test.multiple.databases.second; |
| 2 | + |
| 3 | +import org.postgresql.ds.PGSimpleDataSource; |
| 4 | +import org.springframework.context.annotation.Bean; |
| 5 | +import org.springframework.context.annotation.Configuration; |
| 6 | +import org.springframework.context.annotation.Lazy; |
| 7 | +import org.springframework.data.jdbc.core.convert.*; |
| 8 | +import org.springframework.data.jdbc.core.dialect.JdbcArrayColumns; |
| 9 | +import org.springframework.data.jdbc.core.dialect.JdbcDialect; |
| 10 | +import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
| 11 | +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; |
| 12 | +import org.springframework.data.jdbc.repository.config.DialectResolver; |
| 13 | +import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; |
| 14 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 15 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 16 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 17 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 18 | +import org.springframework.transaction.TransactionManager; |
| 19 | + |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +@Configuration |
| 23 | +@EnableJdbcRepositories(jdbcOperationsRef = "firstNamedParameterJdbcOperations", |
| 24 | + transactionManagerRef = "secondTransactionManager", |
| 25 | + basePackages = "org.test.multiple.databases.second") |
| 26 | +public class SecondDbConfig extends AbstractJdbcConfiguration { |
| 27 | + @Bean |
| 28 | + @Second |
| 29 | + DataSource secondDataSource() { |
| 30 | + PGSimpleDataSource source = new PGSimpleDataSource(); |
| 31 | + source.setServerNames(new String[] { "xxx.xxx.x.x" }); |
| 32 | + source.setPortNumbers(new int[] { 5433 }); |
| 33 | + source.setDatabaseName("second"); |
| 34 | + source.setUser("user"); |
| 35 | + source.setPassword("password"); |
| 36 | + return source; |
| 37 | + } |
| 38 | + |
| 39 | + @Bean |
| 40 | + @Second |
| 41 | + NamedParameterJdbcOperations secondNamedParameterJdbcOperations(@Second DataSource dataSource) { |
| 42 | + return new NamedParameterJdbcTemplate(dataSource); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + @Bean |
| 47 | + @Second |
| 48 | + public DataAccessStrategy dataAccessStrategyBean(@Second NamedParameterJdbcOperations operations, JdbcConverter jdbcConverter, |
| 49 | + JdbcMappingContext context, Dialect dialect) { |
| 50 | + return new DefaultDataAccessStrategy(new SqlGeneratorSource(context, jdbcConverter, dialect), context, |
| 51 | + jdbcConverter, operations, new SqlParametersFactory(context, jdbcConverter, dialect), |
| 52 | + new InsertStrategyFactory(operations, new BatchJdbcOperations(operations.getJdbcOperations()), dialect)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + @Bean |
| 57 | + @Second |
| 58 | + public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, @Second NamedParameterJdbcOperations operations, |
| 59 | + @Lazy RelationResolver relationResolver, JdbcCustomConversions conversions, Dialect dialect) { |
| 60 | + |
| 61 | + JdbcArrayColumns arrayColumns = dialect instanceof JdbcDialect ? ((JdbcDialect) dialect).getArraySupport() |
| 62 | + : JdbcArrayColumns.DefaultSupport.INSTANCE; |
| 63 | + DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory(operations.getJdbcOperations(), arrayColumns); |
| 64 | + |
| 65 | + return new BasicJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory, |
| 66 | + dialect.getIdentifierProcessing()); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + @Bean |
| 71 | + @Second |
| 72 | + public Dialect jdbcDialect(@Second NamedParameterJdbcOperations operations) { |
| 73 | + return DialectResolver.getDialect(operations.getJdbcOperations()); |
| 74 | + } |
| 75 | + |
| 76 | + @Bean |
| 77 | + @Second |
| 78 | + TransactionManager secondTransactionManager(@Second DataSource dataSource) { |
| 79 | + return new DataSourceTransactionManager(dataSource); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments