zoukankan      html  css  js  c++  java
  • Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包。

    1 <dependency>
    2   <groupId>org.springframework.boot</groupId>
    3   <artifactId>spring-boot-starter-data-jpa</artifactId>
    4 </dependency>
    5 
    6 <dependency>
    7   <groupId>mysql</groupId>
    8   <artifactId>mysql-connector-java</artifactId>
    9 </dependency>

    新建dbpeople的一个数据库,用户名root,密码123456

    编写生成配置文件application-prod.yml

    1 server:
    2   port: 8082
    3   context-path: /

    修改application.yml文件

     1 spring:
     2   profiles:
     3     active: prod
     4   datasource:
     5     driver-class-name: com.mysql.jdbc.Driver
     6     url: jdbc:mysql://localhost:3306/dbpeople?useSSL=true
     7  username: root  8  password: 123456  9  jpa: 10  hibernate: 11  ddl-auto: create #update 12 show-sql: true

    添加一个people类People.java

     1 package com.example.demo;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.GeneratedValue;
     5 import javax.persistence.Id;
     6 
     7 //@Entity对应数据库中的一个表
     8 @Entity
     9 public class People {
    10 
    11     //@Id配合@GeneratedValue,表示id自增长
    12     @Id
    13     @GeneratedValue
    14     private Integer id;
    15     private String name;
    16     private Integer age;
    17 
    18     //必须要有一个无参的构造函数,不然数据库会报错
    19     public People() {
    20     }
    21 
    22     public Integer getId() {
    23         return id;
    24     }
    25 
    26     public void setId(Integer id) {
    27         this.id = id;
    28     }
    29 
    30     public String getName() {
    31         return name;
    32     }
    33 
    34     public void setName(String name) {
    35         this.name = name;
    36     }
    37 
    38     public Integer getAge() {
    39         return age;
    40     }
    41 
    42     public void setAge(Integer age) {
    43         this.age = age;
    44     }
    45 }

    添加一个注入类PeopleProperties.java

     1 package com.example.demo;
     2 
     3 import org.springframework.boot.context.properties.ConfigurationProperties;
     4 import org.springframework.stereotype.Component;
     5 
     6 //获取注入还需要加一个@Component注解,@ConfigurationProperties(prefix = "people")获取前缀是people的配置
     7 @Component
     8 @ConfigurationProperties(prefix = "people")
     9 public class PeopleProperties {
    10 
    11     private String name;
    12     private Integer age;
    13 
    14 
    15     public void setName(String name) {
    16         this.name = name;
    17     }
    18     public void setAge(Integer age) {
    19         this.age = age;
    20     }
    21 
    22     public String getName() { return name; }
    23     public Integer getAge() { return age; }
    24 }

    编写一个数据库操作自定义接口,定制操作接口,这里只演示定制查询接口

    PeopleRepository.java

     1 package com.example.demo;
     2 
     3 import org.springframework.data.jpa.repository.JpaRepository;
     4 
     5 import java.util.List;
     6 
     7 public interface PeopleRepository extends JpaRepository<People,Integer> {
     8     //通过年龄来查询
     9     public List<People> findByAge(Integer age);
    10 }

    编写数据库操作类,可以进行增、删、改、查等操作。

     1 package com.example.demo;
     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 PeopleController {
    10     @Autowired
    11     private PeopleRepository peopleRepository;
    12 
    13     /**
    14      * 查询数据库中所有列表
    15      * @return
    16      */
    17     @GetMapping(value = "/people")
    18     public List<People> girlList(){
    19         return peopleRepository.findAll();
    20     }
    21 
    22     /**
    23      * 增加一个人的数据
    24      * @param name
    25      * @param age
    26      * @return
    27      */
    28     @PostMapping(value = "/people")
    29     public People peopleAdd(@RequestParam("name") String name,
    30                             @RequestParam("age") Integer age){
    31         People people = new People();
    32         people.setName(name);
    33         people.setAge(age);
    34 
    35         return peopleRepository.save(people);
    36     }
    37 
    38     /**
    39      * 查询一个人
    40      * @param id
    41      * @return
    42      */
    43     @GetMapping(value = "/people/{id}")
    44     public People peopleFindOne(@PathVariable("id") Integer id){
    45         return peopleRepository.findOne(id);
    46     }
    47 
    48     /**
    49      * 修改一个人的数据
    50      * @param id
    51      * @param name
    52      * @param age
    53      */
    54     @PutMapping(value = "/people/{id}")
    55     public People peopleUpdate(@PathVariable("id") Integer id,
    56                              @RequestParam("name") String name,
    57                              @RequestParam("age") Integer age){
    58         People people = new People();
    59         people.setId(id);
    60         people.setName(name);
    61         people.setAge(age);
    62 
    63         return peopleRepository.save(people);
    64     }
    65 
    66     /**
    67      * 删除一个人的数据
    68      * @param id
    69      */
    70     @DeleteMapping(value = "/people/{id}")
    71     public void peopleDelete(@PathVariable("id") Integer id){
    72         peopleRepository.delete(id);
    73     }
    74 
    75     /**
    76      * 通过年龄查询列表
    77      * @param age
    78      * @return
    79      */
    80     @GetMapping(value = "/people/age/{age}")
    81     public List<People> peopleListByAge(@PathVariable("age") Integer age){
    82         return peopleRepository.findByAge(age);
    83     }
    84 }

    运行,使用postman工具发包给运行起来的服务器

    增加三条数据

    打开数据库可以看到增加了三条数据

    通过id查询数据,看到返回id=2的数据

    查询年龄age为22的数据列表

    可以看到返回两条age=22的数据

    修改数据

    可以看到id=2的数据修改成了

     

    删除id=2的数据

    可以看到id=2的数据已经被删掉了。

  • 相关阅读:
    python计算文件MD5
    #每日一练 使用迭代器函数和初始种子值构建列表。
    python接收从CMD传过来的值
    ping IP,通与不通执行不同操作
    bat按回车执行命令
    adb执行简单命令,用于刷页面访问次数
    #每日一练 合并字典,并为每个字创建一个列表
    #每日一练 按提供函数,返回差异化
    #每日一练 根据过滤内容,把值分成两组
    #每日一练 根据索引对列表进行排序
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7844314.html
Copyright © 2011-2022 走看看