当尝试在 myBatis 中使用单个参数从视图中检索信息时,结果为空。使用静态查询时,它有效
When trying to retrieve information from a View using a single parameter in myBatis, the result is null. When using static query, it works
我有一个代码应该从 Oracle 数据库的视图中检索一些数据(使用 mybatis)。
这是一个接收一个参数的动态查询
愚蠢的代码示例:
//Repo Class
@Select("select surname, name from vw_example where name=#{name}")
@Results({
@Result(property = "surname", column = "surname"),
@Result(property = "name", column = "name"),
})
public List<Result> getSurname(@Param("name") String id)
//Test Class
@Autowired RepoClass repo;
@Test
public void testGetSurname(){
List<Result> result = repo.getSurname("Danilo");
AssertThat(result.size(),is(2));
}
重要的事情(在我正在处理的数据库中,name 列是一个 CHAR[40](请不要问为什么.. 但我无法更改它)。
考虑到数据库中有 2 条记录包含 "Danilo" 作为列 "Name" 的值,它应该 return 2 行对我和映射到对象 "Result".
然而,这并没有发生。 Assert 带有 actual: 0
现在,疯狂的事情...
如果我改变这个:
@Select("select surname, name from vw_example where name=#{name}")
对此:
@Select("select surname, name from vw_example where name='Danilo'")
它很有魅力!!
信息:
(我已经尝试传递 jdbcType (#{name,jdbcType=CHAR))
(好像参数被替换了,因为我用参数值替换了Insert的Select语句,没问题)
有人可以帮忙解决这个疯狂的事情吗?
谢谢
由于列被声明为CHAR(40)
,数据库中的数据被空格填充。
选项 1:在 Java 代码中添加空格。
List<Result> result = repo.getSurname(String.format("%-40s", "Danilo"));
选项 2:在查询中转换参数。
select surname, name from vw_example where name = cast(#{name} as char(40))
查询的文字版本有效是因为使用了 blank-padded comparison semantics,我猜。
With blank-padded semantics, if the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal. (snip) If two values have no differing characters, then they are considered equal. This rule means that two values are equal if they differ only in the number of trailing blanks. Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of datatype CHAR, NCHAR, text literals, or values returned by the USER function.
我有一个代码应该从 Oracle 数据库的视图中检索一些数据(使用 mybatis)。
这是一个接收一个参数的动态查询
愚蠢的代码示例:
//Repo Class
@Select("select surname, name from vw_example where name=#{name}")
@Results({
@Result(property = "surname", column = "surname"),
@Result(property = "name", column = "name"),
})
public List<Result> getSurname(@Param("name") String id)
//Test Class
@Autowired RepoClass repo;
@Test
public void testGetSurname(){
List<Result> result = repo.getSurname("Danilo");
AssertThat(result.size(),is(2));
}
重要的事情(在我正在处理的数据库中,name 列是一个 CHAR[40](请不要问为什么.. 但我无法更改它)。
考虑到数据库中有 2 条记录包含 "Danilo" 作为列 "Name" 的值,它应该 return 2 行对我和映射到对象 "Result".
然而,这并没有发生。 Assert 带有 actual: 0
现在,疯狂的事情...
如果我改变这个:
@Select("select surname, name from vw_example where name=#{name}")
对此:
@Select("select surname, name from vw_example where name='Danilo'")
它很有魅力!!
信息: (我已经尝试传递 jdbcType (#{name,jdbcType=CHAR))
(好像参数被替换了,因为我用参数值替换了Insert的Select语句,没问题)
有人可以帮忙解决这个疯狂的事情吗? 谢谢
由于列被声明为CHAR(40)
,数据库中的数据被空格填充。
选项 1:在 Java 代码中添加空格。
List<Result> result = repo.getSurname(String.format("%-40s", "Danilo"));
选项 2:在查询中转换参数。
select surname, name from vw_example where name = cast(#{name} as char(40))
查询的文字版本有效是因为使用了 blank-padded comparison semantics,我猜。
With blank-padded semantics, if the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal. (snip) If two values have no differing characters, then they are considered equal. This rule means that two values are equal if they differ only in the number of trailing blanks. Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of datatype CHAR, NCHAR, text literals, or values returned by the USER function.