Más contenido relacionado
La actualidad más candente (20)
Similar a Spring bootでweb 基本編 (20)
Spring bootでweb 基本編
- 7. Repositoryの実装
ただのクラスをEntityに仕立てる
/** productテーブルのEntity */
@Entity
@Table(name="product")
public class Product {
/** 商品コード */
@Id
public Integer productCode;
/** 商品名 */
public String productName;
/** 価格 */
public Integer price;
}
クラス宣言に@Entityを付けて、
Entityであることを明記
「product」テーブルの
Entityであることを明記
それぞれのカラムに対応した
フィールドを宣言
フィールド宣言に「@Id」を付けて、
このフィールドがPKであることを明記
※便宜上、フィールドをpublic宣言している。
本来、フィールドはprivateであるべき。
- 8. Repositoryの実装
Productテーブルとサンプルデータ
-- DDL
create table product (
product_code int(5),
product_name varchar(50),
price int(10),
primary key ( product_code )
);
--DML
insert into product values( 1, 'ガム', 100 );
insert into product values( 2, 'アメ', 120 );
insert into product values( 3, 'チョコ', 150 );
- 9. Repositoryの実装
ただのクラスをRepositoryに仕立てる
/** productテーブルのRepository */
public interface ProductRepository
extends JpaRepository <Product, Integer> {
}
JpaRepositoryを拡張した
インターフェースであることを明記 「product」テーブルの
Repositoryであることを明記
PKはInteger(数値フィールド)
であることを明記
JpaRepositoryには基本的なデータベース操作は宣言されていて、
カスタマイズの必要がなければ特に中身のないインターフェース宣言で良い(そんなケースは稀な気が・・・)
- 13. Serviceの実装
Repositoryを呼び出す
/** 商品関連のビジネスロジックを実行するService */
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
/** 商品一覧を返す */
public List<Product> getProductList() {
List<Product> list = productRepository.findAll();
return list;
}
}
使用するRepositoryを宣言
Repositoryのメソッドを
呼び出してデータを取得
取得したデータを返却
- 17. Controllerの実装
リクエストとメソッドを関連付ける
/** 商品関連のページを制御するController */
@Controller
@RequestMapping("product")
public class ProductController {
/** 商品一覧を表示する */
@RequestMapping(value = "/", method=RequestMethod.GET)
public String list() {
return "product/list“;
}
}
value値でURL「product」の
ルート(/) Controllerとして関連付ける
method値でHTTPメソッド「GET」に
関連付ける
テンプレート「product/list.html」を
表示する
- 18. Controllerの実装
Serviceを呼び出す
/** 商品関連のページを制御するController */
@Controller
@RequestMapping("product")
public class ProductController {
@Autowired
private ProductService productService;
/** 商品一覧を表示する */
@RequestMapping(value = "/", method=RequestMethod.GET)
public String list() {
return "product/list";
}
}
使用するServiceを宣言
- 19. Controllerの実装
Serviceを呼び出す
/** 商品関連のページを制御するController */
@Controller
@RequestMapping("product")
public class ProductController {
@Autowired
private ProductService productService;
/** 商品一覧を表示する */
@RequestMapping(value = "/", method=RequestMethod.GET)
public String list(Model model) {
List<Product> list = productService.getProductList();
model.addAttribute("productList", list);
return "product/list";
}
}
引数にModelを受け取る
(ControllerとTemplateの架け橋)
Serviceを呼び出す
Serviceの実行結果を
Modelに詰める
- 20. Controllerの実装
TemplateのHTMLを記述する
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>商品一覧</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
商品一覧
</body>
</html>
テンプレートエンジン「Thymeleaf」で
使用するネームスペースの宣言
src¥main¥resources¥templates¥product¥list.html
- 23. Controllerの実装
動的部分を作成する
<body>
商品一覧
<table border="1">
<tr>
<th style="background-color:#CCCCCC">Code</th>
<th style="background-color:#CCCCCC">商品名</th>
<th style="background-color:#CCCCCC">金額</th>
</tr>
<tr th:each="product: ${productList}">
<td th:text="${product.productCode}">1</td>
<td th:text="${product.productName}">ダミー商品</td>
<td th:text="${product.price}">50</td>
</tr>
</table>
</body>
Modelに詰めたproductListを取り出し、
for-eachでループさせる
Thymeleafで動的部分を実装する (エンジニアさんの仕事)
必要なEntityの要素を出力する