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