首页 > 编程语言 > 详细

springboot使用jpa案例

时间:2019-12-12 15:47:00      阅读:108      评论:0      收藏:0      [点我收藏+]

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;
}
}

springboot使用jpa案例

原文:https://www.cnblogs.com/1314Justin/p/12029602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!