m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

doma2で抽出件数を取得

マニュアルみたけど

CollectとかStreamの使い方がよく分からない(´・ω・`)
コード読んで理解しろってことかな…

DDL

drop table if exists item;
create table if not exists item (
	id        bigint       not null auto_increment,
	name      varchar(255) not null,
	price     int          not null,
	create_at datetime(3)  not null,
	update_at datetime(3)  not null,
	primary key(id)
);

insert into item (name, price, create_at, update_at) values ('test1', 100, current_timestamp(3), current_timestamp(3));
insert into item (name, price, create_at, update_at) values ('test2', 200, current_timestamp(3), current_timestamp(3));
insert into item (name, price, create_at, update_at) values ('test3', 400, current_timestamp(3), current_timestamp(3));
insert into item (name, price, create_at, update_at) values ('test4', 1050, current_timestamp(3), current_timestamp(3));
insert into item (name, price, create_at, update_at) values ('test5', 150, current_timestamp(3), current_timestamp(3));
insert into item (name, price, create_at, update_at) values ('test6', 300, current_timestamp(3), current_timestamp(3));

件数を取得する方法

Daoメソッド定義
@Select
Long findByAllCount();
SQL
select
	count(*)
from
	item
使用例
Long count = dao.findByAllCount();

実行ログ

12 31, 2016 11:19:36 午後 com.example.dao.ItemDaoImpl findByAllCount
情報: [DOMA2220] ENTER  : クラス=[com.example.dao.ItemDaoImpl], メソッド=[findByAllCount]
12 31, 2016 11:19:36 午後 com.example.dao.ItemDaoImpl findByAllCount
情報: [DOMA2076] SQLログ : SQLファイル=[META-INF/com/example/dao/ItemDao/findByAllCount.sql],
select
	count(*)
from
	item
12 31, 2016 11:19:36 午後 com.example.dao.ItemDaoImpl findByAllCount
情報: [DOMA2221] EXIT   : クラス=[com.example.dao.ItemDaoImpl], メソッド=[findByAllCount]
6


件数を取得する系などは戻り値を数値型にしてSQLをcountを指定するだけで取れる感じです。

今回はここまで