標題の件ですが、データベースとの連携がうまく行きません。
どうかお力を貸してください。
お願い致します。
以下は、gitにあるコードです。
https://github.com/microstone-info/springbootsample
▪️エラー内容
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Nov 21 13:48:59 JST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Invalid bound statement (not found): com.example.demo.mapper.ItemMapper.findAll
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.mapper.ItemMapper.findAll
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
at com.sun.proxy.$Proxy136.findAll(Unknown Source)
at com.example.demo.service.ItemService.findAll(ItemService.java:21)
at
▪️ディレクトリ構造
Application.javaのファイル
java
1package com.example.demo; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class Application { 8 9 public static void main(String[] args) { 10 SpringApplication.run(Application.class, args); 11 } 12 13}
ItemController.javaのファイル
java
1package com.example.demo.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.validation.BindingResult; 7import org.springframework.validation.annotation.Validated; 8import org.springframework.web.bind.annotation.DeleteMapping; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.ModelAttribute; 11import org.springframework.web.bind.annotation.PathVariable; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.PutMapping; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import com.example.demo.domain.Item; 17import com.example.demo.service.ItemService; 18 19@Controller 20@RequestMapping("/items") 21public class ItemController { 22 23 @Autowired 24 private ItemService itemService; 25 26 @GetMapping 27 public String index(Model model) { 28 model.addAttribute("items", itemService.findAll(model)); 29 return "index"; 30 } 31 32 @GetMapping("{id}") 33 public String show(@PathVariable Long id, Model model) { 34 model.addAttribute("item", itemService.findOne(id)); 35 return "show"; 36 } 37 38 @GetMapping("new") 39 public String newItem(@ModelAttribute("item") Item item, Model model) { 40 return "new"; 41 } 42 43 @GetMapping("{id}/edit") 44 public String edit(@PathVariable Long id, @ModelAttribute("item") Item item, Model model) { 45 model.addAttribute("item", itemService.findOne(id)); 46 return "edit"; 47 } 48 49 @PostMapping 50 public String create(@ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 51 if (result.hasErrors()) { 52 return "new"; 53 } else { 54 itemService.save(item); 55 return "redirect:/items"; 56 } 57 } 58 59 @PutMapping("{id}") 60 public String update(@PathVariable Long id, @ModelAttribute("item") @Validated Item item, BindingResult result, Model model) { 61 if (result.hasErrors()) { 62 model.addAttribute("item", item); 63 return "edit"; 64 } else { 65 item.setId(id); 66 itemService.update(item); 67 return "redirect:/items"; 68 } 69 } 70 71 @DeleteMapping("{id}") 72 public String delete(@PathVariable Long id) { 73 itemService.delete(id); 74 return "redirect:/items"; 75 } 76} 77
Item.javaのファイル
java
1package com.example.demo.domain; 2 3import javax.validation.constraints.Max; 4import javax.validation.constraints.Min; 5import javax.validation.constraints.NotBlank; 6import javax.validation.constraints.Size; 7 8public class Item { 9 private Long id; 10 11 @NotBlank(message="商品名を記入してください。") 12 private String name; 13 14 @Min(value=10, message="10以上の数値を入力してください。") 15 @Max(value=10000, message="10000以下の数値を入力してください。") 16 private float price; 17 18 @Size(max=50, message="ベーダー名は50文字を超えないでください。") 19 private String vendor; 20 21 public Long getId() { 22 return id; 23 } 24 25 public void setId(Long id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public float getPrice() { 38 return price; 39 } 40 41 public void setPrice(float price) { 42 this.price = price; 43 } 44 45 public String getVendor() { 46 return vendor; 47 } 48 49 public void setVendor(String vendor) { 50 this.vendor = vendor; 51 } 52}
ItemMappe.javaのファイル
java
1package com.example.demo.mapper; 2 3import java.util.List; 4 5import org.apache.ibatis.annotations.Mapper; 6 7import com.example.demo.domain.Item; 8 9@Mapper 10public interface ItemMapper { 11 List<Item> findAll(); 12 13 Item findOne(Long id); 14 15 void save(Item item); 16 17 void update(Item item); 18 19 void delete(Long id); 20}
ItemMappe.xmlのファイル
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3<mapper namespace="com.example.demo.mapper.ItemMapper"> 4 <select id="findAll" resultType="com.example.demo.domain.Item"> 5 select * from item 6 </select> 7 8 <select id="findOne" resultType="com.example.demo.domain.Item"> 9 select * from item where id= #{id} 10 </select> 11 12 <insert id="save" useGeneratedKeys="true" keyProperty="id"> 13 insert into item(name, price, vendor) values(#{name}, #{price}, #{vendor}) 14 15 </insert> 16 17 <update id="update"> 18 update item set name=#{name}, price=#{price}, vendor=#{vendor} where id= #{id} 19 </update> 20 21 <delete id="delete"> 22 delete from item where id = #{id} 23 </delete> 24</mapper>
Item.serviceのファイル
java
1package com.example.demo.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8import org.springframework.ui.Model; 9 10import com.example.demo.domain.Item; 11import com.example.demo.mapper.ItemMapper; 12 13@Service 14public class ItemService { 15 16 @Autowired 17 ItemMapper itemMapper; 18 19 @Transactional 20 public List<Item> findAll(Model model) { 21 return itemMapper.findAll(); 22 } 23 24 @Transactional 25 public Item findOne(Long id) { 26 return itemMapper.findOne(id); 27 } 28 29 @Transactional 30 public void save(Item item) { 31 itemMapper.save(item); 32 } 33 34 @Transactional 35 public void update(Item item) { 36 itemMapper.update(item); 37 } 38 39 @Transactional 40 public void delete(Long id) { 41 itemMapper.delete(id); 42 } 43 44}
sqeme.sqlのファイル
sql
1CREATE TABLE item ( 2 id bigint(20) NOT NULL AUTO_INCREMENT, 3 name varchar(255), 4 price real, 5 vendor varchar(255), 6 PRIMARY KEY (id), 7) ENGINE=InnoDB DEFAULT CHARSET=utf8;
pom.xmlのファイル
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.3.5.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>application</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>11</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.mybatis</groupId> 24 <artifactId>mybatis</artifactId> 25 <version>3.5.5</version> 26 </dependency> 27 <dependency> 28 <groupId>org.mybatis</groupId> 29 <artifactId>mybatis-spring</artifactId> 30 <version>2.0.2</version> 31 </dependency> 32 <dependency> 33 <groupId>org.mybatis.spring.boot</groupId> 34 <artifactId>mybatis-spring-boot-starter</artifactId> 35 <version>2.1.4</version> 36 </dependency> 37 <dependency> 38 <groupId>com.h2database</groupId> 39 <artifactId>h2</artifactId> 40 <scope>runtime</scope> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-jdbc</artifactId> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-thymeleaf</artifactId> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter-validation</artifactId> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-starter-web</artifactId> 57 </dependency> 58 <dependency> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-devtools</artifactId> 61 <scope>runtime</scope> 62 <optional>true</optional> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework.boot</groupId> 66 <artifactId>spring-boot-starter-test</artifactId> 67 <scope>test</scope> 68 </dependency> 69 </dependencies> 70 71 <build> 72 <plugins> 73 <plugin> 74 <groupId>org.springframework.boot</groupId> 75 <artifactId>spring-boot-maven-plugin</artifactId> 76 </plugin> 77 </plugins> 78 </build> 79 80</project>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/21 12:38
2020/11/21 13:13 編集
2020/11/21 14:35
2020/11/22 04:48 編集
2020/11/23 06:37
2020/11/23 06:50
2020/11/23 07:06