zoukankan      html  css  js  c++  java
  • 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)查询列表

        

      

  • 相关阅读:
    查看.NET Core源代码通过Autofac实现依赖注入到Controller属性
    序列化二叉树
    把二叉树打印成多行
    按之字形顺序打印二叉树
    对称的二叉树
    JDK源码阅读顺序
    二叉树的下一个结点
    删除链表中重复的结点
    链表中环的入口结点
    字符流中第一个不重复的字符
  • 原文地址:https://www.cnblogs.com/Zzzzn/p/12029741.html
Copyright © 2011-2022 走看看