<?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>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.kitey</groupId>
<artifactId>demo06_boot_data_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo06_boot_data_jpa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中使用的注解:
@Entity :表明为一个实体类
@Table(name = “tb1_user”) :表明自动创建表时的表名
@Id :这个属性为主键
@GeneratedValue(strategy = GenerationType.IDENTITY):这个属性,为自增长属性
@Column(name = “last_name”, length = 50):设置这个属性在数据库创建时的名称与长度。
package cn.kitey.jpa.entity;
import javax.persistence.*;
/**
* 编写一个实体类
* 使用JPA注解配置映射关系
*/
@Entity //告诉JPA这是一个实体类
@Table(name = "tb1_user")
public class User {
@Id //表明这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //表明为自增主键
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}
继承JpaRepository 则会具有JpaRepository类中的CRUD操作等
package cn.kitey.jpa.repository;
import cn.kitey.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* 继承JpaRepository来完成对数据库的操作
*/
public interface UserRepository extends JpaRepository<User, Integer> {
}
1.首先配置了数据连接的相应信息
2. jpa:
hibernate:
ddl-auto: update 配置为当程序执行时表的创建与更新
3.show-sql: true :表示当执行sql语句的时候会在控制台进行打印
spring:
datasource:
url: jdbc:mysql:///jpa?serverTimezone=UTC
password: 25002500
username: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
# 更新或者创建数据表
ddl-auto: update
# 控制台打印sql语句
show-sql: true
我这就实现了两个方法:
数据的查询,以及数据的插入
package cn.kitey.jpa.controller;
import cn.kitey.jpa.entity.User;
import cn.kitey.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* 带返回值的控制类
*/
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id){
User one = userRepository.findById(id).get();
return one;
}
@GetMapping("/user")
public User insertUser(User user){
User save = userRepository.save(user);
return save;
}
}
首先看一下数据库
执行数据查询功能
控制台输出
执行数据插入
控制台打印
再看一下数据库内容
以上就是SpringBoot关于SpringData JPA的简单介绍了