Springboot从两个库中的两个表搜索,将两个表的数据用list显示出来
时间: 2024-03-19 11:43:41 浏览: 130
可以通过使用Spring Data JPA来实现从两个库中的两个表搜索,并将两个表的数据用list显示出来。下面是一个简单的实现示例:
1. 在application.properties文件中配置两个数据源:
```
spring.datasource.first.url=jdbc:mysql://localhost:3306/first_db
spring.datasource.first.username=root
spring.datasource.first.password=123456
spring.datasource.second.url=jdbc:mysql://localhost:3306/second_db
spring.datasource.second.username=root
spring.datasource.second.password=123456
```
2. 创建两个数据源的JPA配置文件:
```
@Configuration
@EnableJpaRepositories(
basePackages = "com.example.first.repository",
entityManagerFactoryRef = "firstEntityManagerFactory",
transactionManagerRef = "firstTransactionManager"
)
public class FirstDbConfig {
@Primary
@Bean(name = "firstDataSource")
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "firstEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("firstDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.first.entity")
.persistenceUnit("first")
.build();
}
@Primary
@Bean(name = "firstTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("firstEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
@Configuration
@EnableJpaRepositories(
basePackages = "com.example.second.repository",
entityManagerFactoryRef = "secondEntityManagerFactory",
transactionManagerRef = "secondTransactionManager"
)
public class SecondDbConfig {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("secondDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.second.entity")
.persistenceUnit("second")
.build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("secondEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
```
3. 创建两个实体类和两个repository类:
```
@Entity
@Table(name = "first_table")
public class FirstEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getter and setter
}
public interface FirstRepository extends JpaRepository<FirstEntity, Long> {
List<FirstEntity> findByName(String name);
}
@Entity
@Table(name = "second_table")
public class SecondEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getter and setter
}
public interface SecondRepository extends JpaRepository<SecondEntity, Long> {
List<SecondEntity> findByName(String name);
}
```
4. 创建一个service类,用于从两个库中的两个表搜索,并将两个表的数据用list显示出来:
```
@Service
public class DataService {
@Autowired
private FirstRepository firstRepository;
@Autowired
private SecondRepository secondRepository;
public List<Object> search(String name) {
List<Object> result = new ArrayList<>();
result.addAll(firstRepository.findByName(name));
result.addAll(secondRepository.findByName(name));
return result;
}
}
```
5. 创建一个controller类,用于接收请求并调用service类进行搜索:
```
@RestController
public class DataController {
@Autowired
private DataService dataService;
@GetMapping("/search")
public List<Object> search(@RequestParam String name) {
return dataService.search(name);
}
}
```
这样,当你访问/search?name=xxx时,就可以从两个库中的两个表中搜索,并将结果以list的形式返回了。
阅读全文