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案例的更多相关文章

  1. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

  2. 【极简版】SpringBoot+SpringData JPA 管理系统

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...

  3. 带你搭一个SpringBoot+SpringData JPA的环境

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...

  4. 二、springboot使用jpa

    花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...

  5. Springboot+MyBatis+JPA集成

      1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...

  6. 第11章—使用对象关系映射持久化数据—SpringBoot+SpringData+Jpa进行查询修改数据库

    SpringBoot+SpringData+Jpa进行查询修改数据库 JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它又不限于EJB 3.0,你可以在Web应用.甚至桌面应用 ...

  7. 集成Springboot+MyBatis+JPA

    1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...

  8. SpringBoot Data JPA 关联表查询的方法

    SpringBoot Data JPA实现 一对多.多对一关联表查询 开发环境 IDEA 2017.1 Java1.8 SpringBoot 2.0 MySQL 5.X 功能需求 通过关联关系查询商店 ...

  9. 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页

    使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页      JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...

随机推荐

  1. 熟悉javaEE主流框架Spring boot,Spring Cloud,Mybatis,了解Servlet,JDBC

    什么是Tomcat 阿帕奇提供的小型服务器软件,支持servet和jsp规范 lib包:存放jar包 WabApp:发布项目的目录 work:jsp编译生成.class的目录 LOgs:存放日志文件 ...

  2. day17——序列化、os、sys、hashlib、collections

    day17 序列化 json 两组4个方法: 1.dumps(序列化) -- loads(反序列) dumps(list):将对象转换成字符串 loads(str):将字符串转换成对象 list--s ...

  3. C语言的变参列表 va_list

    1. va_list的基本原理和用法 #include<stdio.h> #include<stdarg.h> void func(int i,char *ch,...){ / ...

  4. JSON.stringify()序列化的理解及使用

    该函数的作用是:系列化对象 系列化对象说白了就是把对象的类型转换为字符串类型 语法 JSON.stringify(value[, replacer [, space]]) value 将要序列化成 一 ...

  5. Kafka 消费者

    应用从Kafka中读取数据需要使用KafkaConsumer订阅主题,然后接收这些主题的消息.在我们深入这些API之前,先来看下几个比较重要的概念. Kafka消费者相关的概念 消费者与消费组 假设这 ...

  6. Linux文件比对,批量复制

    --背景 工作中突然有一天文件服务器空间满了,导致文件存不进去,立马换了另外一台服务器作为文件服务器,将服务器挂载上去,原来的服务器修复之后需要重新换回来,但是需要将临时使用的服务器内的文件迁移至原文 ...

  7. C语言--线性表

    #include<stdio.h>#include<stdlib.h>#include<string.h>#define LIST_SIZE 100#define ...

  8. iOS - FlexBox 布局之 YogaKit

    由于刚开始的项目主要用的H5.javaScript技术为主原生开发为辅的手段开发的项目,UI主要是还是H5,如今翻原生.为了方便同时维护两端.才找到这个很不错的库. FlexBox?听起来像是一门H5 ...

  9. Matlab装饰模式

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.根据https://www.runoob.com/design-pattern/decorator ...

  10. csdn 分享私藏的18个黑科技网站,想找什么软件就找什么软件!!!

    https://blog.csdn.net/sinat_33921105/article/details/103307419 1.NO.1–胡萝卜周 http://www.carrotchou.blo ...