-->

springdatajpa基本管理实现

springdatajpa

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

第一步 导入依赖

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

第二步 实体类

注意:这里一定要用 @GeneratedValue(strategy = GenerationType.IDENTITY) ,之前由于自己用的是@GeneratedValue(strategy = GenerationType.AUTO),所以一直在报Table 'db.stu_seq' doesn't exist 表不存在,查了好久发现是id自增没有设置好,错误原因请看下面文章:https://www.cnblogs.com/XiaoMingStudy1/p/15261581.html

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);
    }
}

第五步 StudentController

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,只不过省下了SQL语句。

posted @   ꧁ʚ星月天空ɞ꧂  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示