zoukankan      html  css  js  c++  java
  • Spring boot6之整合Spring Data JPA

    Spring boot整合Spring Data JPA

    JPA Hibernate框架就是JPA的实现

    Spring Data JPA不是对JPA规范的具体实现,本身是一个抽象层

    • 配置pom.xml

        1 <?xml version="1.0" encoding="UTF-8"?>

        2 <project xmlns="http://maven.apache.org/POM/4.0.0"

        3                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        4                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        5         <modelVersion>4.0.0</modelVersion>

        6 

        7         <groupId>com.wiggin</groupId>

        8         <artifactId>springbootdatajap</artifactId>

        9         <version>1.0-SNAPSHOT</version>

       10         <parent>

       11                 <groupId>org.springframework.boot</groupId>

       12                 <artifactId>spring-boot-parent</artifactId>

       13                 <version>2.1.5.RELEASE</version>

       14         </parent>

       15 

       16         <dependencies>

       17                 <dependency>

       18                         <groupId>org.springframework.boot</groupId>

       19                         <artifactId>spring-boot-starter-data-jpa</artifactId>

       20                 </dependency>

       21                 <dependency>

       22                         <groupId>mysql</groupId>

       23                         <artifactId>mysql-connector-java</artifactId>

       24                 </dependency>

       25                 <dependency>

       26                         <groupId>org.projectlombok</groupId>

       27                         <artifactId>lombok</artifactId>

       28                 </dependency>

       29         </dependencies>

       30 

       31 </project>

    • 创建实体类Student

        1 package com.wiggin.entity;

        2 

        3 import lombok.Data;

        4 

        5 import javax.persistence.*;

        6 import java.util.Date;

        7 

        8 // 创建表不用关联数据库

        9 @Data

       10 @Entity

       11 public class Student {

       12         // 主键

       13         @Id

       14         // 自增

       15         @GeneratedValue(strategy = GenerationType.IDENTITY)

       16         private Long id;

       17         @Column

       18         private String name;

       19         @Column

       20         private int score;

       21         @Column

       22         private Date birthday;

       23 }

    • 创建接口StudentRepository

        1 package com.wiggin.repository;

        2 

        3                 import com.wiggin.entity.Student;

        4                 import org.springframework.data.jpa.repository.JpaRepository;

        5 

        6 // 直接继承JpaRepository<Student,Long> ,传入实体类和主键类型,不需要要写功能代码

        7 public interface StudentRepository extends JpaRepository<Student,Long> {

        8         // 自定义方法要符合方法命名规定,同样也不需要实体化

        9         public Student getById(Long id);

       10

    • 创建StudentHandler注入StudentRepository

        1 package com.wiggin.repository;

        2 

        3 import com.wiggin.entity.Student;

        4 import org.springframework.data.jpa.repository.JpaRepository;

        5 

        6 // 直接继承JpaRepository<St

        1 package com.wiggin.controller;

        2 

        3 

        4 import com.wiggin.entity.Student;

        5 import com.wiggin.repository.StudentRepository;

        6 import org.springframework.beans.factory.annotation.Autowired;

        7 import org.springframework.stereotype.Controller;

        8 import org.springframework.web.bind.annotation.*;

        9 

       10 import java.util.List;

       11 

       12 

       13 @RestController

       14 public class StudentHandler {

       15         @Autowired

       16         private StudentRepository studentRepository;

       17 

       18         @GetMapping("/findAll")

       19         public List<Student> findAll(){

       20                 return studentRepository.findAll();

       21         }

       22         @GetMapping("/findById/{id}")

       23         public Student findById(@PathVariable("id") Long id){

       24                 return studentRepository.getById(id);

       25         }

       26         @PostMapping("/save")

       27         public void save(@RequestBody Student student){

       28                 studentRepository.save(student);

       29         }

       30         @PutMapping("/update")

       31         public void update(@RequestBody Student student){

       32                 studentRepository.save(student);

       33         }

       34         @DeleteMapping("/deleteById/{id}")

       35         public void deleteById(@PathVariable("id") Long id){

       36                 studentRepository.deleteById(id);

       37         }

       38 

       39 }

      

    • 配置application.yml

        1 spring:

        2     datasource:

        3         url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

        4         username: root

        5         password: 123456

        6         driver-class-name: com.mysql.cj.jdbc.Driver

        7     jpa:

        8         show-sql: true # 输出SQL

        9         properties:

       10             hibernate:

       11                 format_sql: true # 格式化输出SQL

    • 创建启动类Application

        1 package com.wiggin;

        2 

        3 import org.springframework.boot.SpringApplication;

        4 import org.springframework.boot.autoconfigure.SpringBootApplication;

        5 

        6 @SpringBootApplication

        7 public class Application {

        8         public static void main(String[] args) {

        9                 SpringApplication.run(Application.class,args);

       10         }

       11 }

     

  • 相关阅读:
    如何将cordova导入Android studio,只需两步即可
    Cordova 教程 学习步骤-从零基础开始
    特效插件
    jq 命名空间
    input聚焦后光标移动至末尾
    时间常用api
    jq 便捷api jq 常用 api jq 快捷 api
    键盘事件
    创建爬网规则
    SharePoint 2013 本地开发解决方案以及程调试(真的可以)
  • 原文地址:https://www.cnblogs.com/wigginess/p/13499408.html
Copyright © 2011-2022 走看看