SpringBoot系列之Spring Data Jpa集成教程
Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下的子项目有如下图所示:
官方也有提供了英文版的文档,具体可以参考Spring data官方文档
JPA是一个规范,并非具体的实现框架,而Spring Data JPA就是依赖于Hibernate JPA的实现
ok,接着通过例子的方式介绍Springboot集成Spring Data JPA的方法,进行实验,要先创建一个Initializer工程,如图: 在这里插入图片描述在这里插入图片描述
在这里插入图片描述
新建项目后,会自动加上如下配置,,如果你的mysql服务器是5.7版本的,建议指定mysql-connector-java版本,Druid也需要自己加上,pom配置参考:
<properties>
<java.version>1.8</java.version>
<druid.version>1.1.2</druid.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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</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>
新建数据库和数据表:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springboot` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `springboot`;
/*Table structure for table `sys_user` */
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`userId` int(10) NOT NULL,
`username` varchar(20) NOT NULL,
`sex` char(10) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
PRIMARY KEY (`userId`),
UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `sys_user` */
insert into `sys_user`(`userId`,`username`,`sex`,`password`) values (1,'admin','man','11');
新建一个bean类,代码:
package com.example.springboot.jpa.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
@Entity
@Table(name = "sys_user")
//Springboot2.2.1集成的Spring data jpa比较新,需要加上如下配置
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class User{
@Id //主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
private Integer userId;
@Column(name = "username",length = 20) //这是和数据表对应的一个列
private String username;
@Column
private String sex;
@Column
private String password;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
实现JpaRepository接口,当然Spring data jpa还提供了很多接口,具体可以参考我之前博客:JPA系列之Spring Data JPA系列之入门教程,本博客只介绍在Springboot集成spring data jpa,具体使用还是需要看我之前博客,才可以再来学习本文
package com.example.springboot.jpa.repository;
import com.example.springboot.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Integer> {
}
不写Service类,直接写个controller类进行测试
package com.example.springboot.jpa.controller;
import com.example.springboot.jpa.entity.User;
import com.example.springboot.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/{userId}")
public User getUser(@PathVariable("userId") Integer userId){
User user = userRepository.getOne(userId);
return user;
}
@GetMapping("/user")
public User insertUser(User user){
User save = userRepository.save(user);
return save;
}
}
application.yml配置,当然还要配置数据库JDBC和连接池,具体参考我的博客:SpringBoot系列之集成Druid配置数据源监控,yaml不熟悉可以,参考我之前博客:SpringBoot系列之YAML配置用法学习笔记
spring:
jpa:
hibernate:
# 更新或者创建数据表结构
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# 控制台显示SQL
show-sql: true
启动Postman来调试接口,测试通过:
在这里插入图片描述
代码例子下载:github下载链接
网友评论