zoukankan      html  css  js  c++  java
  • spring--boot数据库增删改查

               spring--boot数据库增删改查

      数据库配置:(必须配置),我写的文件是yml的,和properties是相同的

     1  1 spring:
     2  2   datasource:
     3  3     driver-class-name: com.mysql.jdbc.Driver
     4  4     url: jdbc:mysql://localhost:3306/dbgirl
     5  5     username: root
     6  6     password: root
     7  7   jpa:
     8  8     hibernate:
     9  9       ddl-auto: update
    10 10     show-sql: true

      在建一个Repository.java(继承JpaRepository)

       1 public interface GirlRepository extends JpaRepository<Girl,Integer> { 2 3 4 } 

      再来一个Contorller解决所有增删改查

       1.查询

     1 package com.girl;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.web.bind.annotation.*;
     5 
     6 import java.util.List;
     7 
     8 @RestController
     9 public class GirlController {
    10 
    11     @Autowired
    12     private GirlRepository girlRepository;
    13 
    14     /**
    15      * 获取所有的女生列表
    16      *
    17      * @return
    18      */
    19     @GetMapping(value = "/girls")
    20     public List<Girl> getAllGirl() {
    21         return girlRepository.findAll();
    22     }

      2.修改:

     1  @PutMapping(value = "/girls/{id}")
     2     public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
     3 
     4         Girl girl = new Girl();
     5         girl.setId(id);
     6         girl.setCupSize(cupSize);
     7         girl.setAge(age);
     8 
     9         return girlRepository.save(girl);
    10     }

      3.删除:

    1    @DeleteMapping(value = "/girls/{id}")
    2     public void girlDelete(@PathVariable("id") Integer id) {
    3         girlRepository.deleteById(id);
    4     }

      4.添加

     1   /**
     2      * 添加一个女生
     3      *
     4      * @param cupSize
     5      * @param age
     6      * @return
     7      */
     8     @PostMapping(value = "/girls")
     9     public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
    10         Girl girl = new Girl();
    11         girl.setCupSize(cupSize);
    12         girl.setAge(age);
    13         return girlRepository.save(girl);
    14     }

        完成!!

    2018-04-17

      

  • 相关阅读:
    Cake
    抽屉评论数据库设计
    学习网站
    栈和堆简介
    链表相关操作
    链表操作
    Django form验证二
    django ajax提交form表单数据
    jquery中 after append appendTo 的区别
    Python json.dumps 自定义序列化操作
  • 原文地址:https://www.cnblogs.com/meiLinYa/p/8861388.html
Copyright © 2011-2022 走看看