当前位置: 首页 > news >正文

SpringData JPA 整合Springboot

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.lryc.cn/news/262144.html

相关文章:

  • 打工人副业变现秘籍,某多/某手变现底层引擎-Stable Diffusion 黑白老照片上色修复
  • 第十三章总结
  • 大模型应用_PrivateGPT
  • [Android] ubuntu虚拟机上搭建 Waydroid 环境
  • LeedCode刷题---滑动窗口问题(二)
  • pycharm依赖管理(不要用pip freeze)
  • [Kafka 常见面试题]如何保证消息的不重复不丢失
  • Java中System.setProperty()用法
  • Eclipse 自动生成注解,如果是IDEA可以参考编译器自带模版进行修改
  • 微信小程序vant安装使用过程中遇到无法构建npm的问题
  • [python]用python获取EXCEL文件内容并保存到DBC
  • Spring Boot 如何配置 log4j2
  • 如何安装docker
  • Linux 之 性能优化
  • 用Go汇编实现一个快速排序算法
  • Spring-整合MyBatis
  • sql宽字节注入
  • 开源 LLM 微调训练指南:如何打造属于自己的 LLM 模型
  • Android hilt使用
  • 2023/12/17 初始化
  • 【算法Hot100系列】三数之和
  • CSS 简介
  • myBatis-plus自动填充插件
  • 746. 使用最小花费爬楼梯 --力扣 --JAVA
  • 使用Verdaccio搭建私有npm仓库
  • 87 GB 模型种子,GPT-4 缩小版,超越ChatGPT3.5,多平台在线体验
  • Golang 数组 移除元素 双指针法 leetcode27 小记
  • c# OpenCV 图像裁剪、调整大小、旋转、透视(三)
  • Kafka相关知识
  • gitlab 通过svn hook 触发