Hướng dẫn can we configure mysql with spring boot? - chúng ta có thể định cấu hình mysql với khởi động mùa xuân không?

Trong bài đăng này, chúng tôi sẽ thảo luận về cách định cấu hình MySQL cho ứng dụng khởi động Spring. Chúng tôi sẽ nhận được chi tiết cho sự hỗ trợ được cung cấp bởi Spring Boot cho cơ sở dữ liệu MySQL.configure MySQL for Spring Boot application. We will get into details for the support provided by Spring Boot for MySQL database.

Spring Boot cung cấp hỗ trợ sẵn sàng sử dụng cho cơ sở dữ liệu H2.Spring Boot tự động thiết lập trong cơ sở dữ liệu bộ nhớ H2 Nếu nó phát hiện cấu hình H2 trong ClassPath. Cơ sở dữ liệu cấp sản xuất như MySQL, Oracle, v.v.

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. and other popular relational databases.In this post, we will focus on configuring Spring Boot for MySQL database.

1. Cấu hình cơ sở dữ liệu MySQL

Tôi sẽ không bao gồm cài đặt máy chủ MySQL trên máy của bạn hoặc cách tạo cơ sở dữ liệu mới trong MySQL. Đối với bài đăng này, tôi giả sử rằng chúng tôi đã cài đặt MySQL trên máy của bạn. Vui lòng tạo một cơ sở dữ liệu mới trên máy chủ MySQL (hãy gọi cho cơ sở dữ liệu mới này là javadevjournal).

2. Thêm phụ thuộc MySQL

Để cho phép hỗ trợ cho MySQL trong dự án của chúng tôi, chúng tôi cần thêm & nbsp; ________ 8 và mysql-connector-java STARTER trong tệp pom.xml.

<dependencies>
  <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  <!-- Use MySQL Connector-J -->
   <dependency>
      <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
<dependencies>

Spring Boot Auto Conf Configure DataSource if & nbsp; Spring-boot-boot-starter-data-jpa & nbsp; nằm trong đường dẫn lớp bằng cách đọc các cấu hình cơ sở dữ liệu được xác định trong

spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
spring.datasource.username=javadevjournal
spring.datasource.password=ThePassword
spring.jpa.hibernate.ddl-auto=update

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
0

3. Định cấu hình MySQL bằng các thuộc tính khởi động lò xo

Theo mặc định, Spring Boot cung cấp các cấu hình cơ sở dữ liệu cho cơ sở dữ liệu H2. Để sử dụng MySQL cho ứng dụng của mình, chúng tôi cần ghi đè các cấu hình mặc định này. Một lần chúng tôi xác định các thuộc tính DB trong tệp ứng dụng của dự án. .

spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
spring.datasource.username=javadevjournal
spring.datasource.password=ThePassword
spring.jpa.hibernate.ddl-auto=update

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

Thuộc tính

spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
spring.datasource.username=javadevjournal
spring.datasource.password=ThePassword
spring.jpa.hibernate.ddl-auto=update

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
1 là để khởi tạo cơ sở dữ liệu. Đó là thực tiễn bảo mật tốt mà sau khi cơ sở dữ liệu của bạn ở trạng thái sản xuất, bạn thực hiện điều này
spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
spring.datasource.username=javadevjournal
spring.datasource.password=ThePassword
spring.jpa.hibernate.ddl-auto=update

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
2. Dưới đây là một số giá trị DDL thường được sử dụng:

  • spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    2: Mặc định cho
    spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    4. Chúng tôi không thay đổi cấu trúc cơ sở dữ liệu.
  • spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    5: Hibernate thay đổi cơ sở dữ liệu theo các cấu trúc thực thể.
  • spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    6: Tạo cơ sở dữ liệu mỗi lần nhưng không bỏ nó.
  • spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    7: Tạo cơ sở dữ liệu và bỏ nó khi
    spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
    spring.datasource.username=javadevjournal
    spring.datasource.password=ThePassword
    spring.jpa.hibernate.ddl-auto=update
    
    ## Hibernate Properties
    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    8 đóng.

Trong khi thiết lập các thuộc tính JPA, chúng tôi không cần chỉ định trình điều khiển hoặc phương ngữ. Chúng được phát hiện tự động. Nếu bạn muốn tùy chỉnh các thuộc tính này, vui lòng thêm/tùy chỉnh bằng tệp

spring.datasource.url=jdbc:mysql://localhost:3306/javadevjournal
spring.datasource.username=javadevjournal
spring.datasource.password=ThePassword
spring.jpa.hibernate.ddl-auto=update

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
9.

Đây là những thay đổi cấu hình duy nhất cần thiết để sử dụng MySQL với ứng dụng khởi động Spring của mình. Bạn bắt đầu ứng dụng, Spring Boot sẽ sử dụng MySQL làm cơ sở dữ liệu cơ bản của bạn.

Phần tiếp theo là tùy chọn và cung cấp một ví dụ hoạt động như cách định cấu hình MySQL cho ứng dụng khởi động Spring và lưu dữ liệu trong cơ sở dữ liệu với sự trợ giúp của Spring JPA.configure MySQL for Spring Boot application and save data in the databases with help of Spring JPA.

4. Tạo thực thể JPA Create JPA Entity

Để lưu dữ liệu trong cơ sở dữ liệu bằng JPA Spring, chúng ta cần tạo một mô hình thực thể. Hãy để tạo ra một thực thể người dùng sẽ lưu trữ dữ liệu trong cơ sở dữ liệu. Đây là cách mà thực thể của chúng ta sẽ trông như thế nào:

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}

Hibernate sẽ tự động chuyển đổi thực thể này thành bảng cơ bản trong cơ sở dữ liệu MySQL. Bước tiếp theo là tạo kho lưu trữ. Điều này sẽ giúp chúng tôi thực hiện thao tác CRUD bình thường trong cơ sở dữ liệu. Chúng tôi đang sử dụng giao diện kho lưu trữ lò xo tạo ra ánh xạ cần thiết cho chúng tôi và giúp chúng tôi tránh viết mã Boilerplate.

5. Tạo kho lưu trữ người dùng

@Repository
public interface UserRepository extends JpaRepository < UserEntity, Long > {
    UserEntity findByEmail(String email);
}

Đây là một giao diện và chúng tôi mở rộng

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
0. Spring sẽ tự động tạo một hạt đậu cho mục nhập này (tên Bean là
@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
1) và cung cấp các triển khai cho các hoạt động sữa đông. Bước tiếp theo là tạo bộ điều khiển MVC Spring đơn giản và gọi
@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
2 để lưu thông tin trong cơ sở dữ liệu.

6. Bộ điều khiển người dùng

Hãy để tạo ra một bộ điều khiển người dùng đơn giản để tương tác với

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
1 của chúng tôi

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;
import com.javadevjournal.core.user.jpa.repository.UserRepository;
import com.javadevjournal.core.user.jpa.data.UserEntity;

@Controller
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping(path = "/add")
    public @ResponseBody String addNewUser(@RequestParam String firstName, @RequestParam String firstName, @RequestParam String email) {

        UserEntity user = new UserEntity();
        user.setFirstName(firstName);
        user.setLastName(firstName);
        user.setEmail(email);
        userRepository.save(user);
        return "User Created";
    }

    @GetMapping(path = "/all")
    public @ResponseBody Iterable < User > getAllUsers() {
        return userRepository.findAll();
    }

}

Nếu chúng tôi chạy ứng dụng của mình, chúng tôi có thể tạo người dùng trong cơ sở dữ liệu bằng cách gọi

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
4 với các tham số yêu cầu và có thể nhận tất cả người dùng bằng cách gọi
@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
5Method.

7. Ứng dụng thử nghiệm

Để kiểm tra ứng dụng của chúng tôi, chúng tôi có thể sử dụng lệnh Postman hoặc Curl đơn giản. Chạy lệnh

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
6 sau để tạo người dùng trong cơ sở dữ liệu.

$ curl localhost:8080/user/add -d firstName=Java -d lastName=DevJournal -d [email protected]

Để có được danh sách tất cả khách hàng đã lưu trong cơ sở dữ liệu, hãy chạy lệnh

@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    @Column(unique = true)
    private String email;
    private String password;

    //getter setters
}
6 sau

$ curl 'localhost:8080/user/all'

Bản tóm tắt

Trong bài đăng ngắn này, chúng tôi đã đề cập & NBSP; Định cấu hình MySQL cho ứng dụng khởi động Spring. Chúng tôi đã học cách ghi đè các cấu hình nguồn dữ liệu mặc định được cung cấp bởi Spring Boot để sử dụng MySQL làm cơ sở dữ liệu cơ bản. Mã nguồn có sẵn trên khoconfigure MySQL for Spring Boot application.We learned how to override default data source configurations provided by Spring Boot to use MySQL as the underlying database.The source code is available on our GitHub Repository

Làm thế nào để khởi động mùa xuân tích hợp với mysql?

Cách kết nối khởi động lò xo với MySQL..
Bước 1 - Tạo dự án ..
Bước 2 Tạo một db ..
Bước 3 Tạo một thực thể / mô hình / bảng sẽ được đặt trong cơ sở dữ liệu ..
Bước 4, Thêm điều này vào ứng dụng. đặc tính. server.port = your_port. spring.jpa.Generate-ddl = true. spring.jpa.show-sql = true ..

Spring Boot có thể tạo cơ sở dữ liệu không?

86.3 Khởi tạo khởi động lò xo cơ sở dữ liệu có thể tự động tạo lược đồ (tập lệnh DDL) của dữ liệu của bạn và khởi tạo nó (tập lệnh DML).Nó tải SQL từ các vị trí class -class tiêu chuẩn: lược đồ.SQL và dữ liệu.Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data.

Cơ sở dữ liệu nào được sử dụng trong Spring Boot?

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à H2.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 ứng dụng.Tệp thuộc tính.the default database is H2 . Consequently, when you want to use any other database, you must define the connection attributes in the application. properties file.

Tôi có thể sử dụng MySQL trong Eclipse không?

Nhấp chuột phải vào dự án Java của bạn trong Gói Explorer trong không gian làm việc của Eclipse và đi đến các thuộc tính đi xuống danh sách xuất hiện cho đến khi bạn tìm thấy đường dẫn xây dựng Java và nhấp vào thư viện và sau đó nhấp vào Thêm Lưu trữ bên ngoài.Tìm tệp JAR được tải xuống có tên MySQL-Connector-Java-Version số.jar và chọn nó.