• springboot使用jpa案例


    1 创建entity实体类并生成数据库表

    @Entity
    @Table(name="student")
    public class Student {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer stuid;
        private String  stuname;
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuname() {
            return stuname;
        }
    
        public void setStuname(String stuname) {
            this.stuname = stuname;
        }
    }

    2 创建Dao接口

    @Repository
    public interface  StudentDao extends JpaRepository<Student,Integer> {
    
    }

    3 创建service层接口

    public interface StudentService {
        //查询所有学生信息
        public Iterable<Student> getStudent();
    
        //添加学生信息 
        public Student addStudent(Student student);
    
        //修改学生信息
        public Student updaStudent(Student student);
        
        //删除学生信息
        public  void delStuddent(Integer stuid);
    }

    4 创建service层接口实现类

    @Service("studentService")
    public class StudentServiceImpl implements StudentService{
    
        @Resource
        private StudentDao studentDao;
    
    
        //查询所有学生信息
        @Override
        public Iterable<Student> getStudent() {
            return studentDao.findAll();
        }
        //添加学生信息
        @Override
        public Student addStudent(Student student) {
            return studentDao.save(student);
        }
        //修改学生信息
        @Override
        public Student updaStudent(Student student) {
            return studentDao.save(student);
        }
        //删除学生信息
        @Override
        public void delStuddent(Integer stuid) {
            studentDao.delete(stuid);
        }
    }

    5 编写application.yml文件

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql:///springbootjpa
        username: root
        password: 123
    
      jpa:
        hibernate:
          ddl-auto: update

    6 编写Controller层

    @RestController
    @RequestMapping("/onejpa")
    public class JpaController {
        @Resource
        private StudentService studentService;
    
        //查询数据
        @RequestMapping("/getStudent")
        public Iterable<Student> getStudent(){
            return studentService.getStudent();
        }
        //添加数据
        @RequestMapping("/addStudent")
        public Student addStudent(){
            Student student=new Student();
            student.setStuname("张三");
            return studentService.addStudent(student);
        }
        //修改数据
        @RequestMapping("/updaStudent")
        public Student updaStudent(){
            Student student=new Student();
            student.setStuid(1);
            student.setStuname("李四");
            return studentService.updaStudent(student);
        }
        //删除数据
        @RequestMapping("/delStudent")
        public void delStudent(){
            studentService.delStuddent(1);
        }
    }

    7 启动程序 

    @SpringBootApplication
    public class StartSpringBoot {
        public static void main(String[] args) {
            SpringApplication.run(StartSpringBoot.class,args);
        }
    }
    @Entity
    @Table(name="student")
    public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer stuid;
    private String stuname;

    public Integer getStuid() {
    return stuid;
    }

    public void setStuid(Integer stuid) {
    this.stuid = stuid;
    }

    public String getStuname() {
    return stuname;
    }

    public void setStuname(String stuname) {
    this.stuname = stuname;
    }
    }
  • 相关阅读:
    CSS实例:图片导航块
    导航,头部,CSS基础
    web基础,用html元素制作web页面
    web基础
    timestamp与timedelta,管理信息系统概念与基础
    datetime处理日期和时间
    中文词频统计
    文件方式实现完整的英文词频统计实例
    当你发现任务管理器打不开
    恶搞关机
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029602.html
Copyright © 2020-2023  润新知