MyBatis | 動的SQL | where, trim
DBテーブル
test_table
| id | string | number |
|---|---|---|
| 1 | hoge | 100 |
| 2 | hoge | 200 |
| 3 | fuga | 200 |
| 4 | piyo | 400 |
ソースコード
sample_mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sample.mybatis">
<select id="selectTest" resultType="sample.mybatis.TestTable">
select *
from test_table
<where>
<if test="string != null">
and string = #{string}
</if>
<if test="number != null">
and number = #{number}
</if>
</where>
</select>
</mapper>
Main.java
package sample.mybatis;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream in = Main.class.getResourceAsStream("/mybatis-config.xml")) {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
try (SqlSession session = factory.openSession()) {
// パラメータを空のオブジェクトで実行
Map<String, Object> param = new HashMap<>();
selectAndPrint(session, param);
// パラメータにstringだけを指定して実行
param = new HashMap<>();
param.put("string", "hoge");
selectAndPrint(session, param);
// パラメータにstringとnumberを指定して実行
param = new HashMap<>();
param.put("string", "hoge");
param.put("number", 200);
selectAndPrint(session, param);
}
}
}
private static void selectAndPrint(SqlSession session, Map<String, Object> param) {
session
.selectList("sample.mybatis.selectTest", param)
.forEach(System.out::println);
System.out.println();
}
}
実行結果
[DEBUG] s.m.selectTest - ==> Preparing: select * from test_table
[DEBUG] s.m.selectTest - ==> Parameters:
[DEBUG] s.m.selectTest - <== Total: 4
TestTable [id=1, string=hoge, number=100]
TestTable [id=2, string=hoge, number=200]
TestTable [id=3, string=fuga, number=200]
TestTable [id=4, string=piyo, number=300]
[DEBUG] s.m.selectTest - ==> Preparing: select * from test_table WHERE string = ?
[DEBUG] s.m.selectTest - ==> Parameters: hoge(String)
[DEBUG] s.m.selectTest - <== Total: 2
TestTable [id=1, string=hoge, number=100]
TestTable [id=2, string=hoge, number=200]
[DEBUG] s.m.selectTest - ==> Preparing: select * from test_table WHERE string = ? and number = ?
[DEBUG] s.m.selectTest - ==> Parameters: hoge(String), 200(Integer)
[DEBUG] s.m.selectTest - <== Total: 1
TestTable [id=2, string=hoge, number=200]
説明
<where>タグを使用すると、子要素に何らかの文字列が存在する場合だけ、先頭にWHERE句を追加してくれる。- また、
<where>タグ内の文字列がANDまたはORで始まる場合、その条件接続語は自動的に削除される。 - この動作を
<if>などだけで書くとかなり複雑になるが、<where>タグを使うと簡単に書ける。
trimタグで代替する
上記の<where>を使用した定義は、<trim>タグを使用して次のように置き換えられる。
sample_mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sample.mybatis">
<select id="selectTest" resultType="sample.mybatis.TestTable">
select *
from test_table
<trim prefix="where" prefixOverrides="AND|OR">
<if test="string != null">
and string = #{string}
</if>
<if test="number != null">
and number = #{number}
</if>
</trim>
</select>
</mapper>
- prefix属性には前に追加する文字列を指定する。
- prefixOverrides属性には、先頭から削除する文字列をパイプ(|)区切りで指定する。