Mybatis做子查询,或者递归查询选中的值
Doing subqueries in Mybatis, or query recursively the selected values
更新:
我知道我的问题的解决方案是执行子查询,每次都应用不同的过滤器,并且它们有一个减少的结果集。但是我在 MyBatis 逻辑中找不到这样做的方法。这是我的查询代码
List<IstanzaMetadato> res = null;
SqlSession sqlSession = ConnectionFactory.getSqlSessionFactory().openSession(true);
try {
IstanzaMetadatoMapper mapper = sqlSession.getMapper(IstanzaMetadatoMapper.class);
IstanzaMetadatoExample example = new IstanzaMetadatoExample();
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue());
}
example.setDistinct(true);
res = mapper.selectByExample(example);
我需要执行一个新的 selectByExample 但在 while 循环内,它必须查询以前的 "SELECTED" 结果....
有解决办法吗?
原始问题:
我有这个table结构
我必须使用最终用户指定的不同过滤器 table 中的 select 行。
这些过滤器由一对 (id_metadato, valore) 指定,例如,您可以有 id_metadato = 3 和 valore = "pippo";
用户可以从网页中指定 0-n 个过滤器,在基于 id_metadato
的搜索框中键入 0-n 个值
显然,用户指定的过滤器越多,最终查询的限制就越多。
例如,如果用户只填写第一个搜索框,则查询将只有一个过滤器,并会提供所有包含用户指定的对 (id_metadato, valore) 的行。
如果他使用两个搜索框,则查询将有 2 个过滤器,并且它将在 "first subquery" 完成后提供验证第一个条件和第二个条件的所有行。
我需要以最有效的方式动态地执行此操作。我不能简单地在我的查询中添加 AND 子句,他们每次都必须过滤和减少结果集。
我不能有效地执行 0-n 子查询 (Select * from ... IN (select * from ....) )。
有没有更优雅的方法来做到这一点?我正在使用 MyBatis 阅读动态 SQL 查询教程,但我不确定这是正确的方法。还在摸索resultio的逻辑,接下来我会尝试用MyBatis来实现
感谢您的回答
MyBatis 大大简化了嵌套子查询的过程,连接过滤条件并添加
就足够了
代码摘录如下
try {
IstanzaMetadatoMapper mapper = sqlSession.getMapper(IstanzaMetadatoMapper.class);
IstanzaMetadatoExample example = new IstanzaMetadatoExample();
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
if (listaIdUd.isEmpty()) {
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue());
example.setDistinct(true);
listaIdUd = mapper.selectDynamicNested(example);
continue;
}
example.clear();
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue()).andIdUdIn(listaIdUd);
example.setDistinct(true);
listaIdUd = mapper.selectDynamicNested(example);
}
更新:
我知道我的问题的解决方案是执行子查询,每次都应用不同的过滤器,并且它们有一个减少的结果集。但是我在 MyBatis 逻辑中找不到这样做的方法。这是我的查询代码
List<IstanzaMetadato> res = null;
SqlSession sqlSession = ConnectionFactory.getSqlSessionFactory().openSession(true);
try {
IstanzaMetadatoMapper mapper = sqlSession.getMapper(IstanzaMetadatoMapper.class);
IstanzaMetadatoExample example = new IstanzaMetadatoExample();
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue());
}
example.setDistinct(true);
res = mapper.selectByExample(example);
我需要执行一个新的 selectByExample 但在 while 循环内,它必须查询以前的 "SELECTED" 结果....
有解决办法吗?
原始问题:
我有这个table结构
我必须使用最终用户指定的不同过滤器 table 中的 select 行。 这些过滤器由一对 (id_metadato, valore) 指定,例如,您可以有 id_metadato = 3 和 valore = "pippo";
用户可以从网页中指定 0-n 个过滤器,在基于 id_metadato
的搜索框中键入 0-n 个值显然,用户指定的过滤器越多,最终查询的限制就越多。
例如,如果用户只填写第一个搜索框,则查询将只有一个过滤器,并会提供所有包含用户指定的对 (id_metadato, valore) 的行。 如果他使用两个搜索框,则查询将有 2 个过滤器,并且它将在 "first subquery" 完成后提供验证第一个条件和第二个条件的所有行。
我需要以最有效的方式动态地执行此操作。我不能简单地在我的查询中添加 AND 子句,他们每次都必须过滤和减少结果集。
我不能有效地执行 0-n 子查询 (Select * from ... IN (select * from ....) )。
有没有更优雅的方法来做到这一点?我正在使用 MyBatis 阅读动态 SQL 查询教程,但我不确定这是正确的方法。还在摸索resultio的逻辑,接下来我会尝试用MyBatis来实现
感谢您的回答
MyBatis 大大简化了嵌套子查询的过程,连接过滤条件并添加
就足够了代码摘录如下
try {
IstanzaMetadatoMapper mapper = sqlSession.getMapper(IstanzaMetadatoMapper.class);
IstanzaMetadatoExample example = new IstanzaMetadatoExample();
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
if (listaIdUd.isEmpty()) {
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue());
example.setDistinct(true);
listaIdUd = mapper.selectDynamicNested(example);
continue;
}
example.clear();
example.createCriteria().andIdMetadatoEqualTo(entry.getKey()).andValoreEqualTo(entry.getValue()).andIdUdIn(listaIdUd);
example.setDistinct(true);
listaIdUd = mapper.selectDynamicNested(example);
}