SpringBoot与JPA

JPA是什么

  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操作了。

JPA实现数据库增删改查

  步骤一:导入依赖

<!--  引入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>

  步骤二:application.properties文件

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

  步骤四:创建dao层

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> {
}

  步骤五:创建service接口层

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

}

  步骤六:创建serviceImpl实现层

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

  步骤七:创建controller层

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

}

  步骤八:实现结果

      新增:

          SpringBoot与JPA

           SpringBoot与JPA

      修改:

          SpringBoot与JPA

           SpringBoot与JPA

      删除:

          SpringBoot与JPA

        SpringBoot与JPA

      查询:

          SpringBoot与JPA

相关推荐