SpringBoot + MyBatisでアプリ実行できない

実現したいこと

Java初学者です。
SpringBoot + MyBatisでmysqlに接続する簡単なコードを作成中ですが、実行できずエラーになります。

前提

  • SpringBoot:3.2.0
  • Java:17.0.9
  • 環境:IntelliJ

発生している問題・エラーメッセージ

実行すると、2行目でfinished with non-zero exit value 1のメッセージが出ています。

Execution failed for task ':FirstAppApplication.main()'. > Process 'command '/***/***/Library/Java/JavaVirtualMachines/corretto-17.0.9/Contents/Home/bin/java'' finished with non-zero exit value 1 * 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. > Get more help at https://help.gradle.org. Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD FAILED in 1s 3 actionable tasks: 2 executed, 1 up-to-date

該当のソースコード

build.gradle

1dependencies { 2 implementation 'org.springframework.boot:spring-boot-starter-web' 3 testImplementation 'org.springframework.boot:spring-boot-starter-test' 4 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 5 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.2' 6 runtimeOnly 'com.mysql:mysql-connector-j' 7 compileOnly 'org.projectlombok:lombok' 8 annotationProcessor 'org.projectlombok:lombok' 9}

PostController.java

1package in.training.firstapp; 2 3import lombok.RequiredArgsConstructor; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.GetMapping; 7 8@Controller 9@RequiredArgsConstructor 10public class PostController { 11 private final PostRepository postRepository; 12 13 @GetMapping("/hello") 14 public String showHello(Model model){ 15 var sampleText = "サンプルテキスト"; 16 model.addAttribute("sampleText", sampleText); 17 return "hello"; 18 } 19 20 @GetMapping 21 public String showList(Model model){ 22 var postList = postRepository.findAll(); 23 model.addAttribute("postList", postList); 24 return "index"; 25 } 26}

PostEntity.Java

1package in.training.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}

PostRepository.Java

1package in.training.firstapp; 2 3import org.apache.ibatis.annotations.Mapper; 4import org.apache.ibatis.annotations.Select; 5 6import java.util.List; 7 8@Mapper 9public interface PostRepository { 10 @Select("select * from posts") 11 List<PostEntity> findAll(); 12}

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);

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>

試したこと

PostController.javaの
@RequiredArgsConstructorや
private final PostRepository postRepository;
など、PostRepository関連の記述を削除するとエラーにならないので、データベース接続に関するエラーだと思っています。
mysqlは起動済み、接続したいテーブルも作成済みです。
調べたところライブラリのバージョン周りのエラーかも、という情報がありましたが、解決に至っておりません。

コメントを投稿

0 コメント