農農  

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;
}
}
posted on 2019-12-12 15:43  Baekhyunne  阅读(553)  评论(0编辑  收藏  举报