Computer >> Máy Tính >  >> Lập trình >> MySQL

Cách khởi động mùa xuân kết nối localhost MySQL


Đối với điều này, hãy sử dụng application.properties -

spring.datasource.username=yourMySQLUserName
spring.datasource.password=yourMySQLPassword
spring.datasource.url=jdbc:mysql://localhost:3306/yoruDatabaseName
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Để hiểu cú pháp trên, chúng ta hãy tạo một bảng -

mysql> create table demo71
−> (
−> id int,
−> name varchar(20)
−> );
Query OK, 0 rows affected (3.81 sec)

Chèn một số bản ghi vào bảng với sự trợ giúp của lệnh insert -

mysql> insert into demo71 values(100,'John');
Query OK, 1 row affected (0.13 sec)

mysql> insert into demo71 values(101,'David');
Query OK, 1 row affected (0.49 sec)

mysql> insert into demo71 values(102,'Bob');
Query OK, 1 row affected (0.15 sec)

Hiển thị các bản ghi từ bảng bằng cách sử dụng câu lệnh select -

mysql> select *from demo71;

Điều này sẽ tạo ra kết quả sau -

+------+-------+
| id   | name  |
+------+-------+
| 100  | John  |
| 101  | David |
| 102  | Bob  |
+------+-------+
3 rows in set (0.00 sec)

Để xác minh application.properties ở trên có đang hoạt động với MySQL cục bộ hay không, bạn có thể viết ứng dụng khởi động mùa xuân để kiểm tra.

Sau đây là tệp application.properties.

Cách khởi động mùa xuân kết nối localhost MySQL

Sau đây là mã lớp bộ điều khiển. Mã như sau -

package com.demo.controller;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/table")
public class TableController {
   @Autowired
   EntityManager entityManager;
   @ResponseBody
   @GetMapping("/demo71")
   public String getData() {
      Query sqlQuery= entityManager.createNativeQuery("select name from demo71");
      List<String> result= sqlQuery.getResultList();
      StringBuilder sb=new StringBuilder();
      Iterator itr= result.iterator();
      while(itr.hasNext()) {
         sb.append(itr.next()+" ");
      }
      return sb.toString();
   }
}

Sau đây là lớp học chính. Mã Java như sau -

package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JavaMysqlDemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(JavaMysqlDemoApplication.class, args);
   }
}

Để chạy phần trên, hãy chuyển đến lớp chính và nhấp chuột phải và chọn “ Chạy dưới dạng ứng dụng Java ”.Sau khi thực hiện thành công, bạn cần nhấn vào url bên dưới.

Url như sau -

https://localhost:8093/table/demo71

Điều này sẽ tạo ra kết quả sau -

Cách khởi động mùa xuân kết nối localhost MySQL