中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

如何搭建一個(gè)視頻網(wǎng)站互聯(lián)網(wǎng)營銷專業(yè)

如何搭建一個(gè)視頻網(wǎng)站,互聯(lián)網(wǎng)營銷專業(yè),全國招聘網(wǎng)站排名,wordpress rss解析Spring Boot JpaRepository 示例 Spring Boot建立在 Spring 之上,包含 Spring 的所有功能。由于其快速的生產(chǎn)就緒環(huán)境,使開發(fā)人員能夠直接專注于邏輯,而不必費(fèi)力配置和設(shè)置,因此如今它正成為開發(fā)人員的最愛。Spring Boot 是一個(gè)基…

Spring Boot JpaRepository 示例

Spring Boot建立在 Spring 之上,包含 Spring 的所有功能。由于其快速的生產(chǎn)就緒環(huán)境,使開發(fā)人員能夠直接專注于邏輯,而不必費(fèi)力配置和設(shè)置,因此如今它正成為開發(fā)人員的最愛。Spring Boot 是一個(gè)基于微服務(wù)的框架,在其中創(chuàng)建生產(chǎn)就緒的應(yīng)用程序只需很少的時(shí)間。以下是 Spring Boot 的一些功能:

  • 它可以避免 Spring 中繁重的 XML 配置。
  • 它提供了 REST 端點(diǎn)的輕松維護(hù)和創(chuàng)建。
  • 它包括一個(gè)嵌入式的 Tomcat 服務(wù)器。
  • 部署很簡單,war和jar文件可以輕松地部署在Tomcat服務(wù)器中。

有關(guān)更多信息,請參閱本文:Spring Boot 簡介。在本文中,我們將討論如何使用JpaRepository來管理 Spring Boot 應(yīng)用程序中的數(shù)據(jù)。

Jpa存儲庫

JpaRepository 是 Repository 的一個(gè)JPA(Java 持久性 API)特定擴(kuò)展。它包含CrudRepository 和 PagingAndSortingRepository的完整 API?。因此,它包含用于基本 CRUD 操作的 API 以及用于分頁和排序的 API。

句法:

public interface JpaRepository<T,ID> 
extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>  

這里:

  • T:存儲庫管理的域類型(通常是實(shí)體/模型類名)
  • ID:存儲庫管理的實(shí)體的 id 類型(通常是在實(shí)體/模型類中創(chuàng)建的 @Id 的包裝類)

插圖:

public interface DepartmentRepository extends JpaRepository<Department, Long> {}

方法

JpaRepository 中的一些最重要的方法如下

方法 1:saveAll():

保存所有給定的實(shí)體。

句法:

<S extends T> List<S> saveAll(Iterable<S> entities)

參數(shù):實(shí)體,注意它們不能為空,也不能包含空。

返回類型:已保存的實(shí)體;永遠(yuǎn)不會為空。返回的 Iterable 將與作為參數(shù)傳遞的 Iterable 具有相同的大小。

拋出異常:如果給定的實(shí)體或其某個(gè)實(shí)體為空,則會拋出IllegalArgumentException 。

方法 2:getById():

返回具有給定標(biāo)識符的實(shí)體的引用。根據(jù) JPA 持久性提供程序的實(shí)現(xiàn)方式,這很可能始終返回一個(gè)實(shí)例并在首次訪問時(shí)拋出 EntityNotFoundException。其中一些會立即拒絕無效標(biāo)識符。

句法:

T getById(ID id)

參數(shù):id – 不能為空。

返回類型:對具有給定標(biāo)識符的實(shí)體的引用。

方法 3:flush():

刷新數(shù)據(jù)庫中所有待處理的更改。

句法:

void flush()
方法 4:saveAndFlush():

保存實(shí)體并立即刷新更改。

句法:

<S extends T> S saveAndFlush(S entity)

參數(shù):要保存的實(shí)體。不能為空。

返回類型:已保存的實(shí)體

方法 5:deleteAllInBatch():

批量刪除給定實(shí)體,這意味著它將創(chuàng)建單個(gè)查詢。此類操作會導(dǎo)致 JPA 一級緩存和數(shù)據(jù)庫不同步。在調(diào)用此方法之前,請考慮刷新 EntityManager。

句法:

 void deleteAllInBatch(Iterable<T> entities)

參數(shù)要?jiǎng)h除的實(shí)體,不能為空。

實(shí)現(xiàn):

讓我們考慮一個(gè)使用 JpaRepository 管理 Department 實(shí)體的 Spring Boot 應(yīng)用程序。數(shù)據(jù)保存在 H2 數(shù)據(jù)庫中。我們使用 RESTful 控制器。

步驟1:使用IntelliJ IDEA創(chuàng)建Spring Boot項(xiàng)目,創(chuàng)建一個(gè)Spring Boot項(xiàng)目。

第 2 步:添加以下依賴項(xiàng)

  • Spring Web
  • H2 Database
  • Lombok
  • Spring Data JPA

示例:這是pom.xml文件的完整代碼。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ??
? ? <!-- Model version -->
? ? <modelVersion>4.0.0</modelVersion>
? ??
? ? <!-- Parent configuration for Spring Boot -->
? ? <parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>3.0.0</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ??
? ? <!-- Project metadata -->
? ? <groupId>com.amiya</groupId>
? ? <artifactId>Spring-Boot-Demo-Project</artifactId>
? ? <version>1.0.0-SNAPSHOT</version>
? ? <name>Spring-Boot-Demo-Project</name>
? ? <description>Demo project for Spring Boot</description>
? ??
? ? <!-- Java version property -->
? ? <properties>
? ? ? ? <java.version>17</java.version>
? ? </properties>
? ??
? ? <!-- Dependencies required for the project -->
? ? <dependencies>
? ? ? ? <!-- Spring Boot Starter Web: To build web applications -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? <!-- Spring Boot Starter Data JPA: To work with JPA for data access -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-data-jpa</artifactId>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? <!-- Spring Boot Starter Validation: For Jakarta EE bean validation -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-validation</artifactId>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? <!-- H2 Database: In-memory database for development and testing -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.h2database</groupId>
? ? ? ? ? ? <artifactId>h2</artifactId>
? ? ? ? ? ? <scope>runtime</scope>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? <!-- Lombok: To reduce boilerplate code in Java classes -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? <optional>true</optional>
? ? ? ? </dependency>
? ? </dependencies>
? ??
? ? <!-- Build configuration -->
? ? <build>
? ? ? ? <plugins>
? ? ? ? ? ? <!-- Spring Boot Maven Plugin: To package the application as a jar/war -->
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <excludes>
? ? ? ? ? ? ? ? ? ? ? ? <!-- Exclude Lombok from the final build -->
? ? ? ? ? ? ? ? ? ? ? ? <exclude>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? ? ? ? ? ? ? </exclude>
? ? ? ? ? ? ? ? ? ? </excludes>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
</project>

步驟 3:創(chuàng)建下面列出的 4 個(gè)包,并在這些包中創(chuàng)建一些類和接口,如下圖所示

  • entity
  • repository
  • service
  • controller

注意

  • 綠色圓形圖標(biāo)“I”按鈕是界面
  • 藍(lán)色圓形圖標(biāo)“C”按鈕屬于類

步驟 4:實(shí)體包內(nèi)部

在 Department.java 文件中創(chuàng)建一個(gè)簡單的POJO 類。

package com.amiya.springbootdemoproject.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
?* Represents a Department entity in the application.
?*/
@Entity
@Data // Generates getters, setters, toString, equals, and hashCode methods.
@NoArgsConstructor // Generates a no-args constructor.
@AllArgsConstructor // Generates a constructor with all arguments.
@Builder // Generates a builder pattern for creating instances.
public class Department {
? ? @Id // Specifies the primary key of the entity.
? ? @GeneratedValue(strategy = GenerationType.AUTO) // Auto-generates the primary key value.
? ? private Long departmentId; // Unique identifier for the department.
? ? private String departmentName; // Name of the department.
? ? private String departmentAddress; // Address of the department.
? ? private String departmentCode; // Code representing the department.
}

?

步驟 5:存儲庫包內(nèi)部

創(chuàng)建一個(gè)簡單的接口,并將該接口命名為 DepartmentRepository。正如我們上面討論的那樣,這個(gè)接口將擴(kuò)展JpaRepository 。

例子:

// Java Program to Illustrate DepartmentRepository File

// Importing necessary packages
package com.amiya.springbootdemoproject.repository;

import com.amiya.springbootdemoproject.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
?* Repository interface for Department entity.
?* Provides CRUD operations and custom query methods through JpaRepository.
?*/
@Repository // Indicates that this interface is a Spring Data repository.
public interface DepartmentRepository extends JpaRepository<Department, Long> {}

?

步驟 6:服務(wù)包內(nèi)

在包內(nèi)創(chuàng)建一個(gè)名為DepartmentService 的接口和一個(gè)名為DepartmentServiceImpl 的類。

示例 1-A:

// Java Program to Demonstrate DepartmentService File

// Importing required packages
package com.amiya.springbootdemoproject.service;

import com.amiya.springbootdemoproject.entity.Department;
import java.util.List;

/**
?* Service interface for Department entity.
?* Defines methods for CRUD operations and additional business logic.
?*/
public interface DepartmentService {
? ? /**
? ? ?* Saves a department entity.
? ? ?* @param department the department to save
? ? ?* @return the saved department
? ? ?*/
? ? Department saveDepartment(Department department);

? ? /**
? ? ?* Fetches the list of all department entities.
? ? ?* @return a list of departments
? ? ?*/
? ? List<Department> fetchDepartmentList();

? ? /**
? ? ?* Updates an existing department entity.
? ? ?* @param department the department with updated information
? ? ?* @param departmentId the ID of the department to update
? ? ?* @return the updated department
? ? ?*/
? ? Department updateDepartment(Department department, Long departmentId);

? ? /**
? ? ?* Deletes a department entity by its ID.
? ? ?* @param departmentId the ID of the department to delete
? ? ?*/
? ? void deleteDepartmentById(Long departmentId);
}

?


示例 1-B:

// Java Program to Illustrate DepartmentServiceImpl File

package com.amiya.springbootdemoproject.service;

import com.amiya.springbootdemoproject.entity.Department;
import com.amiya.springbootdemoproject.repository.DepartmentRepository;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
?* Implementation of DepartmentService.
?* Provides business logic for handling department-related operations.
?*/
@Service // Marks this class as a Spring service component.
public class DepartmentServiceImpl implements DepartmentService {

? ? @Autowired
? ? private DepartmentRepository departmentRepository; // Injects the DepartmentRepository dependency.

? ? @Override
? ? public Department saveDepartment(Department department) {
? ? ? ? // Saves and returns the department entity.
? ? ? ? return departmentRepository.save(department);
? ? }

? ? @Override
? ? public List<Department> fetchDepartmentList() {
? ? ? ? // Retrieves and returns a list of all department entities.
? ? ? ? return (List<Department>) departmentRepository.findAll();
? ? }

? ? @Override
? ? public Department updateDepartment(Department department, Long departmentId) {
? ? ? ? // Finds the existing department by ID.
? ? ? ? Department depDB = departmentRepository.findById(departmentId).get();
? ? ? ??
? ? ? ? // Updates fields if they are not null or empty.
? ? ? ? if (Objects.nonNull(department.getDepartmentName()) && !"".equalsIgnoreCase(department.getDepartmentName())) {
? ? ? ? ? ? depDB.setDepartmentName(department.getDepartmentName());
? ? ? ? }
? ? ? ? if (Objects.nonNull(department.getDepartmentAddress()) && !"".equalsIgnoreCase(department.getDepartmentAddress())) {
? ? ? ? ? ? depDB.setDepartmentAddress(department.getDepartmentAddress());
? ? ? ? }
? ? ? ? if (Objects.nonNull(department.getDepartmentCode()) && !"".equalsIgnoreCase(department.getDepartmentCode())) {
? ? ? ? ? ? depDB.setDepartmentCode(department.getDepartmentCode());
? ? ? ? }
? ? ? ??
? ? ? ? // Saves and returns the updated department entity.
? ? ? ? return departmentRepository.save(depDB);
? ? }

? ? @Override
? ? public void deleteDepartmentById(Long departmentId) {
? ? ? ? // Deletes the department entity by its ID.
? ? ? ? departmentRepository.deleteById(departmentId);
? ? }
}

?

步驟 7:控制器包裝內(nèi)部

在包內(nèi)創(chuàng)建一個(gè)名為DepartmentController 的類。

// Java Program to Demonstrate DepartmentController File

package com.amiya.springbootdemoproject.controller;

import com.amiya.springbootdemoproject.entity.Department;
import com.amiya.springbootdemoproject.service.DepartmentService;
import java.util.List;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
?* REST controller for managing Department entities.
?* Handles HTTP requests and routes them to the appropriate service methods.
?*/
@RestController // Marks this class as a RESTful controller.
public class DepartmentController {

? ? @Autowired
? ? private DepartmentService departmentService; // Injects the DepartmentService dependency.

? ? /**
? ? ?* Handles POST requests to save a new department.
? ? ?* @param department the department entity to be saved
? ? ?* @return the saved department entity
? ? ?*/
? ? @PostMapping("/departments")
? ? public Department saveDepartment(@Valid @RequestBody Department department) {
? ? ? ? return departmentService.saveDepartment(department);
? ? }

? ? /**
? ? ?* Handles GET requests to fetch the list of all departments.
? ? ?* @return a list of department entities
? ? ?*/
? ? @GetMapping("/departments")
? ? public List<Department> fetchDepartmentList() {
? ? ? ? return departmentService.fetchDepartmentList();
? ? }

? ? /**
? ? ?* Handles PUT requests to update an existing department.
? ? ?* @param department the department entity with updated information
? ? ?* @param departmentId the ID of the department to be updated
? ? ?* @return the updated department entity
? ? ?*/
? ? @PutMapping("/departments/{id}")
? ? public Department updateDepartment(@RequestBody Department department, @PathVariable("id") Long departmentId) {
? ? ? ? return departmentService.updateDepartment(department, departmentId);
? ? }

? ? /**
? ? ?* Handles DELETE requests to remove a department by ID.
? ? ?* @param departmentId the ID of the department to be deleted
? ? ?* @return a success message
? ? ?*/
? ? @DeleteMapping("/departments/{id}")
? ? public String deleteDepartmentById(@PathVariable("id") Long departmentId) {
? ? ? ? departmentService.deleteDepartmentById(departmentId);
? ? ? ? return "Deleted Successfully";
? ? }
}

?

步驟 8:下面是 application.properties 文件的代碼

server.port=8082# H2 Database
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:dcbapp
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

現(xiàn)在運(yùn)行您的應(yīng)用程序,讓我們在 Postman 中測試端點(diǎn)并參考我們的 H2 數(shù)據(jù)庫。

在 Postman 中測試端點(diǎn)

端點(diǎn) 1:?POST – http://localhost:8082/departments/

端點(diǎn) 2:?GET – http://localhost:8082/departments/

端點(diǎn) 3:?PUT – http://localhost:8082/departments/1

端點(diǎn) 4:刪除 – http://localhost:8082/departments/1

H2數(shù)據(jù)庫如下

http://www.risenshineclean.com/news/35479.html

相關(guān)文章:

  • 如何在公司網(wǎng)站上添加內(nèi)容微信朋友圈產(chǎn)品推廣語
  • 服裝網(wǎng)站建設(shè)與實(shí)現(xiàn)西安seo顧問公司
  • 電商網(wǎng)站開發(fā)平臺實(shí)驗(yàn)河南搜索引擎優(yōu)化
  • 百度優(yōu)化網(wǎng)站建設(shè)網(wǎng)站制作多少錢
  • 村網(wǎng)站建設(shè)計(jì)劃書深圳專業(yè)建站公司
  • 網(wǎng)站開發(fā)需要幾個(gè)人企業(yè)網(wǎng)址怎么注冊
  • 河北建設(shè)廳注冊中心網(wǎng)站長沙做優(yōu)化的公司
  • 做網(wǎng)站順序搜索引擎優(yōu)化排名seo
  • 南京網(wǎng)站的優(yōu)化石景山區(qū)百科seo
  • wordpress七牛加密怎樣優(yōu)化網(wǎng)站排名靠前
  • 電子商務(wù)網(wǎng)站建設(shè)的核心是長沙做網(wǎng)絡(luò)推廣公司的
  • 自建網(wǎng)站如何上傳視頻市場營銷策劃書
  • wordpress php學(xué)習(xí)廣州網(wǎng)站快速排名優(yōu)化
  • 網(wǎng)站建設(shè)策劃書競價(jià)網(wǎng)絡(luò)推廣
  • 網(wǎng)站用途說明全國疫情實(shí)時(shí)動(dòng)態(tài)
  • 汕頭網(wǎng)站建設(shè)系統(tǒng)學(xué)校網(wǎng)站建設(shè)
  • 做房產(chǎn)抵押網(wǎng)站需要什么手續(xù)互動(dòng)營銷經(jīng)典案例
  • 靜態(tài)網(wǎng)站畢業(yè)論文東莞排名優(yōu)化團(tuán)隊(duì)
  • 門戶營銷型網(wǎng)站搭建北京建設(shè)網(wǎng)站公司
  • 安徽品質(zhì)網(wǎng)站建設(shè)創(chuàng)新邀請注冊推廣賺錢的app
  • 哪些建材網(wǎng)站可以做宣傳如何做電商
  • 怎么做網(wǎng)站鏡像小程序商城
  • 怎么查網(wǎng)站是哪家制作公司做的產(chǎn)品推廣方案要包含哪些內(nèi)容
  • 網(wǎng)站平臺建設(shè)公司經(jīng)營范圍網(wǎng)絡(luò)營銷推廣主要做什么?
  • 赤城縣城鄉(xiāng)建設(shè)局網(wǎng)站2023網(wǎng)站分享
  • 陶瓷網(wǎng)站模板下載需要留電話號碼的廣告
  • 地方門戶網(wǎng)站開發(fā)上海搜索優(yōu)化推廣哪家強(qiáng)
  • 成品app直播源碼旅游企業(yè)seo官網(wǎng)分析報(bào)告
  • 淘寶客網(wǎng)站能用淘寶圖標(biāo)做標(biāo)志嗎網(wǎng)站推廣的軟件
  • 建設(shè)公司網(wǎng)站管理制度的意義百度網(wǎng)站推廣教程