talosのプログラミング教室

【MyBatis】自動生成した~Exampleクラスの使い方

スポンサーリンク

こんにちは。talosです。

MyBatis Generatorを使うと~Exampleクラスが生成されるのですが、それの使い方に関する資料があまりないので説明したいと思います。

前提

・MyBatis Generatorを使って自動生成済み
・SqlSessionFactory等のMyBatisを使う準備ができている

使い所

自動生成を行うとテーブル一つに対し以下のような抽象メソッドが作られます。

long countByExample(UserSampleExample example);

int deleteByExample(UserSampleExample example);

int deleteByPrimaryKey(String id);

int insert(UserSample record);

int insertSelective(UserSample record);

List<UserSample> selectByExample(UserSampleExample example);

UserSample selectByPrimaryKey(String id);

int updateByExampleSelective(@Param("record") UserSample record, @Param("example") UserSampleExample example);

int updateByExample(@Param("record") UserSample record, @Param("example") UserSampleExample example);

int updateByPrimaryKeySelective(UserSample record);

int updateByPrimaryKey(UserSample record);

この中で~Exampleクラスを使うのはexampleを引数にとるメソッドだけです。

// ①
long countByExample(UserSampleExample example);

// ②
int deleteByExample(UserSampleExample example);

// ③
List<UserSample> selectByExample(UserSampleExample example);

// ④
int updateByExampleSelective(@Param("record") UserSample record, @Param("example") UserSampleExample example);

// ⑤
int updateByExample(@Param("record") UserSample record, @Param("example") UserSampleExample example);

名前からわかる通り上から三つはそれぞれ、①該当レコードをカウントする、②該当レコードを削除する、③該当レコードを選択するSQLを実行するメソッドです。

下二つはどちらも該当レコードを更新するSQLを実行するメソッドです。

④は引数のrecordにnullの項目がある場合はその項目の更新をスキップし、値を持っている項目だけ更新します。

一方で、⑤はすべての項目を更新します。(nullがある場合はnullで更新します。NOT NULL制約がある場合は例外が起こります)


スポンサーリンク



使い方

public List<UserSample> getAllUsers() {
	List<UserSample> list = new ArrayList<>();
	try (SqlSession session = sqlSessionManager.getSqlSessionFactory().openSession()) {
		UserSampleMapper mapper = session.getMapper(UserSampleMapper.class);
		UserSampleExample example = new UserSampleExample();
		example.createCriteria().andIdIsNotNull();
		list = mapper.selectByExample(example);
	}
	return list;
}

~Exampleクラスのインスタンスを生成します。

そして、createCriteria()を呼び、続けてWHERE句を付加するメソッドを呼びます。

WHERE句を二つ以上使いたい場合は、以下のように後ろに繋げます。

example.createCriteria().andIdIsNotNull().andNameEqualTo("Tanaka");

WHERE句を付加するメソッドにはこれらがあります。

and~Between(value1, value2)
and~NotBetween(value1, value2)
and~EqualTo(value)
and~NotEqualTo(value)
and~GreaterThan(value)
and~GreaterThanOrEqualTo(value)
and~LessThan(value)
and~LessThanOrEqualTo(value)
and~In(values)
and~NotIn(values)
and~IsNull()
and~IsNotNull()
and~Like(value)
and~NotLike(value)

and~Between(value1, value2) / and~NotBetween(value1, value2)

SQLのWHERE ~ BETWEEN value1 AND value2 / WHERE ~ NOT BETWEEN value1 AND value2に相当します。

and~EqualTo(value) / and~NotEqualTo(value)

SQLのWHERE ~ = value / WHERE ~ != value(またはWHERE ~ <> value)に相当します。

and~GreaterThan(value) / and~GreaterThanOrEqualTo(value)

SQLのWHERE ~ > value / WHERE ~ >= valueに相当します。

and~LessThan(value) / and~LessThanOrEqualTo(value)

SQLのWHERE ~ < value / WHERE ~ <= valueに相当します。

and~In(values) / and~NotIn(values)

valuesはList型で、value1, value2,...を持ちます。

SQLのWHERE ~ IN (value1, value2,...) / WHERE ~ NOT IN (value1, value2,...)に相当します。

and~IsNull() / and~IsNotNull()

SQLのWHERE ~ IS NULL / WHERE ~ IS NOT NULLに相当します。

and~Like(value) / and~NotLike(value)

SQLのWHERE ~ LIKE value / WHERE ~ NOT LIKE valueに相当します。

おわりに

今回はMyBatis Generatorで自動生成した~Exampleクラスの使い方を説明しました。

不明点があればコメントお願いします。