目录
- SpringBoot和JPA初探
- 0.准备条件
- 1.创建JPA项目
- 2.项目
- 3.总结
SpringBoot和JPA初探
我们使用SpringBoot+JPA做一个简单的API接口演示,通过一个简单的例子让大家对Spring Data JPA有一个整体的认知。
0.准备条件
- IntelliJ IDEA
- jdk 1.8
- mysql 8.0
- maven 3.8.x
1.创建JPA项目
- 新建项目模块,IDEA默认的新增项目jdk版本较高,使用jdk1.8的建议使用阿里的项目初始化地址
https://start.aliyun.com
如下图所示:
在这里插入图片描述
设置完成后录入
- name [项目名称]
- location [位置]
- language 开发语言选择java
- type 选择maven
- group
- artifact
- package name 包名
- jdk
- 选择springboot版本和添加Spring Boot项目依赖。
- Logbook
- Spring Web
- Spring Data JPA
- Data Base
- 点击next,生成简单的示例项目
- 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>
<groupId>org.uz654</groupId>
<artifactId>jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpa</name>
<description>jpa</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.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-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>org.uz654.jpa.JpaApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.项目
- 新增UserController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.uz654.jpa.ch01.entity.User;
import org.uz654.jpa.ch01.repository.UserRepository;
@RestController
@RequestMapping(path="/api/v1")
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping(path = "user",consumes = {MediaType.APPLICATION_JSON_VALUE})
public User addNewUser(@RequestBody User user) {
return userRepository.save(user);
}
@GetMapping(path = "users")
public Page<User> getAllUsers(Pageable pageable) {
return userRepository.findAll(pageable);
}
}
addNewUser的功能就是将用户提交的用户信息保存到数据库的操作。
getAllUsers 分页查询所有用户的信息返回到前端。
2.新增UserRepository接口
import org.springframework.data.jpa.repository.JpaRepository;
import org.uz654.jpa.ch01.entity.User;
public interface UserRepository extends JpaRepository<User, String> {
}
3.新增实体User
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import javax.persistence.*;
import java.util.UUID;
@Data
@Entity
@Builder
@AllArgsConstructor
public class User {
@Id
private String id;
private String name;
private String email;
public User() {
this.id = UUID.randomUUID().toString();
}
}
- 配置文件配置信息
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=12345678
spring.jpa.generate-ddl=true
- 测试必然是必不可少的一部分,新增的JpaTest
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.Commit;
import org.springframework.util.Assert;
import org.uz654.jpa.ch01.entity.User;
import org.uz654.jpa.ch01.repository.UserRepository;
import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;
@DataJpaTest(properties = {
"spring.datasource.url=jdbc:mysql://localhost:3306/test",
"spring.datasource.username=root",
"spring.datasource.password=12345678",
"spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver",
"hibernate.dialect=org.hibernate.dialect.MySQL8Dialect"
})
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Transactional
public class JpaTest {
@Autowired
private UserRepository userRepository;
@Test
@Commit
public void testSaveUser(){
User user = userRepository.save(User.builder().id(UUID.randomUUID().toString()).name("uz654").email("test@163.com").build());
Assert.notNull(user);
List<User> users = userRepository.findAll();
System.out.println(users);
Assert.notNull(users);
}
}
整体的结构
3.总结
通过以上内容整体上了解SpringBoot+JPA的使用方式,方便快速入门整体上提升开发者的认知。
项目包见附件