在Java中使用JdbcTemplate进行查询时,可以使用queryForXxx()等方法
1、jdbcTemplate.queryForInt()和jdbcTemplate.queryForLong()
查询数据记录的条数,返回一个int(数据范围较小)或者一个Long(数据范围较大)类型
String todayCountTopicsSql = "SELECT count(*) FROM mcp_forum_post";
Integer todayCount = jdbcTemplate.queryForInt(todayCountTopicsSql);
或者
Long todayCount = jdbcTemplate.queryForLong(todayCountTopicsSql);
2、jdbcTemplate.queryForObject()
本质上和queryForInt()相同,返回的都是单行单列的一个数据,例如传回一个String对象
String userAccountSql = "SELECT account FROM scpn_user WHERE user_id = "
+ userAccountId;
String userAccount = (String)jdbcTemplate.queryForObject(userAccountSql,java.lang.String.class);
3、jdbcTemplate.queryForMap()
查询一行数据,即一条记录,一条记录包含多个字段,使用返回的列做key
String userAccountSql = "SELECT account,create_time FROM scpn_user where user_id = "
+ userAccountId;
Map userAccountMap = (Map)jdbcTemplate.queryForMap(userAccountSql);
String userAccount = (String)userAccountMap.get("account");
String createTime = (String)userAccountMap.get("create_time").toString();
4、jdbcTemplate.queryForList():返回Map的集合List(它包含多条记录),
用列名做key,每一个map代表一条数据库记录,需要使用循环来输出每一条记录,
如果想在结果集中加入一个字段,也可以采用如下的put方法。
String all = "SELECT * FROM mcp_forum_post";
List scpnPostList = jdbcTemplate.queryForList(all);
if(scpnPostList != null){
for(int i = 0;i < scpnPostList.size(); i++){
Long userAccountId = (Long)scpnPostList.get(i).get("user_id");
Long lastmodUser = (Long)scpnPostList.get(i).get("lastmod_user");
if(lastmodeUser != null){
String lastmodUserSql = "SELECT account FROM scpn_user WHERE user_id = " + lastmodUser;
String lastmodUserAccount = (String)jdbcTemplate.queryForObject(lastmodUserSql,java.lang.String.class);
scpnPostList.get(i).put("lastmodUserAccount",lastmodUserAccount);
}
}
}
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术