SpringBoot使用JPA进行数据访问

标签:#jpa##SpringBoot##web开发# 时间:2018/06/01 17:05:40 作者:小木

JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

Spring Data JPA 是Spring Data 的一个子项目,它通过提供基于JPA的Repository极大了减少了操作JPA的代码。使用这个访问操作数据库简直不要太简单。

SpringBoot使用JPA以来如下的对象:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>

在SpringBoot项目中使用JPA访问数据,涉及到三个类,一个是domain中对象,名字对应一个数据表,是一个Java领域模型,里面只有getters和setters方法,一个是Repository接口,继承自JpaRepository,它是操作数据库最主要的类,还有一个就是操作数据库的类了。举个例子,假设我们有一个数据表,有三个字段,分别是studentID,studentName和enabled,是学号、姓名和有效性。

studentID, studentName, is_visual

那么我们就有一个domian对象,叫student.java,定义如下。首先在类名中要定义注解@Entity,然后在对应主键的位置定义@Id,然后如果某个字段的定义和数据库中表不一致,例如is_visual,那么就要在这个字段上声明@Column的name,这样就能和数据库的表对应了。如下定义的这个Student类就和数据表中的Student表关联上了。

package com.localgou.admin.domain;

import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@DynamicUpdate
public class Student {

  @Id
  private long studentID;
  private String studentName;

  @Column(name = "is_visual")
  private int enabled;

  public long getStudentID() {
    return studentID;
  }

  public void setStudentID(long studentID) {
    this.studentID = studentID;
  }

  public String getStudentName() {
    return studentName;
  }

  public void setStudentName(String studentName) {
    this.studentName = studentName;
  }

  public int getEnabled() {
    return enabled;
  }

  public void setEnabled(int enabled) {
    this.enabled = enabled;
  }
}

接下来,我们要定义一个JPA接口,StudentRepository.java。这个类继承自JpaRepository,同时我们定义这个类所需要的类为Student,且主键是Long类型。和上面的定义一致。同时我们定义一个方法findByEnabled(int enabled),注意这个方法的By后面的名字和参数的名字必须与字段中enabled一致。

package com.localgou.admin.jpa;

import com.localgou.admin.domain.Student;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface StudentRepository extends JpaRepository<Student, Long> {

  List<Student> findByEnabled(int enabled);

  Page<Student> findByEnabled(int enabled, Pageable pageable);

}

接下来我们就可以使用这个StudentRepository操作数据库了。比如我们在StudentServiceImpl中做如下数据库操作:

package com.localgou.admin.service.impl;

import com.google.common.collect.Lists;
import com.localgou.admin.domain.Student;
import com.localgou.admin.jpa.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;

public class StudentServiceImpl {

  @Autowired
  StudentRepository studentRepository;

  public void test(){

    List<Student> studentList = studentRepository.findAll();
    Optional<Student> student = studentRepository.findById(10000l);
    student.ifPresent(System.out::println);
    List<Student> studentList2 = studentRepository.findByEnabled(1);
    long count = studentRepository.count();

    Pageable pageable = PageRequest.of(1, 10);
    List<Student>  students = Lists.newArrayList(studentRepository.findByEnabled(1, pageable));

  }

}

我们可以看到,即使不定义任何方法,我们也可以轻松的从数据表中根据主键查询,查询所有行数,查询所有的列表等等,如果需要按照某个字段查询,也只需要根据字段名称定义findByFieldName(Object fieldName)即可。注意FieldName不能用连词号连接”_”,字段定义因此要避免连词号,如果数据表是连词号,那就用@Column注解注明。甚至对于分页查询也很简单,只要定义一个Pageable对象即可。在StudentRepository中增加一个参数即可。

欢迎大家关注DataLearner官方微信,接受最新的AI技术推送