SpringData JPA 整合Springboot

news/2024/10/22 15:32:08/

1.导入依赖

<?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"><parent><artifactId>springdata</artifactId><groupId>com.kuang</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springboot-jpa</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--    声明springboot的版本号 --><spring-boot.version>2.2.9.RELEASE</spring-boot.version></properties><!--  引入springboot官方提供的所有依赖的版本号定义,如果项目中使用相关依赖,可以不必写版本号了--><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--    引入springboot的web项目的依赖        --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
<!--     data - jpa 启动器  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--        druid连接--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

2.yml

spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 2001url: jdbc:mysql://localhost:3306/springdata_jpa?useSSL=true&useUnicode=true&characterEncoding=utf8type: com.alibaba.druid.pool.DruidDataSourcejpa:hibernate:ddl-auto: update #数据库表的生成策略show-sql: true  #是否显示SQLgenerate-ddl: true #是否生成建表语句打印在控制台上

3.CustomerRepository 接口

package com.kuang.springdata.repositories;import com.kuang.springdata.pojo.Customer;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;public interface CustomerRepository extends PagingAndSortingRepository<Customer,Long> {
}

4.service

package com.kuang.springdata.service;import com.kuang.springdata.pojo.Customer;public interface CustomerService {Iterable<Customer> getAll();
}
package com.kuang.springdata.service;import com.kuang.springdata.pojo.Customer;
import com.kuang.springdata.repositories.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class CustomerServiceImpl implements CustomerService{@Autowiredprivate CustomerRepository customerRepository;@Overridepublic Iterable<Customer> getAll() {return customerRepository.findAll();}
}

5.Controller

package com.kuang.springdata.controller;import com.kuang.springdata.pojo.Customer;
import com.kuang.springdata.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CustomerController {@Autowiredprivate CustomerService customerService;@RequestMapping("/customer/all")public Iterable<Customer> getAll(){Iterable<Customer> iterable=customerService.getAll();return iterable;}
}

6.运行

7.可以配置的选项


http://www.ppmy.cn/news/1275820.html

相关文章

配置paddleocr及paddlepaddle解决报错 GLIBCXX_3.4.30 FreeTypeFont

配置 https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/StyleText/README_ch.md#style-text 环境配置 https://www.paddlepaddle.org.cn/ 根据自己的cuda版本选择paddlepaddle-gpu # 新建conda环境 # python version conda create -n paddle python3.8 # 安装p…

【机器学习】043_准确率、精确率、召回率

一、定义 在处理偏斜数据集时&#xff0c;通常使用不同的误差度量&#xff0c;而不仅仅是使用分类误差来衡量算法性能。 1. 混淆矩阵的概念 二分类问题的混淆矩阵为2X2矩阵&#xff0c;由四部分组成&#xff1a; 假阴性&#xff08;FN&#xff09;&#xff1a;模型预测为负…

传统小微企业用 CRM 系统能提高多少业绩?(CRM 系统的功能和效果评估)

CRM管理系统迭代至今&#xff0c;其功能已经非常全面。对于小微企业来说&#xff0c;他们需要的往往还是那些核心的CRM功能。这也表明管理潜在客户和联系人以及自动化销售流程仍然是CRM的主要功能之一。鉴于当今有许多渠道可以接触到客户&#xff0c;营销的作用越来越突出。同样…

13 v-show指令

概述 v-show用于实现组件的显示和隐藏&#xff0c;和v-if单独使用的时候有点类似。不同的是&#xff0c;v-if会直接移除dom元素&#xff0c;而v-show只是让dom元素隐藏&#xff0c;而不会移除。 在实际开发中&#xff0c;v-show也经常被用到&#xff0c;需要重点掌握。 基本…

15 使用v-model绑定单选框

概述 使用v-model绑定单选框也比较常见&#xff0c;比如性别&#xff0c;要么是男&#xff0c;要么是女。比如单选题&#xff0c;给出多个选择&#xff0c;但是只能选择其中的一个。 在本节课中&#xff0c;我们演示一下这两种常见的用法。 基本用法 我们创建src/component…

动态规划 - 509.斐波那契数(C#和C实现)

动态规划 - 509.斐波那契数(C#和C实现) 题目描述 斐波那契数&#xff0c;通常用 F(n) 表示&#xff0c;形成的序列称为斐波那契数列。该数列由 0 和 1 开始&#xff0c;后面的每一项数字都是前面两项数字的和。也就是&#xff1a; F(0) 0&#xff0c;F(1) 1 F(n) F(n - 1…

Django(一)

1.web框架底层 1.1 网络通信 注意&#xff1a;局域网 个人一般写程序&#xff0c;想要让别人访问&#xff1a;阿里云、腾讯云。 去云平台租服务器&#xff08;含公网IP&#xff09;程序放在云服务器 先以局域网为例 我的电脑【服务端】 import socket# 1.监听本机的IP和…

webGL开发智慧城市流程

开发智慧城市的WebGL应用程序涉及多个方面&#xff0c;包括城市模型、实时数据集成、用户界面设计等。以下是一个一般性的流程&#xff0c;您可以根据项目的具体需求进行调整&#xff0c;希望对大家有所帮助。 1.需求分析&#xff1a; 确定智慧城市应用程序的具体需求和功能。考…