java – MyBatis“或”标准

前端之家收集整理的这篇文章主要介绍了java – MyBatis“或”标准前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用MyBatis创建一个查询,它将产生如下内容
SELECT first_field,second_filed,third_field
WHERE first_field > 1 AND (second_field > 0 OR third_field < 0)

我怎么能用Criteria对象构造它?

解决方法

一种方法是到 extend the generated Example classes

The generated “example” class includes a nested inner class where the actual functionality of the predicates exists. This inner class is always named GeneratedCriteria.

MBG also generates an inner class named Criteria which extends GeneratedCriteria and which you may use for adding your own functions to the example classes. The Criteria class will not be deleted by the Eclipse Java code merger,so you may add to it without fear of losing your changes upon regeneration.

基本上,生成示例类,并添加自定义条件.

public Criteria multiColumnOrClause(String value1,String value2) {
        addCriterion("value1 = " + value1 + " OR value2 = " + value2);
        return this;
    }

如果您经常重复使用它,并且不想为所有映射器执行此操作,您也可以在“插件”中提取逻辑,尽管文档有点缺乏,但只有一个示例:

org.mybatis.generator.plugins.CaseInsensitiveLikePlugin

原文链接:https://www.f2er.com/java/129801.html

猜你在找的Java相关文章