Fluent Mybatis 中如何使用括号改变 WHERE 条件优先级
在 Fluent Mybatis 中,可以使用`where()`方法来添加where条件,可以通过`and`、`or`等方法来组合多个条件。如果需要改变条件的优先级,可以使用`where().andNested()`或`where().orNested()`方法来添加括号。\n\n下面是一个示例:\n\njava\nimport cn.org.atool.fluent.mybatis.If;\nimport static cn.org.atool.fluent.mybatis.mapper.FluentConst.*;\n\npublic class UserMapper extends FluentMapper<UserEntity> {\n public static final UserMapper Mapper = new UserMapper();\n\n public UserMapper() {\n this.select("id", "username", "age")\n .from(userTable)\n .where()\n .and(UserEntity::getAge, If::gt, 18)\n .andNested(nested -> nested\n .or(UserEntity::getUsername, If::like, "%admin%")\n .or(UserEntity::getUsername, If::like, "%user%")\n )\n .end()\n .end()\n .orderBy.asc(UserEntity::getAge)\n .limit(10)\n .end();\n }\n}\n\n\n在上述示例中,使用`andNested()`方法将两个`or`条件括起来,改变了它们的优先级。
原文地址: https://www.cveoy.top/t/topic/p9Rz 著作权归作者所有。请勿转载和采集!