Hướng dẫn how do i access mysql database with spring data rest application? - làm cách nào để tôi truy cập cơ sở dữ liệu mysql bằng ứng dụng nghỉ dữ liệu mùa xuân?

Hướng dẫn này hướng dẫn bạn qua quá trình tạo một ứng dụng lò xo được kết nối với cơ sở dữ liệu MySQL [trái ngược với cơ sở dữ liệu nhúng trong bộ nhớ, mà hầu hết các hướng dẫn khác và nhiều ứng dụng mẫu sử dụng]. Nó sử dụng JPA dữ liệu Spring để truy cập cơ sở dữ liệu, nhưng đây chỉ là một trong nhiều lựa chọn có thể [ví dụ: bạn có thể sử dụng JDBC Spring Spring].

Những gì bạn sẽ xây dựng

Bạn sẽ tạo cơ sở dữ liệu MySQL, xây dựng một ứng dụng lò xo và kết nối nó với cơ sở dữ liệu mới được tạo.

MySQL được cấp phép với GPL, vì vậy bất kỳ nhị phân chương trình nào mà bạn phân phối cũng phải sử dụng GPL. Xem Giấy phép Công cộng GNU.

Những gì bạn cần

  • MySQL phiên bản 5.6 hoặc tốt hơn. Nếu bạn đã cài đặt Docker, có thể hữu ích khi chạy cơ sở dữ liệu dưới dạng container.

  • Khoảng 15 phút

  • Một trình soạn thảo văn bản yêu thích hoặc IDE

  • JDK 1.8 trở lên

  • Gradle 4+ hoặc Maven 3.2+

  • Bạn cũng có thể nhập mã thẳng vào IDE của mình:

    • Bộ công cụ mùa xuân [STS]

    • Ý tưởng intellij

    • VSCODE

Cách hoàn thành hướng dẫn này

Giống như hầu hết các hướng dẫn bắt đầu mùa xuân, bạn có thể bắt đầu từ đầu và hoàn thành từng bước hoặc bạn có thể bỏ qua các bước thiết lập cơ bản đã quen thuộc với bạn. Dù bằng cách nào, bạn kết thúc với mã làm việc.

Để bỏ qua những điều cơ bản, hãy làm như sau:skip the basics, do the following:

  • Tải xuống và giải nén kho lưu trữ nguồn cho hướng dẫn này hoặc nhân bản nó bằng Git:

    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=ThePassword
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.jpa.show-sql: true
    1

  • CD vào

    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=ThePassword
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.jpa.show-sql: true
    2

  • Nhảy về phía trước để tạo cơ sở dữ liệu.

Khi bạn hoàn thành, bạn có thể kiểm tra kết quả của mình so với mã trong

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
3., you can check your results against the code in
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
3.

Bắt đầu với Spring Inititalizr

Bạn có thể sử dụng dự án được khởi tạo trước này và nhấp vào Tạo để tải xuống tệp ZIP. Dự án này được cấu hình để phù hợp với các ví dụ trong hướng dẫn này.

Để khởi tạo thủ công dự án:

  1. Điều hướng đến //start.spring.io. Dịch vụ này kéo theo tất cả các phụ thuộc bạn cần cho một ứng dụng và thực hiện hầu hết các thiết lập cho bạn.

  2. Chọn Gradle hoặc Maven và ngôn ngữ bạn muốn sử dụng. Hướng dẫn này giả định rằng bạn đã chọn Java.

  3. Nhấp vào phụ thuộc và chọn Spring Web, Spring Data JPA và MySQL Driver.Dependencies and select Spring Web, Spring Data JPA, and MySQL Driver.

  4. Nhấp vào Tạo.Generate.

  5. Tải xuống tệp zip kết quả, đây là kho lưu trữ của ứng dụng web được cấu hình với các lựa chọn của bạn.

Nếu IDE của bạn có tích hợp khởi tạo lò xo, bạn có thể hoàn thành quy trình này từ IDE của mình.

Bạn cũng có thể chia rẽ dự án từ GitHub và mở nó trong IDE hoặc trình soạn thảo khác của bạn.

Tạo cơ sở dữ liệu

Mở một thiết bị đầu cuối [dấu nhắc lệnh trong Microsoft Windows] và mở máy khách MySQL với tư cách là người dùng có thể tạo người dùng mới.

Ví dụ, trên một hệ thống Linux, sử dụng lệnh sau;

Điều này kết nối với MySQL là
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
4 và cho phép truy cập vào người dùng từ tất cả các máy chủ. Đây không phải là cách được đề xuất cho một máy chủ sản xuất.not the recommended way for a production server.

Để tạo cơ sở dữ liệu mới, hãy chạy các lệnh sau tại dấu nhắc

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
5:

mysql> create database db_example; -- Creates the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database

Tạo tệp
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
6

Spring Boot cung cấp cho bạn mặc định trên tất cả mọi thứ. Ví dụ: cơ sở dữ liệu mặc định là

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
7. Do đó, khi bạn muốn sử dụng bất kỳ cơ sở dữ liệu nào khác, bạn phải xác định các thuộc tính kết nối trong tệp
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
6.

Tạo một tệp tài nguyên có tên

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
9, như danh sách sau đây cho thấy:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

Ở đây,

package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
0 có thể là
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
1,
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
2,
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
3 hoặc
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
4. Xem tài liệu Hibernate để biết chi tiết.

  • package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    1: Mặc định cho
    package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    6. Không có thay đổi được thực hiện cho cấu trúc cơ sở dữ liệu.

  • package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    2: Hibernate thay đổi cơ sở dữ liệu theo các cấu trúc thực thể đã cho.

  • package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    3: Tạo cơ sở dữ liệu mỗi lần nhưng không bỏ nó.

  • package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    4: Tạo cơ sở dữ liệu và bỏ nó khi
    package com.example.accessingdatamysql;
    
    import org.springframework.data.repository.CrudRepository;
    
    import com.example.accessingdatamysql.User;
    
    // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
    // CRUD refers Create, Read, Update, Delete
    
    public interface UserRepository extends CrudRepository {
    
    }
    0 đóng.

Bạn phải bắt đầu với

package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
3 hoặc
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
2, vì bạn chưa có cấu trúc cơ sở dữ liệu. Sau lần chạy đầu tiên, bạn có thể chuyển nó sang
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
2 hoặc
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
1, theo yêu cầu của chương trình. Sử dụng
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
2 Khi bạn muốn thực hiện một số thay đổi đối với cấu trúc cơ sở dữ liệu.

Mặc định cho

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
7 và cơ sở dữ liệu nhúng khác là
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
4. Đối với các cơ sở dữ liệu khác, chẳng hạn như
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
6, mặc định là
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
1.

Đó là một thông lệ bảo mật tốt, sau khi cơ sở dữ liệu của bạn ở trong trạng thái sản xuất, đặt điều này thành
package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}
1, thu hồi tất cả các đặc quyền từ người dùng MySQL được kết nối với ứng dụng Spring và chỉ cung cấp cho người dùng MySQL
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
1,
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
2,
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
3 và
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
4. Bạn có thể đọc thêm về điều này ở cuối hướng dẫn này.

Tạo mô hình
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
5

Bạn cần tạo mô hình thực thể, như danh sách sau [trong

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
6] hiển thị:

package com.example.accessingdatamysql;

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

@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue[strategy=GenerationType.AUTO]
  private Integer id;

  private String name;

  private String email;

  public Integer getId[] {
    return id;
  }

  public void setId[Integer id] {
    this.id = id;
  }

  public String getName[] {
    return name;
  }

  public void setName[String name] {
    this.name = name;
  }

  public String getEmail[] {
    return email;
  }

  public void setEmail[String email] {
    this.email = email;
  }
}

Hibernate tự động dịch thực thể vào một bảng.

Tạo kho lưu trữ

Bạn cần tạo kho lưu trữ giữ hồ sơ người dùng, như danh sách sau [trong

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
7] hiển thị:

package com.example.accessingdatamysql;

import org.springframework.data.repository.CrudRepository;

import com.example.accessingdatamysql.User;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository {

}

Spring tự động thực hiện giao diện kho lưu trữ này trong một hạt đậu có cùng tên [với một thay đổi trong trường hợp - nó được gọi là

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
8].

Tạo một bộ điều khiển

Bạn cần tạo bộ điều khiển để xử lý các yêu cầu HTTP cho ứng dụng của mình, vì danh sách sau [trong

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}
9] hiển thị:

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping[path="/demo"] // This means URL's start with /demo [after Application path]
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping[path="/add"] // Map ONLY POST Requests
  public @ResponseBody String addNewUser [@RequestParam String name
      , @RequestParam String email] {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User[];
    n.setName[name];
    n.setEmail[email];
    userRepository.save[n];
    return "Saved";
  }

  @GetMapping[path="/all"]
  public @ResponseBody Iterable getAllUsers[] {
    // This returns a JSON or XML with the users
    return userRepository.findAll[];
  }
}

Ví dụ trước chỉ định rõ ràng
package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
0 và
package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
1 cho hai điểm cuối. Theo mặc định,
package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
2 Bản đồ tất cả các hoạt động HTTP.

Tạo một lớp ứng dụng

Spring IniticizR tạo một lớp đơn giản cho ứng dụng. Danh sách sau đây cho thấy lớp khởi tạo được tạo cho ví dụ này [trong

package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
3]:

package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}

Đối với ví dụ này, bạn không cần sửa đổi lớp

package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
4.

package com.example.accessingdatamysql;

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

@SpringBootApplication
public class AccessingDataMysqlApplication {

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

}
5 là một chú thích thuận tiện thêm tất cả những điều sau đây:

  • package com.example.accessingdatamysql;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AccessingDataMysqlApplication {
    
      public static void main[String[] args] {
        SpringApplication.run[AccessingDataMysqlApplication.class, args];
      }
    
    }
    6: Thẻ lớp là nguồn định nghĩa đậu cho bối cảnh ứng dụng.

  • package com.example.accessingdatamysql;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AccessingDataMysqlApplication {
    
      public static void main[String[] args] {
        SpringApplication.run[AccessingDataMysqlApplication.class, args];
      }
    
    }
    7: Nói với Spring Boot để bắt đầu thêm đậu dựa trên cài đặt đường dẫn lớp, các loại đậu khác và các cài đặt thuộc tính khác nhau. Ví dụ: nếu
    package com.example.accessingdatamysql;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AccessingDataMysqlApplication {
    
      public static void main[String[] args] {
        SpringApplication.run[AccessingDataMysqlApplication.class, args];
      }
    
    }
    8 nằm trên đường dẫn lớp, chú thích này đánh dấu ứng dụng dưới dạng ứng dụng web và kích hoạt các hành vi chính, chẳng hạn như thiết lập
    package com.example.accessingdatamysql;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AccessingDataMysqlApplication {
    
      public static void main[String[] args] {
        SpringApplication.run[AccessingDataMysqlApplication.class, args];
      }
    
    }
    9.

  • java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
    0: Nói với Spring để tìm kiếm các thành phần, cấu hình và dịch vụ khác trong gói
    java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
    1, cho phép nó tìm các bộ điều khiển.

Phương pháp

java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
2 sử dụng phương thức Spring Boot
java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
3 để khởi chạy một ứng dụng. Bạn có nhận thấy rằng không có một dòng XML nào không? Không có tệp
java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
4. Ứng dụng web này là Java thuần túy 100% và bạn không phải đối phó với việc định cấu hình bất kỳ hệ thống ống nước hoặc cơ sở hạ tầng nào.

Xây dựng một cái lọ thực thi

Bạn có thể chạy ứng dụng từ dòng lệnh với Gradle hoặc Maven. Bạn cũng có thể xây dựng một tệp JAR thực thi duy nhất có chứa tất cả các phụ thuộc, lớp và tài nguyên cần thiết và chạy điều đó. Xây dựng một bình có thể thực thi giúp bạn dễ dàng vận chuyển, phiên bản và triển khai dịch vụ như một ứng dụng trong suốt vòng đời phát triển, trên các môi trường khác nhau, v.v.

Nếu bạn sử dụng Gradle, bạn có thể chạy ứng dụng bằng cách sử dụng

java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
5. Ngoài ra, bạn có thể xây dựng tệp JAR bằng cách sử dụng
java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
6 và sau đó chạy tệp JAR, như sau:

java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar

Nếu bạn sử dụng Maven, bạn có thể chạy ứng dụng bằng cách sử dụng

java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
7. Ngoài ra, bạn có thể xây dựng tệp JAR với
java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
8 và sau đó chạy tệp JAR, như sau:

java -jar target/gs-accessing-data-mysql-0.1.0.jar

Khi bạn chạy ứng dụng, đầu ra ghi nhật ký được hiển thị. Dịch vụ nên được tăng và chạy trong vài giây.

Kiểm tra ứng dụng

Bây giờ ứng dụng đang chạy, bạn có thể kiểm tra nó bằng cách sử dụng

java -jar build/libs/gs-accessing-data-mysql-0.1.0.jar
9 hoặc một số công cụ tương tự. Bạn có hai điểm cuối HTTP mà bạn có thể kiểm tra:

java -jar target/gs-accessing-data-mysql-0.1.0.jar
0: Nhận tất cả dữ liệu.
java -jar target/gs-accessing-data-mysql-0.1.0.jar
1: Thêm một người dùng vào dữ liệu.

Lệnh Curl sau đây thêm người dùng:

Câu trả lời phải như sau:

Lệnh sau đây hiển thị tất cả người dùng:

$ curl 'localhost:8080/demo/all'

Câu trả lời phải như sau:

Lệnh sau đây hiển thị tất cả người dùng:

Thực hiện một số thay đổi bảo mật

Khi bạn đang ở trong môi trường sản xuất, bạn có thể tiếp xúc với các cuộc tấn công tiêm SQL. Một hacker có thể tiêm

java -jar target/gs-accessing-data-mysql-0.1.0.jar
2 hoặc bất kỳ lệnh SQL phá hủy nào khác. Vì vậy, như một thông lệ bảo mật, bạn nên thực hiện một số thay đổi đối với cơ sở dữ liệu của mình trước khi bạn đưa ứng dụng cho người dùng của mình.

mysql> revoke all on db_example.* from 'springuser'@'%';

Lệnh sau đây thu hồi tất cả các đặc quyền từ người dùng được liên kết với ứng dụng Spring:

Bây giờ ứng dụng lò xo không thể làm bất cứ điều gì trong cơ sở dữ liệu.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
0

Ứng dụng phải có một số đặc quyền, vì vậy hãy sử dụng lệnh sau để cấp các đặc quyền tối thiểu mà ứng dụng cần:

Loại bỏ tất cả các đặc quyền và cấp một số đặc quyền cung cấp cho ứng dụng Spring của bạn các đặc quyền cần thiết để thực hiện các thay đổi chỉ cho dữ liệu của cơ sở dữ liệu chứ không phải cấu trúc [lược đồ].

  1. Khi bạn muốn thay đổi cơ sở dữ liệu:

  2. Quyền lợi.

  3. Thay đổi

    package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    0 thành
    package com.example.accessingdatamysql;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity // This tells Hibernate to make a table out of this class
    public class User {
      @Id
      @GeneratedValue[strategy=GenerationType.AUTO]
      private Integer id;
    
      private String name;
    
      private String email;
    
      public Integer getId[] {
        return id;
      }
    
      public void setId[Integer id] {
        this.id = id;
      }
    
      public String getName[] {
        return name;
      }
    
      public void setName[String name] {
        this.name = name;
      }
    
      public String getEmail[] {
        return email;
      }
    
      public void setEmail[String email] {
        this.email = email;
      }
    }
    2.

Chạy lại các ứng dụng của bạn.

Sau đó lặp lại hai lệnh hiển thị ở đây để làm cho ứng dụng của bạn an toàn để sử dụng lại. Vẫn tốt hơn, sử dụng một công cụ di chuyển chuyên dụng, chẳng hạn như Flyway hoặc Liquibase.

Bản tóm tắt

Làm thế nào để các thuộc tính ứng dụng khởi động Spring kết nối với cơ sở dữ liệu MySQL?

Các bước để tích hợp MySQL với Spring Boot..
Bước 1: Tạo cơ sở dữ liệu MySQL ..
Bước 2: Nối phụ thuộc MySQL ..
Bước 3: Đặt cấu hình kết nối MYSQL khởi động lò xo ..
Bước 4: Xây dựng lớp kho lưu trữ cho Spring Boot ..
Bước 5: Chuyển đổi lớp blog thành thực thể ..
Bước 6: Thêm bộ điều khiển vào Spring Boot MYSQL tích hợp ..

Làm thế nào để dữ liệu lò xo JPA kết nối với cơ sở dữ liệu?

Bắt đầu với Spring Inititalizr..
Chọn Gradle hoặc Maven và ngôn ngữ bạn muốn sử dụng.....
Nhấp vào phụ thuộc và chọn JPA dữ liệu lò xo và sau đó là cơ sở dữ liệu H2 ..
Nhấp vào Tạo ..
Tải xuống tệp zip kết quả, đây là kho lưu trữ của ứng dụng web được cấu hình với các lựa chọn của bạn ..

Chúng ta có thể định cấu hình MySQL với Spring Boot không?

Spring Boot có hỗ trợ cho MySQL và các cơ sở dữ liệu quan hệ phổ biến khác. Trong bài đăng này, chúng tôi sẽ tập trung vào việc định cấu hình Spring Boot cho cơ sở dữ liệu MySQL.

Làm thế nào để ứng dụng khởi động Spring kết nối với cơ sở dữ liệu bằng JDBC?

Để truy cập cơ sở dữ liệu quan hệ bằng cách sử dụng JDBCTEMPLATE trong ứng dụng khởi động Spring, chúng tôi cần thêm phụ thuộc JDBC khởi động Spring khởi động trong tệp cấu hình bản dựng của chúng tôi.Sau đó, nếu bạn @Autowired lớp JDBCTemplate, Spring Boot sẽ tự động kết nối cơ sở dữ liệu và đặt dữ liệu cho đối tượng JDBCTEMPLATE.add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

Bài Viết Liên Quan

Chủ Đề