spring boot学习第五篇:spring boot与JPA结合

1、准备表,创建表语句如下

CREATE TABLE `girl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cup_Size` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

2、pom.xml文件,内容如下



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hmblogs
    backend
    0.0.1-SNAPSHOT
    backend
    backend
    
        8
        1.2.8
    
    
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

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

        
            mysql
            mysql-connector-java
            8.0.28
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3、GirlController.java控制层代码,如下 

package com.hmblogs.backend.controller;


import com.hmblogs.backend.dao.GirlRep;
import com.hmblogs.backend.entity.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GirlRep girlRep;

    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping(value = "/girls")
    public List getGirlList(){
        return girlRep.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @RequestMapping(value = "/girls",method = RequestMethod.POST)
    public Girl addGirl(@RequestParam("cupSize") String cupSize,
                        @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlRep.save(girl);
    }

}

4、dao层代码如下,

package com.hmblogs.backend.dao;

import com.hmblogs.backend.entity.Girl;
import org.springframework.data.jpa.repository.JpaRepository;

//其中第二个参数为Id的类型
public interface GirlRep extends JpaRepository {
}

5、entity类代码如下

package com.hmblogs.backend.entity;


import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
public class Girl {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String cupSize;
    private Integer age;

}

6、启动类代码如下

package com.hmblogs.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }

}

7、application.yml代码如下

server:
  port: 8080
  servlet.context-path: /backend

spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
      username: ${DB_USER:root}
      password: ${DB_PWD:123456}

9、启动成功,并且贴出代码结构

spring boot学习第五篇:spring boot与JPA结合_第1张图片

10、调试接口

10.1调试get接口获取所有girl

http://localhost:8080/backend/girls

spring boot学习第五篇:spring boot与JPA结合_第2张图片

10.2调试post添加girl接口

http://localhost:8080/backend/girls?cupSize=A22&age=18

spring boot学习第五篇:spring boot与JPA结合_第3张图片 添加成功。

你可能感兴趣的:(Spring,Boot,spring,boot,java)