使用 Java 開發(fā)程序最痛苦的事情之一就是要處理動態(tài)生成 SQL 的問題,雖然通過 Mapper 文件可以規(guī)劃生成的 SQL語句。 但在遇到復(fù)雜情況時代碼方式依然是最佳選擇。
提示:
條件構(gòu)造器可以用于 ?query
?、?update
?、?delete
?三類操作中,下面以 ?query
?為例展示不同方法對應(yīng)的的 sql 語句。
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.eq(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age = ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.ne(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age <> ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.gt(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age > ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.ge(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age >= ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.lt(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age < ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.le(TestUser::getAge, 32)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age <= ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.like(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name LIKE CONCAT('%', ? ,'%')
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.likeLeft(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name LIKE CONCAT('%', ?)
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.likeRight(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name LIKE CONCAT(?, '%')
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.notLike(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name NOT LIKE CONCAT('%', ? ,'%')
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.notLikeLeft(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name NOT LIKE CONCAT('%', ?)
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.notLikeRight(TestUser::getAge, "001")
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name NOT LIKE CONCAT(?, '%')
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.isNull(TestUser::getAge)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age IS NULL
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.isNotNull(TestUser::getAge)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age IS NOT NULL
List<Integer> argsIn = Arrays.asList(22, 32);
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query10.in(TestUser::getAge, argsIn)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age IN ( ? , ? )
List<Integer> argsNotIn = Arrays.asList(22, 32);
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query10.notIn(TestUser::getAge, argsNotIn)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age NOT IN ( ? , ? )
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.between(TestUser::getAge, 20, 30)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age BETWEEN ? AND ?
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.notBetween(TestUser::getAge, 20, 30)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE age NOT BETWEEN ? AND ?
當(dāng)擁有多個條件進(jìn)行查詢時,就會用到關(guān)系式。
與 是默認(rèn)關(guān)系,因此當(dāng)多個條件之間都是 與 關(guān)系時,可以不指明關(guān)系式。例如:
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.eq(TestUser::getName, "123")
.eq(TestUser::getAge, 12)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name = ? AND age = ?
也可以展開寫成:
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.and().eq(TestUser::getName, "123")
.and().eq(TestUser::getAge, 12)
.queryForList();
表示多個條件之間是或關(guān)系使用如下方式:
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.eq(TestUser::getName, "123")
.or()
.eq(TestUser::getAge, 12)
.queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE name = ? OR age = ?
在一些較為復(fù)雜的條件中可以使用嵌套關(guān)系,例如:
LambdaQuery<TestUser> query = lambdaTemplate.lambdaQuery(TestUser.class);
List<TestUser> result = query.and(qc -> {
qc.like(TestUser::getName, "123").eq(TestUser::getAge, 12);
}).or(qc -> {
qc.eq(TestUser::getId, 1);
}).queryForList();
對應(yīng)的 SQL:
SELECT * FROM test_user WHERE
( name LIKE CONCAT('%', ? ,'%') AND age = ? )
OR
( id = ? )
提示:
條件嵌套可以多層
更多建議: