使用规则处理复杂条件
· One min read
AND/OR 规则进阶
因此规则还可稍微复杂一点
@{and, (age = :age and sex = '1') or (name = :name and id in (:ids)) }
对应的 SQL 为:
(age = ? and sex = '1') or (name = ? and id in (?, ?, ?))
因此规则还可稍微复杂一点
@{and, (age = :age and sex = '1') or (name = :name and id in (:ids)) }
对应的 SQL 为:
(age = ? and sex = '1') or (name = ? and id in (?, ?, ?))
当查询一张超大表并获取它的结果集时要使用 流式返回 否则内存极易出现溢出。
不同的数据库开启流式返回的方式虽有差异,但都需要设置 Statement/PreparedStatement 的参数。
下面就以 MySQL 为例展示一下通过定制 Statement 实现流式查询的例子:
// 定制 PreparedStatement
PreparedStatementCreator creator = con -> {
PreparedStatement ps = con.prepareStatement(
"select * from test_user",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY
);
ps.setFetchSize(Integer.MIN_VALUE);
return ps;
};
// 行读取工具
MappingRowMapper<TestUser> rowMapper = new MappingRowMapper<>(TestUser.class);
// 流式消费数据
RowCallbackHandler handler = (rs, rowNum) -> {
TestUser dto = rowMapper.mapRow(rs, rowNum);
};
// 执行流式处理
jdbcTemplate.executeCreator(creator, handler);