忿忿的泥巴坨 发表于 2024-5-18 10:59:03

Spring Boot应用中如何动态指定命据库,实现不同用户不同数据库的场景

当在 Spring Boot 应用步伐中利用Spring Data JPA 举行数据库操作时,设置Schema名称是一种常见的做法。然而,在某些情况下,模式名称需要是动态的,大概会在应用步伐运行时发生变化。比如:需要做数据隔离的SaaS应用。
所以,这篇博文将资助您解决了在 Spring Boot 应用步伐中如何设置动态 Schema。
问题场景

假设,您的应用步伐是一个SaaS软件,需要为多个租户提供服务,每个租户都需要一个单独的数据库架构。
在这种情况下,在应用步伐属性中对Shema名称举行硬编码是不太大概的,如许有一个用户新增,就要去写代码更新。
所以,为了应对这一挑战,我们将探索一种允许在运行时动态设置模式名称的解决方案。
我们创建了一个高质量的技术交换群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。
代码案例

让我们创建一个 Spring Boot 项目 首先设置一个具有必要依靠项的新 Spring Boot 项目。在项目设置中包罗 Spring Web、Spring Data JPA 和关于数据库的依靠项。
界说Spring Data JPA的实体类,例如:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product")
public class Product {
    @Id
    private Long id;
    private String name;
    private double price;

}创建数据访问接口,以便您的实体提供 CRUD 操作:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

}创建一个用来处理业务逻辑,包罗与数据库交互的方法:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    private final ProductRepository productRepository;

    @Autowired
    public ProductService(ProductRepository productRepository) {
      this.productRepository = productRepository;
    }

    public List<Product> getAllProducts() {
      return productRepository.findAll();
    }

}实现API接口:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
      this.productService = productService;
    }

    @GetMapping
    public List<Product> getAllProducts() {
      return productService.getAllProducts();
    }

}重点:设置动态Schema
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DynamicSchemaConfig {

    @Value("${custom.schema.name}")
    private String customSchemaName;

    @Bean
    public DataSource dataSource() {
      String dataSourceUrl = "jdbc:mysql://localhost:3306/" + customSchemaName;
      return DataSourceBuilder.create().url(dataSourceUrl).build();
    }
}重新打包该Spring Boot应用,然后当我们要为不同用户利用完全隔离的数据库、完全隔离的应用的时候,只需要通过下面的启动命令,就能轻松实现了:
java -jar -Dcustom.schema.name=my_dynamic_schema your-application.jar这里,通过启动命令中的custom.schema.name参数,就能去指定不同的数据库Schema,而应用步伐端都是同一套代码,由于启动了新的Spring Boot应用,所以应用端进程也是完全隔离的。这种方法,对于利用Spring Boot构建需要一定资源隔离SaaS软件来说,是个不错的实现方案。
接待关注我的公众号:步伐猿DD。第一时间相识前沿行业消息、分享深度技术干货、获取优质学习资源

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Spring Boot应用中如何动态指定命据库,实现不同用户不同数据库的场景