Basic steps for JPA to access database

Keywords: Database Lombok Spring JDBC

Basic steps for JPA to access database:

1. Define the Table in the Entity class mapping database. The key is @ Entity, @ Table annotation Entity class, @ Entity annotation marks the Entity class of JPA, @ Table annotation indicates which Table in the database corresponds to the Entity class. @The Id annotation class indicates the primary key in the corresponding Table, and the @ Column annotation Table indicates the members corresponding to other columns. @The Id annotation must exist.

package com.japtest.jpaapplication.bean;

import lombok.Data;

import javax.persistence.*;


@Data   //lombok, no need to define a bunch of getter s and setter s
@Entity //Indicates that the class is an entity class of JPA
@Table(name="student")  //Indicates that the entity class is mapped to the studeng table in the database. If no name is specified, the class name is specified by default.
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column
    private String name;
    @Column
    private Integer age;
}

2. Define the interface to operate the entity class. The interface must inherit from the Repository or its subclass. General relational databases use jparepository. The jparepository interface supports CRUD operations and paging queries for relational databases. Jparepository class is a generic class with two parameters. The first parameter is the type of entity class operated by the interface, and the second parameter is the type of primary key of entity class operated by the interface.

package com.japtest.jpaapplication.repository;

import com.japtest.jpaapplication.bean.Student;
import org.springframework.data.jpa.repository.JpaRepository;

//The JpaRepository generic class has two generic parameters. The first parameter is the entity class of the interface operation, which is Student. The second parameter is the primary key type of the entity class described as operation. The primary key of the Student class is id, which is Integer
public interface StudentRepository extends JpaRepository<Student,Integer> {
}

3. Configure the default spring data source and the operation database of entity class. This file can be configured with either the yml file or the properties file. The configuration and writing methods of yml and properties are different, but the meaning is the same.

# yml mode configuration
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/palmap?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

# The following configuration will create or update the table structure corresponding to the defined entity class in the database when it is turned on
#  jpa:
#    hibernate:
#      ddl-auto: update

#    show-sql: true

4. In the Controller, you can use the interface in step 2 to operate the database.

package com.japtest.jpaapplication.controller;

import com.japtest.jpaapplication.bean.Student;
import com.japtest.jpaapplication.repository.StudentRepository;
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 StudentController {
    @Autowired
    private StudentRepository studentRep;

    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id)
    {
        return studentRep.findById(id).get();
    }

    @GetMapping("/student")
    public Student insertStudent(Student student)
    {
        Student s=studentRep.save(student);
        return s;
    }
}

Source address
Extraction code: g8wl

Published 7 original articles, won praise 0, visited 358
Private letter follow

Posted by maxrisc on Mon, 09 Mar 2020 02:59:29 -0700