我有一个 Java SE 8 Spring 4.1.6-RELEASE 应用程序,我正在其中实现 org.springframework.jdbc.core.RowMapper<T> 界面,我对 java.sql.ResultSet 有一些疑问在其 T mapRow(ResultSet rs, int rowNum) 中传递的接口(interface)方法。
当我检查 ResultSet类,我看到了一堆方法来取回列值:
╔══════════════╦═════════════════════╦════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ ║ Return Type ║ Method ║ Return (javadoc, se 8) ║ ╠══════════════╬═════════════════════╬════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣ ║ String ║ getString ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ boolean ║ getBoolean ║ the column value; if the value is SQL NULL, the value returned is false ║ ║ byte ║ getByte ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ short ║ getShort ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ int ║ getInt ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ long ║ getLong ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ float ║ getFloat ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ double ║ getDouble ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ BigDecimal ║ getBigDecimal ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ byte[] ║ getBytes ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Date ║ getDate ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Time ║ getTime ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Timestamp ║ getTimestamp ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ InputStream ║ getAsciiStream ║ a Java input stream that delivers the database column value as a stream of one-byte ASCII characters; if the value is SQL NULL, the value returned is null ║ ║ Reader ║ getCharacterStream ║ a java.io.Reader object that contains the column value; if the value is SQL NULL, the value returned is null in the Java programming language ║ ║ InputStream ║ getBinaryStream ║ a Java input stream that delivers the database column value as a stream of uninterpreted bytes; if the value is SQL NULL, the value returned is null ║ ║ <T> T ║ getObject ║ an instance of type holding the column value ║ ╚══════════════╩═════════════════════╩════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
Is the genernal expectation / practice to call:
rs.getObject("COLUMN_NAME", Boolean.class);
rs.getObject("COLUMN_NAME", Byte.class);
rs.getObject("COLUMN_NAME", Short.class);
rs.getObject("COLUMN_NAME", Integer.class);
rs.getObject("COLUMN_NAME", Long.class);
等等,对于所有的原始类型?由于其他一切都返回 null例如 SQL NULL .
如果是这样,当类型化对象方法存在时,拥有不同类型的所有方法有什么意义?
此外,每种方法的优缺点是什么?
使用 getInt(String columnLabel) :
Integer resultingActionId = rs.getInt("RESULTING_ACTION_ID");
if (rs.wasNull) {
resultingActionId = null
} 使用 getObject(String columnLabel)并转换到Integer :
Integer resultingActionId = (Integer) rs.getObject("RESULTING_ACTION_ID"); 使用 getObject(String columnLabel, Class type) :
Integer resultingActionId = rs.getObject("RESULTING_ACTION_ID", Integer.class); 例如,我注意到 org.springframework.jdbc.core.JdbcTemplate 曾经有 , queryForLong 等方法,用于从单行查询中获取单个值并将它们全部替换为有利于类型化的 queryForInt queryForObject方法。
谢谢!
最佳答案
如果你看一下java.sql.ResultSet ,你可以看到你不需要那么明确。实际上,除非你有一个允许你使用 getObject 方法的连接类型映射器,否则它不会工作(java.sql.ResultSet.getObject)。
我不知道它是否对你有帮助,但我设法找到了一个我自己的 RowMapper,它非常适合我的需要。
private class ShabaUserMapper implements RowMapper<ShabaUser>
{
@Override
public ShabaUser mapRow( ResultSet rs, int rowNum ) throws SQLException
{
Collection<SimpleGrantedAuthority> roles = new ArrayList<SimpleGrantedAuthority>();
String auths = rs.getString( "role" );
roles.add( new SimpleGrantedAuthority( auths ) );
ShabaUser user = new ShabaUser( rs.getString( "username" ), rs.getString( "password" ),
rs.getBoolean( "enabled" ), rs.getString( "first_name" ),
rs.getString( "last_name" ), rs.getString( "email" ),
rs.getString( "date_joined" ), rs.getString( "last_online" ), true, true, true,
roles );
// Can be null!
Integer awesomeness = rs.getInt( "awesomeness" );
if ( rs.wasNull() )
{
awesomeness = null;
}
user.setAwesomeness( awesomeness );
return user;
}
}
private class ShabaUserListExtractor implements ResultSetExtractor<List<ShabaUser>>
{
private final ShabaUserMapper rowMapper;
private int rowsExpected;
public ShabaUserListExtractor()
{
this( new ShabaUserMapper(), 0 );
}
public ShabaUserListExtractor( ShabaUserMapper rowMapper, int rowsExpected )
{
Assert.notNull( rowMapper, "RowMapper is required" );
this.rowMapper = rowMapper;
this.rowsExpected = rowsExpected;
}
@Override
public List<ShabaUser> extractData( ResultSet rs ) throws SQLException
{
HashMap<String, ShabaUser> results = ( this.rowsExpected > 0
? new HashMap<String, ShabaUser>(
rowsExpected )
: new HashMap<String, ShabaUser>() );
int rowNum = 0;
while ( rs.next() )
{
ShabaUser user = rowMapper.mapRow( rs, rowNum++ );
if ( results.containsKey( user.getUsername() ) )
{
ShabaUser inUser = results.get( user.getUsername() );
ArrayList<GrantedAuthority> combinedAuthorities = new ArrayList<GrantedAuthority>();
combinedAuthorities.addAll( inUser.getAuthorities() );
combinedAuthorities.addAll( user.getAuthorities() );
results.put( user.getUsername(),
createUserDetails( user.getUsername(), user, combinedAuthorities ) );
} else
{
results.put( user.getUsername(), user );
}
}
return new ArrayList<ShabaUser>( results.values() );
}
}
我知道这是很多代码,但希望您能看到这里完成了什么。实际的 RowMapper 实现实际上是为了容纳所有从行信息中提取对象的“脏工作”。
只要您的数据库设置正确,并且在必需的列上设置了 NOT NULL,您就永远不会遇到提取空行的问题。虽然我认为检查 ResultSet 的空响应不会有什么坏处,但如果该列应该有一个值,您仍然会最终抛出异常。
关于Java Spring - RowMapper ResultSet - 整数/空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266078/
有没有一种简单的方法可以将给定的整数格式化为具有固定长度和前导零的字符串?#convertnumberstostringsoffixedlength3[1,12,123,1234].map{|e|???}=>["001","012","123","234"]我找到了解决方案,但也许还有更聪明的方法。format('%03d',e)[-3..-1] 最佳答案 如何使用%1000而不是进行字符串操作来获取最后三位数字?[1,12,123,1234].map{|e|format('%03d',e%1000)}更新:根据theTinMan的
我有很多这样的文档:foo_1foo_2foo_3bar_1foo_4...我想通过获取foo_[X]的所有实例并将它们中的每一个替换为foo_[X+1]来转换它们。在这个例子中:foo_2foo_3foo_4bar_1foo_5...我可以用gsub和一个block来做到这一点吗?如果不是,最干净的方法是什么?我真的在寻找一个优雅的解决方案,因为我总是可以暴力破解它,但我觉得有一些正则表达式技巧值得学习。 最佳答案 我(完全)不懂Ruby,但类似这样的东西应该可以工作:"foo_1foo_2".gsub(/(foo_)(\d+)/
问题1:我无法通过以下方式找到将负整数转换为二进制的方法。我应该像这样转换它。-3=>"11111111111111111111111111111101"我在下面试过:sprintf('%b',-3)=>"..101"#..appearsanddoesnotshow111111bit.-3.to_s(2)=>"-11"#Thisjustadds-tothebinaryofthepositiveinteger3.问题2:有趣的是,如果我使用在线转换器,它告诉我-3的二进制是“0010110100110011”。"11111111111111111111111111111101"和"001
我有一个随机大小的散列,它可能有类似"100"的值,我想将其转换为整数。我知道我可以使用value.to_iifvalue.to_i.to_s==value来做到这一点,但我不确定我将如何在我的散列中递归地做到这一点,考虑到一个值可以是一个字符串,或一个数组(哈希或字符串),或另一个哈希。 最佳答案 这是一个非常简单的递归实现(尽管必须同时处理数组和散列会增加一些技巧)。deffixnumifyobjifobj.respond_to?:to_i#IfwecancastittoaFixnum,doit.obj.to_ielsifobj
我想在ruby中生成一个64位整数。我知道在Java中你有很多渴望,但我不确定你会如何在Ruby中做到这一点。另外,64位数字中有多少个字符?这是我正在谈论的示例......123456789999。@num=Random.rand(9000)+Random.rand(9000)+Random.rand(9000)但我认为这是非常低效的,必须有一种更简单、更简洁的方法来做到这一点。谢谢! 最佳答案 rand可以将范围作为参数:pa=rand(2**32..2**64-1)#=>11093913376345012184putsa.
如果我想要“00001”而不是“1”,除了我自己写填零方法之外,有没有内置的方法可以帮助我为整数填零? 最佳答案 puts"%05d"%1#00001参见:String::%,Kernel::sprintf这是正在发生的事情。%左侧的"%05d"是C风格的格式说明符。%右边的变量就是要格式化的东西。格式说明符可以像这样解码:%-格式说明符的开头0-用前导零填充5-长度为5个字符d-被格式化的是一个整数如果你要格式化多个东西,你会把它们放在一个数组中:"%d-%s"%[1,"One"]#=>1-one
我相信我对这个问题有一个很好的答案,但我想确保ruby-philes没有更好的方法来做到这一点。基本上,给定一个输入字符串,我想在适当的情况下将该字符串转换为整数,或在适当的情况下将其转换为float。否则,只返回字符串。我会在下面发布我的答案,但我想知道是否有更好的方法。例如:to_f_or_i_or_s("0523.49")#=>523.49to_f_or_i_or_s("0000029")#=>29to_f_or_i_or_s("kittens")#=>"kittens" 最佳答案 我会尽可能避免在Ruby中使用正则表达式
我知道我可以使用Fixnum#to_s将整数表示为二进制格式的字符串。但是1.to_s(2)生成1而我希望它生成00000001。我怎样才能使所有返回的字符串都以零作为填充到8个字符?我可以使用类似的东西:binary="#{'0'*(8-(1.to_s(2)).size)}#{1.to_s(2)}"if(1.to_s(2)).size但这看起来不是很优雅。 最佳答案 使用字符串格式。"%08b"%1#=>"00000001" 关于ruby-如何在Ruby中返回整数的固定长度二进制表示?
我有一份报告,其中列出了总值,然后在括号中列出了变化。例如:歌曲:45首(比上周增加10首)所以我想将整数10打印为“+10”,将-10打印为“-10”我现在正在做(song_change>=0?'+':'')+song_change.to_s有没有更好的办法? 最佳答案 "%+d"%song_changeString#%根据字符串中的打印说明符格式化右侧。打印说明符“%d”也表示十进制。整数,添加到打印说明符的“+”强制始终打印适当的符号。您可以在Kernel#sprintf中找到有关打印说明符的更多信息,或在manpagefor
我目前正在将一种算法从Java转换为Ruby,但由于Ruby中缺少整数溢出,我遇到了一些障碍。假设我的值为2663860877,它大于最大整数2147483648。在Java中,它环绕,我应该得到-1631106419。我找到了这段代码,但它似乎不起作用:defforce_overflow(i)ifi2147483647i&0xffffffffelseiendend并且'ing变量不会像您期望的那样强制它为负。 最佳答案 假设32位整数具有二进制补码负数,这应该可行:defforce_overflow_signed(i)force_