Skip to content

Commit

Permalink
Merge pull request #23 from igakilab/sample4-4
Browse files Browse the repository at this point in the history
sample4-4
  • Loading branch information
igaki committed Feb 25, 2020
2 parents f2de96d + e3b751c commit c9914b2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Readme.md
Expand Up @@ -316,6 +316,7 @@ false
- FruitsMapper.java
- INSERT文
- Sample4Controller.java
- sample43.html

### 関連する機能
- @GetMapping
Expand Down Expand Up @@ -354,10 +355,32 @@ spring.datasource.username=sa
spring.datasource.password=
```
## [Sample4-4] DBから複数の値をArrayListで取得し,htmlで表示するサンプル
### 参考
- https://qiita.com/NagaokaKenichi/items/c6d1b76090ef5ef39482#%E7%B9%B0%E3%82%8A%E8%BF%94%E3%81%97%E3%83%AB%E3%83%BC%E3%83%97
- タイムリーフにおける繰り返し処理やステータス変数(stat)について
### 関連するファイル
- 実装:
- Sample4Controller.java
- FruitsMapper.java
- sample44.html
### 関連する機能
- @Select
- @GetMapping
- Mapperを利用したFruitsオブジェクトのArrayList取得

### 動作確認
- http://localhost:8000/sample43 にアクセスし,果物を追加で登録する
- http://localhost:8000/sample44 にアクセスし,下記のようにブラウザにFruitsのリストが表示されればOK
```
index:0 id:1 さがほのか 10 5.8 いちご false
index:1 id:2 レモン 100 0.0 false
```
- 最初のindexはステータス変数(stat.index)の値.他のステータス変数については参考資料参照.

# Samples
### 参考

- https://qiita.com/NagaokaKenichi/items/c6d1b76090ef5ef39482#%E7%B9%B0%E3%82%8A%E8%BF%94%E3%81%97%E3%83%AB%E3%83%BC%E3%83%97
- タイムリーフにおける繰り返し処理やステータス変数(stat)について
### 関連するファイル

### 関連する機能
Expand Down
@@ -1,5 +1,7 @@
package jp.ac.igakilab.springbootsamples.controller;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
Expand Down Expand Up @@ -77,4 +79,18 @@ public String sample43PostFruits(@ModelAttribute("fruit") Fruits fruit) {

return "sample43.html";
}

/**
*
* @param model htmlで表示したい果物のリストを格納するmapオブジェクト
* @return HTMLのviewを返す
*/
@GetMapping("sample44")
public String sample44GetFruitsList(ModelMap model) {
// すべての果物のArrayListを取得し,modelに格納する
ArrayList<Fruits> fruitsList = this.fruitsMapper.selectAllFruits();
model.addAttribute("fruits", fruitsList);
return "sample44.html";

}
}
@@ -1,5 +1,7 @@
package jp.ac.igakilab.springbootsamples.model;

import java.util.ArrayList;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
Expand Down Expand Up @@ -30,4 +32,12 @@ public interface FruitsMapper {
@Insert("INSERT INTO fruits (name,num,weight,details,sent) VALUES (#{name}, #{num}, #{weight}, #{details}, #{sent})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Fruits fruit);

/**
* fruitsテーブルにある全ての果物のリストを返す.テーブルのフィールドとFruitsクラスのフィールドが同一であれば,個別にフィールド名を指定しなくとも自動的に変換してくれる
*
* @return 複数のFruitsオブジェクトが格納されたArrayList
*/
@Select("SELECT * FROM fruits")
ArrayList<Fruits> selectAllFruits();
}
27 changes: 27 additions & 0 deletions src/main/resources/templates/sample44.html
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="utf-8">
<title>Fruits</title>
</head>

<body>
<h1>Hello Fruits</h1>
<!--親となるタグでobject名を宣言すると,そのオブジェクト内のフィールドを子となるタグでは下記フォーマットで参照できるようになる-->
<!-- この場合,fruitsからfruit1つを取り出し,それをth:objectで指定している-->
<!--statはステータス変数-->
<div th:each="fruit,stat : ${fruits}" th:object="${fruit}">
<tr>
<td th:text="'index:' + ${stat.index}"></td>
<td th:text="'id:'+ *{id}"></td>
<td th:text="*{name}"></td>
<td th:text="*{num}"></td>
<td th:text="*{weight}"></td>
<td th:text="*{details}"></td>
<td th:text="*{sent}"></td>
</tr>
</div>
</body>

</html>

0 comments on commit c9914b2

Please sign in to comment.