JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。 目录 一、初始化springboot项目
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
目录
一、初始化springboot项目
二、引入依赖文件
三、编写配置文件
四、编写测试案例
User.java
UserDao.java
UserService.java
UserServiceimpl.java
JpaController.java
五、运行结果展示
一、初始化springboot项目
二、引入依赖文件
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
三、编写配置文件
注意:需要提前建立好数据库,表可以不建。
#通用数据源配置spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_jpa?charset=utf8mb4&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# Hikari 数据源专用配置
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
# JPA 相关配置
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
四、编写测试案例
User.java
package com.csdn.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Table(name = "AUTH_USER") //表名
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id //主键ID
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键自增
private Long id;
@Column(length = 32) //字段最大允许长度
private String username;
@Column(length = 32)
private String password;
}
UserDao.java
package com.csdn.dao;import com.csdn.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends JpaRepository<User, Long> {
}
UserService.java
package com.csdn.service;import com.csdn.entity.User;
import java.util.List;
public interface UserService {
public List<User> users();
}
UserServiceimpl.java
package com.csdn.service.impl;import com.csdn.dao.UserDao;
import com.csdn.entity.User;
import com.csdn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceimpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> users() {
User user1 = new User(1L, "admin1", "123456");
User user2 = new User(2L, "admin2", "123456");
userDao.save(user1);
userDao.save(user2);
List<User> users = userDao.findAll();
return users;
}
}
JpaController.java
package com.csdn.controller;import com.csdn.entity.User;
import com.csdn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class JpaController {
@Autowired
private UserService userService;
@GetMapping("/users")
public List<User> users() {
return userService.users();
}
}
五、运行结果展示
我们在启动日志中可以看到数据库的创建过程。
然后我们访问:localhost:8080/users
到这里,一个简单的整合过程就完成了。如果本篇文章对你有帮助,请一键三连支持,谢谢。