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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?