実現したいこと
postsテーブルを作成したいです。
前提
Javaを習い始めて間もないのですが、簡易的なアプリ作成をしています。その中でMySQLを導入したらpostsテーブルが作成されるとのことだったのですが作成されません。習い始めで分からないのですが、おそらくエラーも出ていない気がするのですが、見方がわからないです、、
発生している問題・エラーメッセージ
エラーメッセージ 12:30:13: ':FirstAppApplication.main()' を実行中... > Task :compileJava UP-TO-DATE > Task :processResources UP-TO-DATE > Task :classes UP-TO-DATE > Task :FirstAppApplication.main() . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.12) 2023-05-29 12:30:16.752 INFO 62598 --- [ main] i.techcamp.firstapp.FirstAppApplication : Starting FirstAppApplication using Java 17.0.7 on ueharakentanoMacBook-Air.local with PID 62598 (/Users/ueharakenta/java_projects/firstapp/build/classes/java/main started by ueharakenta in /Users/ueharakenta/java_projects/firstapp) 2023-05-29 12:30:16.753 INFO 62598 --- [ main] i.techcamp.firstapp.FirstAppApplication : No active profile set, falling back to 1 default profile: "default" 2023-05-29 12:30:16.990 WARN 62598 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[in.techcamp.firstapp]' package. Please check your configuration. 2023-05-29 12:30:17.161 INFO 62598 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2023-05-29 12:30:17.166 INFO 62598 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-05-29 12:30:17.166 INFO 62598 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.75] 2023-05-29 12:30:17.205 INFO 62598 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-05-29 12:30:17.205 INFO 62598 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 431 ms 2023-05-29 12:30:17.284 INFO 62598 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2023-05-29 12:30:17.407 INFO 62598 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2023-05-29 12:30:17.412 INFO 62598 --- [ main] i.techcamp.firstapp.FirstAppApplication : Started FirstAppApplication in 1.056 seconds (JVM running for 1.261) 2023-05-29 12:30:27.571 INFO 62598 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2023-05-29 12:30:27.571 INFO 62598 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2023-05-29 12:30:27.573 INFO 62598 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
該当のソースコード
Postcontroller.java
1package in.techcamp.firstapp; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.web.bind.annotation.GetMapping; 6 7import java.util.List; 8 9@Controller 10public class PostController { 11 @GetMapping("/hello") 12 public String showHello(Model model) { 13 var sampleText = "サンプルテキスト"; 14 model.addAttribute("sampleText", sampleText); 15 return "hello"; 16 } 17 18 @GetMapping 19 public String showList(Model model) { 20 var postList = List.of( 21 new PostEntity(1, "投稿1"), 22 new PostEntity(2, "投稿2"), 23 new PostEntity(3, "投稿3") 24 ); 25 model.addAttribute("postList", postList); 26 return "index"; 27 } 28}
build.gradle
1plugins { 2 id 'java' 3 id 'org.springframework.boot' version '2.7.12' 4 id 'io.spring.dependency-management' version '1.0.15.RELEASE' 5} 6 7group = 'in.techcamp' 8version = '0.0.1-SNAPSHOT' 9sourceCompatibility = '17' 10 11repositories { 12 mavenCentral() 13} 14 15dependencies { 16 implementation 'org.springframework.boot:spring-boot-starter-web' 17 testImplementation 'org.springframework.boot:spring-boot-starter-test' 18 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 19 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' 20 runtimeOnly 'com.mysql:mysql-connector-j' 21 compileOnly 'org.projectlombok:lombok' 22 annotationProcessor 'org.projectlombok:lombok' 23} 24 25tasks.named('test') { 26 useJUnitPlatform() 27}
application.properties
1spring.sql.init.mode=always 2spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3spring.datasource.url=jdbc:mysql://localhost:3306/first_app_java 4spring.datasource.username=root 5spring.datasource.password=
schema.sql
1CREATE TABLE IF NOT EXISTS posts ( 2 id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, 3 memo VARCHAR(256) NOT NULL 4);
PostEntity.java
1package in.techcamp.firstapp; 2 3public class PostEntity { 4 private long id; 5 private String memo; 6 7 public PostEntity(long id, String memo) { 8 this.id = id; 9 this.memo = memo; 10 } 11 12 public long getId() { 13 return id; 14 } 15 16 public void setId(long id) { 17 this.id = id; 18 } 19 20 public String getMemo() { 21 return memo; 22 } 23 24 public void setMemo(String memo) { 25 this.memo = memo; 26 } 27} 28
index.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6</head> 7<body> 8<ul th:each="post:${postList}"> 9 <li th:text="${post.getMemo()}"> 10 </li> 11</ul> 12</body> 13</html>
試したこと
ネットで検索してみてはいるんですが、どうしていいのかさっぱりわからないです。
コンソールログを見る限りでは目立ったエラーは出ていないようです。
> その中でMySQLを導入したらpostsテーブルが作成されるとのことだったのですが作成されません。
postsテーブルが作成されていないと判断された理由や方法があれば教えてください。

コメントありがとうございます。
Sequel Proというアプリでデータベースを表示させているのですが、カリキュラムには実行したらpostsテーブルがSequel Proに表示されてそこにデータを直接入力するという流れになっていたのですが、そのpostsテーブルが表示されなくて作成がされていないと判断しました。
①Sequel Proの接続先や接続ユーザは正しいですか?
②schema.sqlはsrc/main/resourcesディレクトリに設置していますか?

確認したところ、ディレクトリは正常に設置されています。
Sequel Proには、first_app_java の中にposts が追加されるとのことだったのですが、first_app_javaがしっかり作られています。更新ボタンを押したりアプリを一回落としてから立ち上げたりなどやってみましたが、反映はされないです。

アプリの停止を行いました。するとこんな文がターミナルで表示されました。
Execution failed for task ':FirstAppApplication.main()'.
> Build cancelled while executing task ':FirstAppApplication.main()'
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
これは何か関係しているのでしょうか。
スタックトレースオプションを有効にしてエラーを出力させ、それを読んで問題を特定するしかありませんね
Sequel Proの接続先は正しそう、schema.sqlも正しい場所に設置されているとなると、ちょっと原因がわからないですね…。気になるのはソースコード上でDBへアクセスする処理がないことです。Spring Boot起動時のコンポーネントスキャンで、DBアクセスなしと判定され、schema.sqlが実行されていない可能性があります。カリキュラムの作成者に問い合わせていただくほうが早いかもしれません。
またはapplication.propertiesに以下のプロパティを追記してみるのはどうでしょうか?
spring.sql.init.schema-locations=schema.sql

回答2件
あなたの回答
tips
プレビュー