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.可以配置的选项

相关推荐
克鲁德战士7 分钟前
【Java并发编程的艺术3】Java内存模型(下)
java·开发语言·redis
爱上语文9 分钟前
Maven快速入门及其POM
java·maven
ZmyCoder18 分钟前
SpringBoot中忽略实体类中的某个属性不返回给前端的方法
java
哎呦没19 分钟前
英语知识网站开发:Spring Boot框架技巧
spring boot·后端·性能优化
是小博同学鸭19 分钟前
【面向对象的程序设计——集合框架】主要接口
java·开发语言
ZmyCoder19 分钟前
springboot项目使用maven打包,第三方jar问题
spring boot·maven
世间万物皆对象23 分钟前
Spring Boot Web应用开发:数据访问
java·数据库·spring boot
顽疲42 分钟前
springboot vue 开源 会员收银系统 (9) 库存管理 结算时扣库存
vue.js·spring boot·后端
2401_857600951 小时前
英语知识在线教学:Spring Boot网站构建
spring boot·后端·mfc
A Runner for leave1 小时前
105.找到冠军
java·数据结构·python·算法·leetcode