zoukankan      html  css  js  c++  java
  • 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;
    }
    }
  • 相关阅读:
    Python标准库之csv(1)
    python3:csv的读写
    Python os模块方法
    Python闭包
    Python修饰器
    Python生成器
    Python迭代器
    Python文件对象方法
    Justep X5 Studio,业界公认第一的快速开发平台
    马云:早九晚五的工作方式在2013-2015年就是慢性自杀
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029602.html
Copyright © 2011-2022 走看看