Dockerを使って、SpringbootとMysqlのコンテナを立ち上げ、MybatisでDB接続しようとしています。
DB接続を試しにサンプルで実行してみようとこのこのサイトを参考にソースを書いて、実行したのですが、DBから情報を取得するところでエラーが出ます。
apprivcation.propertiesの設定やdocker-compose,build.gradleなどの設定を何度も見返しましたが、
原因がわかりませんでした。
teratailなどで同様のエラーを見つけたのですが、apprication.propertiesの設定ミスが多いかったのですが、エラーを見つけることができませんでした。
一週間悩み続けても解決できず、今回投稿させていただきました。
すみませんが、ご教授いただければ幸いです。
よろしくお願いします。
エラーメッセージ
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Sep 11 02:21:35 UTC 2022 There was an unexpected error (type=Internal Server Error, status=500). nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. ### The error may exist in com/example/demo/dao/BookDao.xml ### The error may involve com.example.demo.dao.BookDao.findAll ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. ### The error may exist in com/example/demo/dao/BookDao.xml ### The error may involve com.example.demo.dao.BookDao.findAll ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441) at jdk.proxy2/jdk.proxy2.$Proxy57.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86) at jdk.proxy3/jdk.proxy3.$Proxy59.findAll(Unknown Source) at com.example.demo.service.BookService.getBookList(BookService.java:23) at com.example.demo.controller.BookController.index(BookController.java:32) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) (略)
ソースコード
docker
1version: '3.6' 2services: 3 app: 4 container_name: app 5 build: 6 context: . 7 dockerfile: ./docker/java/Dockerfile 8 ports: 9 - 8080:8080 10 tty: true 11 volumes: 12 - ./server:/srv:cached 13 working_dir: /srv 14 command: /bin/bash -c "./gradlew bootRun" 15 16 mysql: 17 image: mysql:5.7 18 container_name: db 19 ports: 20 - 3306:3306 21 volumes: 22 - ./docker/db/mysql:/var/lib/mysql 23 - ./docker/db/initdb:/docker-entrypoint-initdb.d/ 24 - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf 25 environment: 26 MYSQL_ROOT_PASSWORD: password 27 MYSQL_DATABASE: library 28 MYSQL_USER: user 29 MYSQL_PASSWORD: password 30 31 phpmyadmin: 32 image: phpmyadmin/phpmyadmin 33 container_name: phpmyadmin 34 environment: 35 - PMA_ARBITRARY=1 36 - PMA_HOST=mysql 37 - PMA_USER=root 38 - PMA_PASSWORD=password 39 links: 40 - mysql 41 ports: 42 - 4040:80 43 volumes: 44 - ./docker/phpmyadmin/sessions:/sessions
build.gradle
1dependencies { 2 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 3 implementation 'org.springframework.boot:spring-boot-starter-web' 4 implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2' 5 runtimeOnly 'mysql:mysql-connector-java' 6 compileOnly 'org.projectlombok:lombok' 7 developmentOnly 'org.springframework.boot:spring-boot-devtools' 8 annotationProcessor 'org.projectlombok:lombok' 9 testImplementation 'org.springframework.boot:spring-boot-starter-test' 10}
application.properties
1spring.datasource.url=jdbc:mysql://localhost:3306/library?serverTimezone=JST 2spring.datasource.username=user 3spring.datasource.password=password 4spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 5mybatis.configuration.map-underscore-to-camel-case=true 6spring.datasource.initialization-mode=always
Book.java
1package com.example.demo.entity; 2import java.time.LocalDate; 3import lombok.Data; 4 5@Data 6public class Book { 7 private Integer id; 8 private String bookName; 9 private Integer volumeNum; 10 private String authorName; 11 private LocalDate publishedDate; 12 13 public Book (Integer id, String bookName, Integer volumeNum, String authorName, LocalDate publishedDate) { 14 this.id = id; 15 this.bookName = bookName; 16 this.volumeNum = volumeNum; 17 this.authorName = authorName; 18 this.publishedDate = publishedDate; 19 } 20 21 public Book () { 22 23 } 24}
my.cnf
1[mysqld] 2character-set-server=utf8mb4 3collation-server=utf8mb4_general_ci 4explicit-defaults-for-timestamp=1 5general-log=1 6general-log-file=/var/log/mysql/mysqld.log 7 8[client] 9default-character-set=utf8mb4
試したこと
・こちらのサイトの17番に空のコンストラクタが存在することとあったので、Book.javaにコンストラクタを追加しましたが、エラー内容は変わりませんでした。
・下記のサイトも参考にして、実行しましたが同じエラーが表示されました。
Spring Boot + MyBatisでデータベースに接続する方法
MyBatisとは?使い方やSpringとの連携方法をサンプル付きで解説!

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