Mybatisで小なりを実装する方法

Mybatisで小なりを実装する方法

MyBatisで小なりを実装する方法

MyBatisで小なり(<)を使った条件を実装する方法は、基本的にifタグを使用します。
ここでは、testTableというテーブルに対して、小なりの条件を使ってデータを取得する例を示します。

1. テーブル構造
CREATE TABLE testTable (
    id INT PRIMARY KEY,
    value INT
);
2. MyBatis Mapperインターフェース
public interface testTableMapper {
    List<testTable> selectByValueLessThan(int value);
}
3. MyBatis XMLマッピングファイル
<mapper namespace="com.example.testTableMapper">
    <select id="selectByValueLessThan" resultType="testTable">
        SELECT * FROM testTable
        WHERE value &lt; #{value}
    </select>
</mapper>
4. サービスクラスでの使用例
@Service
public class testTableService {
    @Autowired
    private testTableMapper testTableMapper;

    public List<testTable> getValuesLessThan(int value) {
        return testTableMapper.selectByValueLessThan(value);
    }
}

この例では、valueカラムが指定した値より小さいレコードを取得しています。
MyBatisのselectタグの中で、valueが小なりの条件で指定されています。