SpringBoot数据访问——整合JPA
文章目录
0.JPA简介
JPA:Java persistence API(Java持久化接口),是定义了对象关系映射(ORM)以及实体对象持久化的标准接口,在SpringBoot中JPA基于Hibernate。
1.导入依赖
maven项目中的pom.xml,添加依赖如下:处了springboot的启动器外还有:JPA,Web模块,MySQL,Lombok
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!-- Web 模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- Java效率工具Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2.配置数据连接
数据源使用默认的,数据库配置如下:
默认的application.properties
#mysql配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/springboot_demo
spring.datasource.username=账号
spring.datasource.password=密码
yml格式:
#配置数据源
spring:
datasource:
username: 账号
password: 密码
url: jdbc:mysql://*.*.*.*:3306/springboot_demo
driver-class-name: com.mysql.jdbc.Driver
3.初始化测试数据表
这里就用了一个简单了表,包含id(主键且自增),english,chinese这三个字段。
4.使用JPA编写操作数据的代码
4.1.编写数据库中表对应的实体类Bean
entity包下建立word类:
import lombok.Data;
import javax.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "word")//指明该类和数据表映射;省略则默认为类名首字母小写
public class Word {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键自增
private Integer id;
@Column(name = "english")//这是和数据表对应的一个列;省略则列名=属性名
private String english;
@Column
private String chinese;
}
这里使用了Lombok插件中的注解 @Data
- @Data注解在类自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
- @AllArgsConstructor 所有参数的构造方法
- @NoArgsConstructor 无参构造方法
其他注释均为JPA的:
- @Table(name = “word”)//指明该类和数据表映射;省略则默认为类名首字母小写
- @Id标注为主键
- @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
- @Column(name = “english”)//标注为列;省略则列名=属性名
更多关于Lombok的安装与使用可查看:IntelliJ IDEA Java高率开发插件Lombok
4.2 编写操作数据的接口repository
首先JPA提供了三种repository接口,各自封装了不同的基本功能:
- CrudRepository<T, ID extends Serializable>:最基本的对实体类的添删改查操作
- PagingAndSortingRepository<T, ID extends Serializable>:基本的分页与排序功能
- JpaRepository<T, ID extends Serializable>:提供了JPA的相关功能
repository包下建WordRepository接口类:
继承自CrudRepository<Word, Integer>,泛型的第一为上述的实体类Bean,第二个为主键数据类型,继承了该接口后便拥有了基本的CRUD操作。
import com.piao.springboot_jpa.entity.Word;
import org.springframework.data.jpa.repository.JpaRepository;
//操作数据的接口repository 使其拥有常规CRUD功能
public interface WordRepository extends CrudRepository<Word, Integer> {
}
其中CrudRepository接口源码如下,看看有什么直接可用的方法:(注释简单说明)
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S var1);//保存数据,用于插入或修改数据
<S extends T> Iterable<S> saveAll(Iterable<S> var1);//保存整个可迭代的集合中的数据,比如list<Word>
Optional<T> findById(ID var1);//按照id查找
boolean existsById(ID var1);//按照id查找,有返回true,否则false
Iterable<T> findAll();//查询所有,以可迭代的集合返回,比如List<word>
Iterable<T> findAllById(Iterable<ID> var1);//查询集合中所有符和条件的数据,以可迭代的集合返回,比如List<word>
long count();//数据量计数
void deleteById(ID var1);//按照id删除
void delete(T var1);//删除指定实体类对应的数据
void deleteAll(Iterable<? extends T> var1);//删除集合中所有符和条件的数据
void deleteAll();//全删,慎用!!
}
其他两个在此不赘述,自行查看源码看看,都是一些常规方法,帮我们省去了开发时间。
4.3 配置JPA
JPA底层由Hibernate实现,有的配置其实就是配置Hibernate相关的东西。
yml格式:
show-sql: true使其在控制台显示操作数据的SQL语句;
ddl-auto: update 更新或创建数据表,即使上述没有初始化数据表,JPA也会根据实体类的注释创建对应的数据表。
#配置数据源
spring:
datasource:
username: root
password: 23573
url: jdbc:mysql://47.107.229.140:3306/springboot_demo?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
# 更新或创建数据表
ddl-auto: update
show-sql: true
# 控制台显示操作数据的SQL语句
4.4 编写控制层实现CRUD
功能包括基本的CRUD操作:
- 添加单词信息
- 根据id查询单词信息
- 根据id更新单词信息
- 根据id删除单词信息
- 查询所有单词
@Autowired
WordRepository wordRepository;
将业务对象WordRepository注入后便可直接使用自带的CRUD方法操作数据。
这里加了详细且原始的数据校验判空处理,当然有更优雅的方式…这里就不展开了。
推荐这位大佬的博客:啥?听说你还在手写复杂的参数校验?
package com.piao.springboot_jpa.controller;
import com.piao.springboot_jpa.entity.Word;
import com.piao.springboot_jpa.repository.WordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class JPAController {
//注入业务对象 WordRepository
@Autowired
WordRepository wordRepository;
//存储预返回页面的结果对象
private Map<String, Object> result;
//添加单词信息
@PostMapping("/insertWord")
@ResponseBody
public Map<String, Object> insertWord(Word word) {
result = new HashMap<>();
//数据校验 判空!
if (null == word) {
result.put("message", "插入的单词为空");
return result;
}
if (null == word.getChinese() || "".equals(word.getEnglish())) {
result.put("message", "插入的单词中文为空或者为空字符串");
return result;
}
if (null == word.getChinese() || "".equals(word.getChinese())) {
result.put("message", "插入的单词中文为空或者为空字符串");
return result;
}
wordRepository.save(word);//插入单词,插入数据使用save
result.put("message", "插入单词成功");
return result;
}
//根据id查询单词信息
@GetMapping("/getWordById")
@ResponseBody
public Map<String, Object> getWordById(Integer id) {
result = new HashMap<>();
//数据校验 判空!
if (null == id) {
result.put("message", "传入id为空");
return result;
}
result.put("word", wordRepository.findById(id));//按照id(主键)查询getOne()
result.put("message", "查询单词成功");
return result;
}
//根据id更新单词信息
@PostMapping("/updateWordById")
@ResponseBody
public Map<String, Object> updateWordById(Integer id, String english, String chinese) {
result = new HashMap<>();
//数据校验 判空!
if (null == id) {
result.put("message", "更新的单词id为空");
return result;
}
if (null == english || "".equals(english)) {
result.put("message", "更新的单词中文为空或者为空字符串");
return result;
}
if (null == chinese || "".equals(chinese)) {
result.put("message", "更新的单词中文为空或者为空字符串");
return result;
}
wordRepository.save(new Word(id, english, chinese));
result.put("message", "更新单词成功");
return result;
}
//根据id删除单词信息
@PostMapping("/deleteWordById")
@ResponseBody
public Map<String, Object> deleteWordById(Integer id) {
result = new HashMap<>();
//数据校验 判空!
if (null == id) {
result.put("message", "输入的单词id为空");
return result;
}
wordRepository.deleteById(id);//根据id删除单词信息
result.put("message", "删除单词成功");
return result;
}
//查询所有单词
@GetMapping("/getAllWord")
@ResponseBody
public Map<String, Object> getAllWord() {
result = new HashMap<>();
result.put("wordList",wordRepository.findAll());//查询所有单词
result.put("message", "查询所有单词成功");
return result;
}
}
这五个接口均用postman测试过,操作数据正常,数据校验的判空处理正常。