SpringData初步学习-连接MySQL数据库

1.添加mysql驱动和spring-data-jpa依赖

复制代码
<dependencies>
    <!-- Spring Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
复制代码

2.配置数据源和实体管理器

配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

实体类

复制代码
import javax.persistence.*;
 
@Entity
public class YourEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // 其他字段和方法
}
复制代码

3.数据访问接口

import org.springframework.data.jpa.repository.JpaRepository;
 
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
    // 自定义查询方法
}

深入的学习笔记(其他博主的笔记,写的很深,后续看):https://lawsssscat.blog.csdn.net/article/details/103466076

posted @   暮商  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示