点击查看数据访问demo:LearnSpringBoot06DataJPA
Spring Data JPA - Reference 文档
简介
Spring Data的JPA模块包含一个允许定义存储库bean的自定义名称空间。它还包含JPA特有的某些特性和元素属性。通常,可以使用repositories元素来设置JPA存储库:
点击查看更多的SpringBoot教程
一、引入spring-boot-starter-data-jpa
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二、application.yml配置文件
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://192.168.0.103:3307/jpadriver-class-name: com.mysql.cj.jdbc.Driverjpa:
# database: mysql
# database-platform: org.hibernate.dialect.MySQLDialecthibernate:# 更新或者创建数据表结构ddl-auto: update# naming:# physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl# generate-ddl: true 开启逆向工程,存在数据库的表,然后数据库表可以生成实体类,只有在开发阶段设置trueshow-sql: true # 控制台显示SQL
# properties:
# hibernate:
# hbm2ddl:
# auto: update
三、使用JPA注解配置映射关系
User.java实体类代码
package com.example.learnspringboot06datajpa.entity;import jakarta.persistence.*;//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "tbl_user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user;
public class User {@Id@GeneratedValue(strategy = GenerationType.AUTO)//自增主键private Integer id;@Column(name = "last_name ", length = 50)//这是和数据表对应的一个列private String lastName;@Column //省略默认列名就是属性名private String email;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}
四、创建Repository接口继承JpaRepository来完成对数据库的操作
UserRepository.java接口代码
package com.example.learnspringboot06datajpa.repository;import com.example.learnspringboot06datajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;//继承JpaRepository来完成对数据库的操作
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {//<T, ID> T代表 entity对象,ID 对应表主键类型
}
五、测试结果
jpa数据库
项目运行后,在浏览器访问http://localhost:8081/user/2
控制台日志
在浏览器访问
http://localhost:8081/user?lastName=test&email=test01@test.com
控制台日志
六、pom.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>LearnSpringBoot06DataJPA</artifactId><version>0.0.1-SNAPSHOT</version><name>LearnSpringBoot06DataJPA</name><description>LearnSpringBoot06DataJPA</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
<!-- </dependency>--><!--改完POM后不生效法1:右击POM文件-Maven-Reload project--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>