springBoot+Hibernate(Jpa)多数据源配置与使用

本文详细介绍了如何在SpringBoot项目中配置和使用多个数据源,包括项目创建、资源文件配置、多资源库配置及功能实现。通过定义数据源配置类,实现了对主数据库和从数据库的连接管理,提供了核心的控制层和服务层代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习的过程中,大多时候项目与数据库都在本机上,使用hibernate或者mybatits加上简单的配置就能够打通程序到数据库路径,让程序能够访问到数据库。然而在一些情况下,我们不仅需要访问本机的数据库,还需要访问到另外一个数据库中的资源。本文书写的目的便在于解决访问多资源库的问题,更多的是为了交流与学习。学习,源码是很重要的。文末给出了我的Github中的源码链接,感兴趣的可以下载阅览。

一、项目创建与资源文件配置

建立一个初始的spring项目的方式在我的另一篇文章《springboot整合mybatis注解版与XML配置版》已经详细介绍了,在这里就不多赘述。当然,网上也有很多详细的教程,你也可以自行搜索阅览。新建好的项目的pom.xml文件中已经集成了必要的依赖包。本文所用到的pom.xml文件如下。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.40</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>


        <!--部署热启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </depend
### 配置开发基于 Spring Boot 和 Hibernate JPA 的 Maven 项目 #### 添加依赖项 为了使Spring Boot项目能够利用JPAHibernate功能,在`pom.xml`文件中加入`sprig-boot-starter-data-jpa`依赖是必要的操作[^1]。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 此依赖不仅引入了JPA API,同时也集成了Hibernate作为默认的持久层框架实现。对于数据库支持方面,Spring Boot提供了诸如H2、MySQL以及PostgreSQL等多种选择,仅需在配置文件内指定相应参数即可完成数据源设置。 #### 数据库连接配置 针对不同的关系型数据库管理系统(RDBMS),可以在`application.properties`或`application.yml`文件中定义具体的连接属性: - 对于采用`.properties`格式的情况: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.jpa.hibernate.ddl- 若偏好使用YAML语法,则可如下所示进行设定: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC username: root password: password jpa: hibernate: ddl-auto: update ``` 上述配置示例展示了如何为MySQL数据库建立链接,并启用了DDL自动生成机制来同步模式变更至目标数据库表结构中。 #### 创建实体类仓库接口 当完成了基础环境搭建之后,下一步便是构建领域模型——即所谓的“实体”。这些Java Bean对象映射到数据库中的表格记录;此同时,还需设计相应的存储库接口用于执行CRUD及其他查询逻辑。例如,假设存在一个名为`User`的数据表,那么对应的程序单元可能看起来像这样[^2]: ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // Getters and Setters... } ``` 接着定义仓储组件以供业务服务调用访问底层资源: ```java import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository<User, Long> {} ``` 这种做法遵循了面向接口编程的原则,简化了应用程序内部各层次间的协作方式。
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值