我有以下代码:
@Override
public String parsePrice(Document document) {
Elements metaElements = document.getElementsByTag("meta");
for (Element tag : metaElements) {
String content = tag.attr("content");
String item = tag.attr("itemprop");
if ("price".equals(item)) {
return content.equals("0") ? "Free" : content;
}
}
return "Information not available";
}
Run Code Online (Sandbox Code Playgroud)
它将返回类似“7,49$”的价格。我想用 java 8 功能替换此代码。我是流的新手,但尝试过:
metaElements.stream().filter(tag -> "price".equals(tag.attr("itemprop")))
.findFirst().orElse(null);
Run Code Online (Sandbox Code Playgroud)
但它返回 <meta itemprop="price" content="7,49$">
我不能像这样过滤(缺少 return 语句):
metaElements.stream().filter(tag -> {
String content = tag.attr("content");
String item = tag.attr("itemprop");
if ("price".equals(item)) {
return content.equals("0") ? "Free" : content;
}
}).findFirst().orElse(null);
Run Code Online (Sandbox Code Playgroud)
如何解决?
我在项目中找到了代码。看起来像:
private <S, T extends BaseEntity<S>> void saveIfNotExist(T baseEntity, JpaRepository<T, S> jpaRepository) {
jpaRepository
.findById(baseEntity.getId())
.orElseGet(() -> jpaRepository.save(baseEntity));
}
Run Code Online (Sandbox Code Playgroud)
据我了解,如果实体已经存在,此方法不会在数据库中保存/更新实体。
BaseEntity:
public interface BaseEntity<T> {
T gettId();
}
Run Code Online (Sandbox Code Playgroud)
执行:
@Entity
@Table(..)
public class Account implements BaseEntity<String> {
@Id
private String login;
@Column(name = "is_archive")
private Boolean isArchive;
public String getId() {
return this.getLogin();
}
}
Run Code Online (Sandbox Code Playgroud)
存储库:
@Repository
public interface AccountRepository extends JpaRepository<Account, Integer>
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何用常见的 Spring Data JPA 实现替换此方法(仅在不存在时保存,不更新)?
我有一个 JSON:
{
"range": "'All traffic",
"majorDimension": "ROWS",
"values": [
[
"Date",
"Sessions",
"Leeds",
"?ontact_query",
"wo_?ontact_query"
],
[
"20200107",
"3461",
"15",
"0",
"15"
],
[
"20200115",
"7824",
"43",
"0",
"43"
]
]
}
Run Code Online (Sandbox Code Playgroud)
values数组中的元素index[0]是 JSON 属性的名称。我想用以 index[0+] 开头的值映射这些名称。每个索引的值数量可以不同。映射应该是动态的,没有硬索引。
我想获得以下 JSON:
[
{
"Date": "20200107",
"Sessions": "3461",
"Leeds": "15",
"Contact_query": "0",
"wo_Contact_query": "15"
},
{
"Date": "20200115",
"Sessions": "7824",
"Leeds": "43",
"Contact_query": "0",
"wo_Contact_query": "43"
}
]
Run Code Online (Sandbox Code Playgroud)
我用有点奇怪的 JOLT 配置成功地做到了:
[
{
"operation": "shift",
"spec": {
"values": { …Run Code Online (Sandbox Code Playgroud) 我有一个包含 field 的实体date。
@Entity
@Table(name="messages", schema = "users")
...
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "author")
private String author;
@Column(name = "tags")
private String tags;
@Column(name = "message_date")
private LocalDate date;
@Override
public String toString() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
Message findByMessageId(Long id);
}
Run Code Online (Sandbox Code Playgroud)
我正在将 Spring Data JPA 与存储库一起使用。我想从数据库获取过去 3 天的所有消息(字段date)。我怎样才能用 Spring JPA 做到这一点?
@Query(...?)
List<Message> findBy...?
Run Code Online (Sandbox Code Playgroud) 我有三个 LocalDate(最后 3 天),对于每个日期,我想循环并执行一些操作:
StringBuilder result = "";
for (LocalDate currentDate = LocalDate.now(); currentDate.isAfter(LocalDate.now().minusDays(3)); currentDate = currentDate.minusDays(1)){
Page<User> users = userService.getAllUsersByRegistrationDate(currentDate);
String reportTable = reportService.getReportTable(users, tableTemplate);
result.append(reportTable);
}
Run Code Online (Sandbox Code Playgroud)
如何用 Stream API 替换此代码或提高可读性?