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;
    }
    }
  • 相关阅读:
    js 生成32位UUID方法
    win10把控制声音改成和win7一样
    jQuery.inArray()方法
    在eclipse中安装activiti插件
    关于NOIP运输计划一题几种思路和若干种做法的研究
    该博客停止更新
    [CTSC2010]产品销售
    roi 学习轨迹
    「PA 2019」Szprotki i szczupaki
    LOJ576签到游戏
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12029602.html
Copyright © 2011-2022 走看看