スプリングブートはローカルホストMySQLにどのように接続しますか
これには、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
上記の構文を理解するために、テーブルを作成しましょう-
mysql> create table demo71 −> ( −> id int, −> name varchar(20) −> ); Query OK, 0 rows affected (3.81 sec)
挿入コマンド-
を使用して、いくつかのレコードをテーブルに挿入します。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)
selectステートメントを使用してテーブルのレコードを表示する-
mysql> select *from demo71;
これにより、次の出力が生成されます-
+------+-------+ | id | name | +------+-------+ | 100 | John | | 101 | David | | 102 | Bob | +------+-------+ 3 rows in set (0.00 sec)
上記のapplication.propertiesを確認するには ローカルのMySQLを使用しているかどうかに関係なく、テスト用のSpringBootアプリケーションを作成できます。
以下はapplication.propertiesファイルです。
以下はコントローラーのクラスコードです。コードは次のとおりです-
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();
}
} 以下はメインクラスです。 Javaコードは次のとおりです-
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);
}
} 上記を実行するには、メインクラスに移動し、右クリックして[Javaアプリケーションとして実行]を選択します。 」。正常に実行されたら、URLの下を押す必要があります。
URLは次のとおりです-
https://localhost:8093/table/demo71
これにより、次の出力が生成されます-
-
Springboot + JSP + Spring Security:データソースの設定に失敗しました。 MySQLでデータソースを設定する方法は?
Springbootでデータソースを構成するには、 application.propertiesにデータソースを定義できます。 。 Springbootのapplication.propertiesは次のとおりです- spring.datasource.username=yourUserName spring.datasource.password=yourPassword spring.datasource.url=yourDatabaseUrl spring.datasource.driver-class-name=yourDriverClassName プロジェクトの構成は次のとおり
-
IntelliJでMySQLサーバーに接続する方法の説明
ご存知のとおり、MySQLサーバーと並行してMySQL Workbenchを使用すると、データベースの操作がより簡単で直感的になります。 しかし、最近では、ソースコードのコンパイルとコンパイルをサポートするだけでなく、データベース管理システムの操作もサポートする最新のIDEが数多くあります。 その代表的なものが、優れた機能を備えたJetBrains製品です。この記事では、InteliJを使用してMySQLサーバーにリンクする方法を説明します。これは、今日多くのプログラマーが使用しているIDEです。 では、今すぐ始めましょう! IntelliJでMySQLサーバーにリンクするため