标签:save 开源框架 repos response ice turn ike data info
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA可以极大的简化JPA的写法,可以爱几乎不用写实现的情况下,实现对数据库的访问操作。除了CRUD外,还包括分页,排序等一些常用的功能,
首先我们需要清除的是Spring data是一个开源框架,在这个框架中Spring Data JPA只是这个框架中的一个模块,所以名称才叫Spring Data JPA。如果单独使用JPA开发,你会发现这个代码量和使用JDBC开发一样有点烦人,所以Spring Data JPA的出现就是为了简化JPA的写法,让你只需要编写一个接口继承一个类就能实现CRUD操作了。
<!-- 引入mysql需要的依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 引入spring data jpa需要的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
#Spring Data JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent-output=true spring.jpa.database=mysql
package com.wn.springbootproject01.entity; import javax.persistence.*; @Entity @Table(name = "Grade") public class Grade { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer grade_id; private String grade_name; public Grade(Integer grade_id, String grade_name) { this.grade_id = grade_id; this.grade_name = grade_name; } public Grade(){ } 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; } }
package com.wn.springbootproject01.dao; import com.wn.springbootproject01.entity.Grade; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface GradeDao extends CrudRepository<Grade,Integer> { }
package com.wn.springbootproject01.service; import com.wn.springbootproject01.entity.Grade; public interface GradeService { //新增 public Grade getinter(Grade grade); //修改 public Grade getupdate(Grade grade); //删除 public void getdel(Integer id); //查询 public Iterable<Grade> getAll(); }
package com.wn.springbootproject01.service.impl; import com.wn.springbootproject01.dao.GradeDao; import com.wn.springbootproject01.entity.Grade; import com.wn.springbootproject01.service.GradeService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class GradeServiceImpl implements GradeService { @Resource private GradeDao dao; @Override public Grade getinter(Grade grade) { return dao.save(grade); } @Override public Grade getupdate(Grade grade) { return dao.save(grade); } @Override public void getdel(Integer id) { dao.deleteById(id); } @Override public Iterable<Grade> getAll() { return dao.findAll(); } }
package com.wn.springbootproject01.controller; import com.wn.springbootproject01.entity.Grade; import com.wn.springbootproject01.service.GradeService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @Controller @RequestMapping("/grade") public class GradeController { //植入dao层对象 @Resource private GradeService service; //新增 @RequestMapping("/getinter") @ResponseBody public Grade getinter(){ Grade grade=new Grade(); grade.setGrade_name("小明"); return service.getinter(grade); } //修改 @RequestMapping("/getupdate") @ResponseBody public Grade getupdate(){ Grade grade=new Grade(); grade.setGrade_id(1); grade.setGrade_name("小红"); return service.getupdate(grade); } //删除 @RequestMapping("/getdel") @ResponseBody public String getdel(){ service.getdel(11); return "success"; } //查询 @RequestMapping("/getAll") @ResponseBody public Iterable<Grade> getAll(){ return service.getAll(); } }
标签:save 开源框架 repos response ice turn ike data info
原文地址:https://www.cnblogs.com/wnwn/p/12028928.html