首页 > 其他分享 >springdatajpa基本管理实现

springdatajpa基本管理实现

时间:2023-03-06 18:34:20浏览次数:41  
标签:基本 springdatajpa return 实现 public import studentRepository id

springdatajpa

近期由于想提升自己,所以简单的学了学springdatajpa,下面将简单利用springdatajpa实现数据的基础管理

第一步 导入依赖

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

第二步 实体类

package com.example.springdatajpa.student;

import jakarta.persistence.*;
import lombok.Data;

@Data
@Entity
@Table(name = "students")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;


    private String name;

    private String stunum;

    private String phone;

}

第三步 StudentRepository(类似于Mapper,只不过省了SQL语句)

注意这里虽然省下了SQL语句,但是条件方法名必须findBy开头,后面加条件用and连接,例如findByNameAndPhone

package com.example.springdatajpa.student;


import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;


public interface StudentRepository extends JpaRepository<Student, Integer> {

    List<Student> findByNameAndPhone(String name,String phone);
}

第四步 StudentService

package com.example.springdatajpa.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class StudentService {

    private final StudentRepository studentRepository;

    @Autowired
    public StudentService(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    public Student addStudent(Student student){
        return studentRepository.saveAndFlush(student);
    }

    public void deleteById(Integer id){
        studentRepository.deleteById(id);
    }

//    public Student UpdateStudent(Student student){
//        return studentRepository.saveAndFlush();
//    }

    public Optional<Student> getById(Integer id){
        return studentRepository.findById(id);
    }

    public List<Student> getAll(){
        return studentRepository.findAll();
    }

    public List<Student> findByNameAndPhone(String name,String phone){
        return studentRepository.findByNameAndPhone(name, phone);
    }
}

第五步

package com.example.springdatajpa.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/stus")
public class StudentController {

    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping()
    public List<Student> getAll(){
        return studentService.getAll();
    }

    @GetMapping("/{id}")
    public Optional<Student> getById(@PathVariable Integer id){
        return studentService.getById(id);
    }

    @GetMapping("/condition")
    public List<Student> getByCondition(String name,String phone){
        return studentService.findByNameAndPhone(name, phone);
    }

    @PostMapping("/addOrUpdate")
    public Student addOrUpdateStudent(@RequestBody Student student){
        return studentService.addStudent(student);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Integer id){
        studentService.deleteById(id);
    }

}

总结

springdatajpa类似于mybatis-plus,只不过省下了SQL语句。

标签:基本,springdatajpa,return,实现,public,import,studentRepository,id
From: https://www.cnblogs.com/beijie/p/17184921.html

相关文章

  • jQuery实现省级联动效果——源码
    一、效果图二、index.html<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>jQuery实现省级联动</title></head><body><div><select......
  • 项目中怎么实现分库分表
    当单表行数超过500W行或者单表数据容量超过2G时,就会对查询性能产生较大影响,这个时候建议对表进行优化。其实500W数据只是一个折中的值,具体的数据量和数据库服务器配置......
  • MVCC 是什么?InnoDB 是如何实现 MVCC 机制的?
    概念MVCC全称Multi-VersionConcurrencyControl,多版本并发控制。指维护一个数据的多个版本,使得读写操作没有冲突。解决问题MVCC主要解决的问题是可以保证MySQL在读的......
  • 记录--uni-app中安卓包检查更新、新版本下载、下载进度条显示功能实现
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助需求描述如果想要做一个app的话,可以有很多种选择方案,uni-app是其中的一个性价比高一些(坑多一些)的方案。......
  • C/C++ 数据结构链栈的基本操作实现
    #include<iostream>#include<string.h>usingnamespacestd;typedefintSElemType;typedefstructStackNode{SElemTypedata;structStackNode*next;......
  • JUnit4基本使用
    JUnit4的配置和基本使用配置1.从网上下载jar包,导入Eclipse中从maven仓库中下载,官网https://mvnrepository.com/artifact/junit/junit导入Eclipse中,导入流程右键项目......
  • 网页基本 标签
    <doctypehtml><html><head>  <metacharset="utf-8">  <title>study</title>  <style>  </style></head><!--&nbsp;表示空格><表示<><stron......
  • # JUnit4的配置和基本使用
     ##配置###1.从网上下载jar包,导入Eclipse中-从maven仓库中下载,官网https://mvnrepository.com/artifact/junit/junit-导入Eclipse中,导入流程右键项目-----BuildPa......
  • element UI 里面的switch怎么在表格里实现单独控制一行的开关
        参考:https://segmentfault.com/q/1010000009522360......
  • JUnit4基本使用
    JUnit4的配置和基本使用配置1.从网上下载jar包,导入Eclipse中从maven仓库中下载,官网https://mvnrepository.com/artifact/junit/junit导入Eclipse中,导入流程右键项目......