SpringData JPA 整合Springboot

1.导入依赖



    
        springdata
        com.kuang
        1.0-SNAPSHOT
    
    4.0.0

    springboot-jpa

    
        8
        8
        UTF-8

        
        2.2.9.RELEASE
    
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.2.6
        
        
            org.projectlombok
            lombok
        
    

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 {
}

4.service

package com.kuang.springdata.service;

import com.kuang.springdata.pojo.Customer;

public interface CustomerService {
    Iterable 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 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 getAll(){
     Iterable iterable=customerService.getAll();
        return iterable;
    }
}

6.运行

SpringData JPA 整合Springboot_第1张图片7.可以配置的选项

SpringData JPA 整合Springboot_第2张图片

你可能感兴趣的:(hibernate,spring,boot,后端,java)