MyBatis | Dynamic SQL | where, trim
DB table
test_table
| id | string | number |
|---|---|---|
| 1 | hoge | 100 |
| 2 | hoge | 200 |
| 3 | fuga | 200 |
| 4 | piyo | 400 |
Source code
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()) {
// Run with an empty parameter object.
Map<String, Object> param = new HashMap<>();
selectAndPrint(session, param);
// Run with only string specified as a parameter.
param = new HashMap<>();
param.put("string", "hoge");
selectAndPrint(session, param);
// Run with both string and number specified as parameters.
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();
}
}
Execution result
[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]
Explanation
- When using the
<where>tag, a WHERE clause is added at the beginning only if one of the child elements produces text. - Also, if the text inside the
<where>tag starts with AND or OR, that condition connector is removed automatically. - Writing this behavior with only tags such as
<if>would become fairly complex, but the<where>tag makes it simple.
Replacing it with the trim tag
The definition that uses <where> above can be rewritten with the <trim> tag as follows.
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>
- Add the string to prepend in the prefix attribute.
- In the prefixOverrides attribute, specify strings to remove from the beginning, separated by pipes (|).