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
标签:SpringData,jpa,spring,数据库,MySQL,datasource,mysql,data,your From: https://www.cnblogs.com/mushang/p/18256144