SpringBootJPA实现增删改查

一、目录展示

  

 二、导入依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--jpa-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
View Code

三、Student实体类

package com.zn.entity;

import javax.persistence.*;

@Entity
@Table(name = "studentinfo")
public class Student {
    @Id
    @GeneratedValue//默认自增
    private Integer stu_id;
    @Column(name = "stu_name")
    private String stu_name;

    private Integer sex;

    private Integer age;

    public Student() {
    }

    public Student(String stu_name, Integer sex, Integer age) {
        this.stu_name = stu_name;
        this.sex = sex;
        this.age = age;
    }

    public Integer getStu_id() {
        return stu_id;
    }

    public void setStu_id(Integer stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
View Code

四、StudentDao

  

 五、StudentService

  

 六、StudentServiceImpl  

package com.zn.service.impl;

import com.zn.dao.StudentDao;
import com.zn.entity.Student;
import com.zn.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentDao studentDao;

    @Override
    public Student insertStudent(Student student) {
        return studentDao.save(student);
    }

    @Override
    public Student updateStudent(Student student) {
        return studentDao.save(student);
    }

    @Override
    public void deleteStudent(Integer id) {
        studentDao.deleteById(id);
    }

    @Override
    public Iterable<Student> getAll() {
        return studentDao.findAll();
    }
}
View Code

七、StudentController

package com.zn.controller;

import com.zn.entity.Student;
import com.zn.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/studentController")
public class StudentController {
    @Resource
    StudentService studentService;

    //新增数据
    @RequestMapping("/saveAllStudent")
    public Student saveAllStudent(){
        Student student=new Student();
        student.setStu_name("张三");
        student.setAge(16);
        student.setSex(1);
        return studentService.insertStudent(student);
    }

    //修改数据
    @RequestMapping("/updateStudent")
    public Student updateStudent(){
        Student student=new Student();
        student.setStu_id(2);
        student.setStu_name("张大三");
        student.setAge(20);
        student.setSex(1);
        return studentService.updateStudent(student);
    }

    //删除数据
    @RequestMapping("/deleteStudent")
    public void deleteStudent(){
        studentService.deleteStudent(2);
    }

    //查询数据
    @RequestMapping("/getAllStudent")
    public Iterable<Student> getAllStudent(){
        return studentService.getAll();
    }
}
View Code

八、application.properties配置文件

  

 九、测试类StartJPA

  

 十、效果展示

  (1)添加

    

     

      (2)修改

    

     

   (3)删除

    

     

   (4)查询列表

    

  

猜你喜欢

转载自www.cnblogs.com/Zzzzn/p/12029741.html