SpringData JPA 整合Springboot

1.导入依赖

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">
    <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

XML 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 2001
    url: jdbc:mysql://localhost:3306/springdata_jpa?useSSL=true&useUnicode=true&characterEncoding=utf8
    type: com.alibaba.druid.pool.DruidDataSource
  jpa:
    hibernate:
      ddl-auto: update #数据库表的生成策略
    show-sql: true  #是否显示SQL
    generate-ddl: true #是否生成建表语句打印在控制台上

3.CustomerRepository 接口

java 复制代码
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

java 复制代码
package com.kuang.springdata.service;

import com.kuang.springdata.pojo.Customer;

public interface CustomerService {
    Iterable<Customer> getAll();
}
java 复制代码
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{

    @Autowired
    private CustomerRepository customerRepository;
    @Override
    public Iterable<Customer> getAll() {
        return customerRepository.findAll();
    }
}

5.Controller

java 复制代码
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 {

    @Autowired
    private CustomerService customerService;
    @RequestMapping("/customer/all")
    public Iterable<Customer> getAll(){
     Iterable<Customer> iterable=customerService.getAll();
        return iterable;
    }
}

6.运行

7.可以配置的选项

相关推荐
岳轩子5 分钟前
linux安装jdk
java·linux·运维
解孔明6 分钟前
Java如何扫描指定包下所有类?
java·开发语言
睆小白18 分钟前
【springboot】实现文件上传和下载
java·spring boot·后端
ChinaRainbowSea24 分钟前
十六,Spring Boot 整合 Druid 以及使用 Druid 监控功能
java·spring boot·后端·spring·web
吴半杯35 分钟前
Mysql调优之性能监控(一)
java·linux·mysql
.生产的驴1 小时前
Docker 消息队列RabbitMQ 安装延迟消息插件
运维·spring boot·后端·docker·容器·rabbitmq·java-rabbitmq
慢慢雨夜1 小时前
uniapp 苹果安全域适配
java·前端·uni-app
mit6.8241 小时前
[Linux#49][UDP] 2w字详解 | socketaddr | 常用API | 实操:实现简易Udp传输
linux·网络·c++·笔记·后端
敲代码不忘补水1 小时前
二十种编程语言庆祝中秋节
java·javascript·python·golang·html
码农研究僧1 小时前
Java或者前端 实现中文排序(调API的Demo)
java·前端·localecompare·中文排序·collator