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.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 接口
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{
@Autowired
private CustomerRepository customerRepository;
@Override
public 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 {
@Autowired
private CustomerService customerService;
@RequestMapping("/customer/all")
public Iterable<Customer> getAll(){
Iterable<Customer> iterable=customerService.getAll();
return iterable;
}
}
6.运行
7.可以配置的选项