成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringBoot 實戰(zhàn) (八) | 使用 Spring Data JPA 訪問 Mysql 數(shù)據(jù)

hedzr / 3369人閱讀

摘要:是一個基于映射的標準協(xié)議目前最新版本是。的主要實現(xiàn)由和等完成,我們只要使用來開發(fā),無論是哪一個開發(fā)方式都是一樣的。是的一個子項目,它通過基于的極大地減少了作為數(shù)據(jù)訪問方案的代碼量。源碼下載后語以上為使用訪問數(shù)據(jù)庫的教程。

微信公眾號:一個優(yōu)秀的廢人
如有問題或建議,請后臺留言,我會盡力解決你的問題。
前言

如題,今天介紹 Spring Data JPA 的使用。

什么是 Spring Data JPA

在介紹 Spring Data JPA 之前,首先介紹 Hibernate 。 Hibernate 使用 O/R 映射 (Object-Relation Mapping) 技術(shù)實現(xiàn)數(shù)據(jù)訪問, O/R 映射即將領(lǐng)域模型類與數(shù)據(jù)庫的表進行映射,通過程序操作對象而實現(xiàn)表數(shù)據(jù)操作的能力,讓數(shù)據(jù)訪問操作無需關(guān)注數(shù)據(jù)庫相關(guān)技術(shù)。

Hibernate 主導了 EJB 3.0 的 JPA 規(guī)范, JPA 即 Java Persistence API。JPA 是一個基于 O/R 映射的標準協(xié)議(目前最新版本是 JPA 2.1)。所謂規(guī)范即只定義標準規(guī)制(如注解、接口),不提供實現(xiàn),軟件提供商可以按照標準規(guī)范來實現(xiàn),而使用者只需按照規(guī)范中定義的方式來使用,而不用和軟件提供商的實現(xiàn)打交道。JPA 的主要實現(xiàn)由 Hibernate 、 EclipseLink 和 OpenJPA 等完成,我們只要使用 JPA 來開發(fā),無論是哪一個開發(fā)方式都是一樣的。

Spring Data JPA 是 Spring Data 的一個子項目,它通過基于 JPA 的 Repository 極大地減少了 JPA 作為數(shù)據(jù)訪問方案的代碼量。

簡而言之,JPA 是一種 ORM 規(guī)范,但并未提供 ORM 實現(xiàn),而 Hibernate 是一個 ORM 框架,它提供了 ORM 實現(xiàn)。

準備工作

IDEA

JDK1.8

SpringBoot 2.1.3

pom.xml 文件引入的依賴如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.nasus
    jpa
    0.0.1-SNAPSHOT
    jpa
    jpa Demo project for Spring Boot

    
        1.8
    

    

        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

簡單說下,加入 JPA 依賴;mysql 連接類用于連接數(shù)據(jù);web 啟動類,但凡是 web 應用都需要依賴它;lombok 用于簡化實體類。不會的看這篇舊文介紹:SpringBoot 實戰(zhàn) (三) | 使用 LomBok

application.yaml 配置文件
spring:
# 數(shù)據(jù)庫相關(guān)
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
    username: root
    password: 123456
# JPA 相關(guān)
  jpa:
    hibernate:
      ddl-auto: update   #ddl-auto:設(shè)為 create 表示每次都重新建表
    show-sql: true
repository (dao) 層
package com.nasus.jpa.repository;

import com.nasus.jpa.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

/**
 * Project Name:springboot_jpa_demo 
* Package Name:com.nasus.jpa.repository
* Date:2019/2/19 21:37
* Description: TODO: 描述該類的作用
* @author nasus
*/ @Repository public interface StudentRepository extends JpaRepository, CrudRepository { }

從上圖,可以看出 JpaRepository 繼承于 PangingAndSortingRepository 繼承于 CrudRepository 。

CrudRepository 提供基本的增刪改查PagingAndSortingRepository 提供分頁和排序方法;JpaRepository 提供 JPA 需要的方法。在使用的時候,可以根據(jù)具體需要選中繼承哪個接口。

使用這些接口的好處有:

繼承這些接口,可以使Spring找到自定義的數(shù)據(jù)庫操作接口,并生成代理類,后續(xù)可以注入到Spring容器中;

可以不寫相關(guān)的sql操作,由代理類生成

service 層
package com.nasus.jpa.service;

import com.nasus.jpa.entity.Student;
import java.util.List;

/**
 * Project Name:springboot_jpa_demo 
* Package Name:com.nasus.jpa.service
* Date:2019/2/19 21:41
* Description: TODO: 描述該類的作用
* @author nasus
*/ public interface StudentService { Student save(Student student); Student findStudentById(Integer id); void delete(Integer id); void updateStudent(Student student); List findStudentList(); }

實現(xiàn)類:

package com.nasus.jpa.service.impl;

import com.nasus.jpa.entity.Student;
import com.nasus.jpa.repository.StudentRepository;
import com.nasus.jpa.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name:springboot_jpa_demo 
* Package Name:com.nasus.jpa.service.impl
* Date:2019/2/19 21:43
* Description: TODO: 描述該類的作用
* @author nasus
*/ @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; /** * 保存學生信息 * @param student * @return */ @Override public Student save(Student student) { return studentRepository.save(student); } /** * 根據(jù) Id 查詢學生信息 * @param id * @return */ @Override public Student findStudentById(Integer id) { return studentRepository.findById(id).get(); } /** * 刪除學生信息 * @param id */ @Override public void delete(Integer id) { Student student = this.findStudentById(id); studentRepository.delete(student); } /** * 更新學生信息 * @param student */ @Override public void updateStudent(Student student) { studentRepository.save(student); } /** * 查詢學生信息列表 * @return */ @Override public List findStudentList() { return studentRepository.findAll(); } }
controller 層構(gòu)建 restful API
package com.nasus.jpa.controller;

import com.nasus.jpa.entity.Student;
import com.nasus.jpa.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Project Name:springboot_jpa_demo 
* Package Name:com.nasus.jpa.controller
* Date:2019/2/19 21:55
* Description: TODO: 描述該類的作用
* @author nasus
*/ @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @PostMapping("/save") public Student saveStudent(@RequestBody Student student){ return studentService.save(student); } @GetMapping("/{id}") public Student findStudentById(@PathVariable("id") Integer id){ return studentService.findStudentById(id); } @GetMapping("/list") public List findStudentList(){ return studentService.findStudentList(); } @DeleteMapping("/{id}") public void deleteStudentById(@PathVariable("id") Integer id){ studentService.delete(id); } @PutMapping("/update") public void updateStudent(@RequestBody Student student){ studentService.updateStudent(student); } }
測試結(jié)果

其他接口已通過 postman 測試,無問題。

源碼下載:https://github.com/turoDog/De...

后語

以上為 SpringBoot 使用 Spring Data JPA 訪問 Mysql 數(shù)據(jù)庫的教程。最后,對 Python 、Java 感興趣請長按二維碼關(guān)注一波,我會努力帶給你們價值,如果覺得本文對你哪怕有一丁點幫助,請幫忙點好看,讓更多人知道。

另外,關(guān)注之后在發(fā)送 1024 可領(lǐng)取免費學習資料。資料內(nèi)容詳情請看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73329.html

相關(guān)文章

  • spring boot - 收藏集 - 掘金

    摘要:引入了新的環(huán)境和概要信息,是一種更揭秘與實戰(zhàn)六消息隊列篇掘金本文,講解如何集成,實現(xiàn)消息隊列。博客地址揭秘與實戰(zhàn)二數(shù)據(jù)緩存篇掘金本文,講解如何集成,實現(xiàn)緩存。 Spring Boot 揭秘與實戰(zhàn)(九) 應用監(jiān)控篇 - HTTP 健康監(jiān)控 - 掘金Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...

    rollback 評論0 收藏0
  • SpringBoot 實戰(zhàn) (十) | 聲明式事務

    摘要:前言如題,今天介紹的聲明式事務。提供一個注解在配置類上來開啟聲明式事務的支持。而在配置里還開啟了對聲明式事務的支持,代碼如下所以在中,無須顯式開啟使用注解。源碼下載后語以上為聲明式事務的教程。 微信公眾號:一個優(yōu)秀的廢人如有問題或建議,請后臺留言,我會盡力解決你的問題。 前言 如題,今天介紹 SpringBoot 的 聲明式事務。 Spring 的事務機制 所有的數(shù)據(jù)訪問技術(shù)都有事務處...

    ygyooo 評論0 收藏0

發(fā)表評論

0條評論

hedzr

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<