SpringBootJPA实现增删改查
Posted dabrk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBootJPA实现增删改查相关的知识,希望对你有一定的参考价值。
Grade实体类
@Entity public class Grade { @Id @GeneratedValue(strategy= GenerationType.AUTO) private Integer grade_id; private String grade_name; public Integer getGrade_id() { return grade_id; } public void setGrade_id(Integer grade_id) { this.grade_id = grade_id; } public String getGrade_name() { return grade_name; } public void setGrade_name(String grade_name) { this.grade_name = grade_name; } }
IGradeMapper
@Repository public interface IGradeMapper extends CrudRepository<Grade,Integer> { }
IGradeController
@RestController @RequestMappng("/IGradeController") public class IGradeController { @Resource private IGradeService iGradeService; //新增数据 @RequestMapping("/insertGrade") public Grade insertGrade(){ Grade grade=new Grade(); grade.setGrade_name("S1"); return iGradeService.insertGrade(grade); } @RequestMapping("/updateGrade") public Grade updateGrade(){ Grade grade=new Grade(); grade.setGrade_id(1); grade.setGrade_name("S2"); return iGradeService.updateGrade(grade); } @RequestMapping("/deleteGrade") public void deleteGrade(){ iGradeService.deleteGrade(1); } @RequestMapping("/getAllGrade") public Iterable<Grade> getAllGrade(){ return iGradeService.getAll(); } }
IGradeService
public interface IGradeService { public Grade insertGrade(Grade grade); public Grade updateGrade(Grade grade); public void deleteGrade(Integer id); public Iterable<Grade> getAll(); }
IGradeServcieImpl
@Service("iGradeService") public class IGradeServcieImpl implements IGradeService { @Resource private IGradeMapper iGradeMapper; @Override public Grade insertGrade(Grade grade) { return iGradeMapper.save(grade); } @Override public Grade updateGrade(Grade grade) { return iGradeMapper.save(grade); } @Override public void deleteGrade(Integer id) { iGradeMapper.delete(id); } @Override public Iterable<Grade> getAll() { return iGradeMapper.findAll(); } }
StartSpringBoot
@SpringBootApplication public class StartSpringBoot { public static void main(String[] args) { SpringApplication.run(StartSpringBoot.class,args); } }
以上是关于SpringBootJPA实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章
springboot整合JPA+MYSQL+queryDSL数据增删改查