小编Sha*_*Sha的帖子

如何在具有相同类型参数的Java中创建方法?

我的代码如下所示:

enum EnumType {
 CATEGORY,
 GROUP,
 MAIN
}
Run Code Online (Sandbox Code Playgroud)

方法:

public void call(EnumType type){
   switch(type):
     case CATEGORY:
        return methodForCategory();
     case GROUP:
        return methodForGroup();
     ...
}
public void methodForCategory(){
   ... Operations according to EnumType.CATEGORY
}
public void methodForGroup(){
   ... Operations according to EnumType.GROUP
}
public void methodForMain(){
   ... Operations according to EnumType.MAIN
}
Run Code Online (Sandbox Code Playgroud)

但是我想在没有开关/情况下调用它,如下所示;

public void call(EnumType type){
    methodForType(EnumType type);
}
Run Code Online (Sandbox Code Playgroud)

有可能还是有更好的选择?

java oop design-patterns

5
推荐指数
1
解决办法
84
查看次数

是否可以摆脱 Spring Data JPA 存储库中的 @Param 注解

在Spring Data JPA中,当我想编写自定义查询时,如果我指定一个参数,我用@Param注释发送这个参数值,是否可以在Spring Data JPA存储库中摆脱@Param注释?

查询示例:

@Query("select u from User where u.name = :name")
User findUserByName(@Param("name") String name);
Run Code Online (Sandbox Code Playgroud)

所需查询:

@Query("select u from User where u.name = :name")
User findUserByName(String name);
Run Code Online (Sandbox Code Playgroud)

注意,我不想使用 ?0 或类似的东西,我想直接使用名称参数。

java spring-data-jpa

5
推荐指数
1
解决办法
2145
查看次数

使用 MockMvc 进行 Spring Boot Aspectj 测试

我有一个带有 Aspectj 的 Spring 启动代码。这段代码是用基本的 MVC 架构编写的。然后我只是尝试用 MockMVC 测试它。但是当我尝试对其进行测试时,Aspectj 并没有中断。Aspectj 有没有特殊的配置?

控制器:

@GetMapping("user/{userId}/todo-list")
public ResponseEntity<?> getWaitingItems(@RequestUser CurrentUser currentUser){
    ...handle it with service method.
}
Run Code Online (Sandbox Code Playgroud)

方面:

@Pointcut("execution(* *(.., @RequestUser (*), ..))")
void annotatedMethod()
{
}

@Before("annotatedMethod() && @annotation(requestUser)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, RequestUser requestUser)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

测试:

@WebMvcTest(value = {Controller.class, Aspect.class})
@ActiveProfiles("test")
@ContextConfiguration(classes = {Controller.class, Aspect.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class ControllerTest
{
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private Controller controller;

    @MockBean
    private Service service;

    @Before
    public void …
Run Code Online (Sandbox Code Playgroud)

java aspectj spring-boot spring-boot-test

4
推荐指数
2
解决办法
885
查看次数

将枚举值与类字段匹配

我在 JAVA 中有一个 enum 和 POJO 类。在枚举类中,每个枚举值都与 POJO 类的变量匹配......然后我想在两个类之间创建关系。

枚举类:

public enum MyEnum
{
   FIELD1,
   FIELD2,
   FIELD3,
   ...
}
Run Code Online (Sandbox Code Playgroud)

POJO类:

public class MyPojo
{
   private String field1;
   private String field2_not_ref;
   private String field3;
   ...
}
Run Code Online (Sandbox Code Playgroud)

然后,当我尝试匹配两个类的这些字段时,我实现了如下代码:

public String matchMethod(MyEnum myenum)
{
  switch(myenum)
  {
   case FIELD1:
      return field1;
   case FIELD2:
      return field2_not_ref;
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我认为这不是一个好的/明确的解决方案。有什么建议吗?

java oop enums switch-statement java-8

3
推荐指数
1
解决办法
1763
查看次数

如何在 Grafana 中将 Alias 模式与 ElasticSearch 结合使用?

我使用 ElasticSearch 作为源。在我的桌子上,我有两个指标和一个 groupBy,我想更改表上指标+ groupBy 的名称: 在此输入图像描述

但这对我不起作用。我还检查了这一点: https: //github.com/grafana/grafana/issues/4697

有什么建议吗?

elasticsearch grafana

2
推荐指数
1
解决办法
9049
查看次数

PostgreSQL 中的 Index-Only 和 Bitmap Index Scan 有什么区别?

在我的查询中,我只想使用精确的 where 条件调用数据。这些 where 条件是在 index.js 中创建的。但是解释显示位索引扫描。我不明白为什么。

我的查询如下所示:

Select 
r.spend,
r.date,
...
from metadata m 
inner join 
report r
on m.org_id = r.org_id and m.country_or_region = r.country_or_region and m.campaign_id = r.campaign_id and m.keyword_id = r.keyword_id  
where r.org_id = 1 and m.keyword_type = 'KEYWORD'
offset 0  limit 20 
Run Code Online (Sandbox Code Playgroud)

索引:

Metadata(org_id, keyword_type, country_or_region, campaign_id, keyword_id);
Report(org_id, country_or_region, campaign_id, keyword_id, date);
Run Code Online (Sandbox Code Playgroud)

解释分析:

"Limit  (cost=811883.21..910327.87 rows=20 width=8) (actual time=18120.268..18235.831 rows=20 loops=1)"
"  ->  Gather  (cost=811883.21..2702020.67 rows=384 width=8) (actual time=18120.267..18235.791 rows=20 loops=1)"
"        Workers Planned: …
Run Code Online (Sandbox Code Playgroud)

postgresql indexing

1
推荐指数
1
解决办法
714
查看次数

Spring Boot @Retryable 模拟测试

我有一个如下代码块:

    @Service
    ExecutorService {

    @Autowired
    IAccountService accountService;

        @Retryable(maxAttempts = 3, value = {DataIntegrityViolationException.class}, backoff = @Backoff(delay = 1000, multiplier = 2))
        public void execute(RequestDto reqDto)
        {
            Account acc = accountService.getAccount(reqDto.getAccountId);
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 Mockito 测试中,我只想按预期看到调用方法 3 次。

    @RunWith(SpringRunner.class)
    public class CampaignExecuterServiceTest
    {
        private static final Long ACCOUNT_ID = 1L;

        @InjectMocks
        private ExecutorService executorService;

        @Mock
        private IAccountService accountService;

        @Test
        public void execute_success()
        {
            Account account = new Account(ACCOUNT_ID, null, null, null, null);
            RequestDto reqDto = new RequestDto();
            when(accountService.getAccount(any())).thenThrow(DataIntegrityViolationException.class);
            executorService.execute(reqDto);
            verify(executorService, …
Run Code Online (Sandbox Code Playgroud)

unit-testing mockito spring-boot

1
推荐指数
1
解决办法
1万
查看次数