where条件设置语法形式如下, 以where开头,以end()方法结束 在内置变量where后面,可以自动列出可以设置的表字段(方法), 在字段后可以紧跟着比较符和比较值。
.where
.字段().条件(条件参数)
.end()
@Test
public void column_eq_value() {
UserQuery query = new UserQuery()
.where.userName().eq("darui.wu")
.and.eMail().eq("darui.wu@163.com").end();
mapper.findOne(query);
db.sqlList().wantFirstSql().eq(
"SELECT id, gmt_create, gmt_modified, is_deleted, account, avatar, birthday, bonus_points, e_mail, password, phone, status, user_name " +
"FROM user " +
"WHERE user_name = ? " +
"AND e_mail = ?",
StringMode.SameAsSpace);
}
当预言when为真时, 才会执行 eq(value)判断
示例:
/**
* 根据条件查询用户列表
* 如果设置了积分, 加上积分条件
* 如果设置了状态, 加上状态条件
*
* @return
*/
@Override
public List<UserEntity> findByBirthdayAndBonusPoints(Date birthday, Long points, String status) {
UserQuery query = super.query()
.where.birthday().eq(birthday)
.and.bonusPoints().ge(points, If::notNull)
.and.status().eq(status, If::notBlank).end();
return mapper.listEntity(query);
}
上面的示例,相当于下面常见的java条件设置
public List<UserEntity> findByBirthdayAndBonusPoints(Date birthday, Long points, String status) {
UserQuery query = super.query();
query.where.birthday().eq(birthday);
if (points != null) {
query.where.bonusPoints().ge(points);
}
if (status != null && !status.trim().isEmpty()) {
query.where.status().eq(status).end();
}
return mapper.listEntity(query);
}
或者,使用mybatis的条件设置
<select id="findByBirthdayAndBonusPoints" parameterType="java.util.Map" resultMap="UserEntity">
SELECT ... FROM user
WHERE birthday = #{birthday}
<if test="bonus_points != null ">
AND bonus_points = #{bonusPoints}
</if>
<if test="status != null and status != '' ">
AND status = #{status}
</if>
</select>
使用fluent mybatis的条件语句可以达到上面java判断和xml判断的同样效果,同时,如果birthday如果传入一个空串,还会进行判空,抛出异常, 防止传参错误,导致的非预期条件查询。
@Test
public void ne() {
UserQuery query = new UserQuery()
.where
.age().ne(34)
.end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age <> ?");
}
@Test
public void gt() {
UserQuery query = new UserQuery()
.where.age().gt(34).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age > ?");
}
@Test
public void ge() {
UserQuery query = new UserQuery()
.where.age().ge(34).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age >= ?");
}
@Test
public void lt() {
UserQuery query = new UserQuery()
.where.age().lt(34).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age < ?");
}
@Test
public void le() {
UserQuery query = new UserQuery()
.where.age().le(34).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age <= ?");
}
@Test
public void between() {
UserQuery query = new UserQuery()
.where.age().between(23, 40).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age BETWEEN ? AND ?");
}
@Test
public void not_between() {
UserQuery query = new UserQuery()
.where.age().notBetween(23, 40).end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age NOT BETWEEN ? AND ?");
}
@Test
void like() {
mapper.listEntity(new UserQuery()
.where
.userName().like("abc")
.end()
);
// ? 值为: '%abc%'
db.sqlList().wantFirstSql().end("WHERE user_name LIKE ?");
}
@Test
void not_like() {
mapper.listEntity(new UserQuery()
.where
.userName().notLike("abc")
.end()
);
// ? 值为: '%abc%'
db.sqlList().wantFirstSql().end("WHERE user_name NOT LIKE ?");
}
@Test
void isNull() {
mapper.listEntity(new UserQuery()
.where.age().isNull().end()
);
db.sqlList().wantFirstSql().end("WHERE age IS NULL");
}
@Test
void isNotNull() {
mapper.listEntity(new UserQuery()
.where.age().isNotNull().end()
);
db.sqlList().wantFirstSql()
.end("WHERE age IS NOT NULL");
}
@Test
public void in_collection() {
UserQuery query = new UserQuery()
.where
.age().in(Arrays.asList(34, 35))
.end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age IN (?, ?)");
db.sqlList().wantFirstPara().eqReflect(new Object[]{34, 35});
}
@Test
public void in_array() {
UserQuery query = new UserQuery()
.where
.age().in(new int[]{34, 35})
.end();
mapper.count(query);
db.sqlList().wantFirstSql().end("WHERE age IN (?, ?)");
db.sqlList().wantFirstPara().eqReflect(new Object[]{34, 35});
}
以上条件,框架会自动判断参数不允许为空,否则会抛出异常,防止全表扫描
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )