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语句。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通