带有Postgresql的Apache Ignite缓存

时间:2018-06-29 07:26:26

标签: java apache caching jcache

我需要几天的时间来了解Postgressql的缓存 这是POST类

public class Post implements Serializable {
private static final long serialVersionUID = 0L;
private String id;
private String title;
private String description;
private LocalDate creationDate;
private String author;
// rest of the setter and getter are omitted
}

这是我根本听不懂的代码

@Override
public Post load(String key) throws CacheLoaderException {
    Map<String, Object> inputParam = new HashMap<>();
    inputParam.put("id", key);
    return jdbcTemplate.queryForObject("SELECT * FROM POSTS WHERE id=?", inputParam, new RowMapper<Post>() {
        @Override
        //WHAT THE mapRow method does? Is there another way to do some thing?
        public Post mapRow(ResultSet rs, int i) throws SQLException {
            return new Post(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5));
        }
    });

}

这是完整的代码:link

这些代码取自高性能内存计算 与Apache Ignite一起使用。 谢谢...

1 个答案:

答案 0 :(得分:0)

您在这里尝试处理下一个SQL请求:

“从ID ==的帖子中选择*?”

例如表“ POSTS”有5个条目。每个POST条目都包含5个字段。在数据库中执行此SQL命令时,将获得5行。每行将按一定顺序包含这5个字段(取决于表的创建方式)。

在您的示例中,您将使用jdbcTemplate.queryForObject方法。它需要实现RowMapper对象,该对象将从“ SELECT * FROM POSTS WHERE id =?”开始的行继续。

您可以在此处阅读有关内容:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/RowMapper.html#mapRow-java.sql.ResultSet-int-

工作原理:

例如“ SELECT * FROM POSTS WHERE id =?”应该为某些ID返回1或更多行。每行将显示为ResultSet对象(您可以将其视为具有5个字段的行)。在将每个ResultSet(行)返回给用户之前,应将其转换为POST对象。

public Post mapRow(ResultSet rs, int i) throws SQLException {
    return new Post(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5));
}

这段代码只是说,您获得具有5个字段(字符串,字符串,字符串,日期,字符串)的行,并使用这5个字段构建POST对象。

相关问题