前提・実現したいこと
画面に入力した従業員IDを使ってDBに検索を行い、その検索結果を画面に出力するというサンプルプログラムです。
実行時に下記のエラーが出てしまいます。
UnsatisfiedDependencyExceptionというエラー文から推測するに、DIの注入?(まだ初心者なのでほとんどわかりません)が
できていないということなのでしょうか?
ご回答よろしくお願いします。
コンソールログ
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-07-10 22:49:57.872 ERROR 16628 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'helloService'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'helloService': Unsatisfied dependency expressed through field 'helloRepository'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'helloRepository': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration$JdbcTemplateConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'class path resource [schema.sql]' is invalid: The specified resource does not exist.
ソースコード
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringSample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringSample</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
SpringSampleApplication
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringSampleApplication { public static void main(String[] args) { SpringApplication.run(SpringSampleApplication.class, args); } }
HelloService
package com.example.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class HelloService { @Autowired private HelloRepository helloRepository; public Employee findOne(int id) { Map<String, Object>map = helloRepository.findOne(id); int employeeId = (Integer)map.get("employee_id"); String employeeName = (String)map.get("employee_name"); int age = (Integer)map.get("age"); Employee employee = new Employee(); employee.setEmployeeId(employeeId); employee.setEmployeeName(employeeName); employee.setAge(age); return employee; } }
HelloRepository
package com.example.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class HelloRepository { @Autowired private JdbcTemplate jdbcTemplate; public Map<String, Object>findOne(int id){ String query = "SELECT " + "Employee_id " + "employee_name " + "age " + "FROM employee " + "WHERE employee_id=?"; Map<String, Object> employee = jdbcTemplate.queryForMap(query, id); return employee; } }
HelloController
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @Autowired private HelloService helloService; @GetMapping("/hello") private String getHello(){ return "hello"; } @PostMapping("/hello") public String postRequest(@RequestParam("text1")String str, Model model ) { model.addAttribute("sample", str); return "helloResponse"; } @PostMapping("/hello/db") public String postDbRequest(@RequestParam("text2")String str, Model model) { int id = Integer.parseInt(str); Employee employee = helloService.findOne(id); model.addAttribute("id", employee.getEmployeeId()); model.addAttribute("name", employee.getEmployeeName()); model.addAttribute("age", employee.getAge()); return "helloResponseDB"; } }
Employee
package com.example.demo; import lombok.Data; @Data public class Employee { private int employeeId; private String employeeName; private int age; }
データベース
schma
CREATE TABLE IF NOT EXISTS employee( employee_id INT PRIMARY KEY, employee_name VARCHAR(50), age INT );
data
INSERT INTO employee(employee_id, employee_name, age) VALUES (1, '山田太郎', 30);
補足情報(FW/ツールのバージョンなど)
Eclipse Photon(4.8.0)
STS 3.9.7
Maven 3.1.1
application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.driver-class-name=org.h2.Driver spring.datasouce.username=sa spring.datasouce.password= spring.datasource.sql-script-encoding=UTF-8 spring.h2.console.enabled=true spring.datasource.initialize=true spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql
まだ回答がついていません
会員登録して回答してみよう