如何在JPA查询中对行表进行计数

时间:2019-01-09 03:13:15

标签: mysql spring-boot spring-data-jpa jpql

我是Spring Boot的新手。我有一个mysql数据库,我使用查询对表中的行进行计数。但这是行不通的,它仍然返回我的原始表数据。你能帮我检查一下我的代码吗?

这是我的实体:

ERROR (SelectorManager.run):  java.lang.IllegalStateException: character zero is not allowed in output
at org.xmlpull.mxp1_serializer.MXSerializer.writeAttributeValue(MXSerializer.java:849)
at org.xmlpull.mxp1_serializer.MXSerializer.attribute(MXSerializer.java:624)

这是我的查询:

@Entity
@Table(name = "act_id_membership", schema = "activiti", catalog = "")
@IdClass(ActIdMembershipEntityPK.class)
public class ActIdMembershipEntity {
    private String userId;
    private String groupId;

    @Id
    @Column(name = "USER_ID_")
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Id
    @Column(name = "GROUP_ID_")
    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ActIdMembershipEntity that = (ActIdMembershipEntity) o;
        return Objects.equals(userId, that.userId) &&
                Objects.equals(groupId, that.groupId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, groupId);
    }
}

更新的代码: 我的服务等级:

@Repository
public interface MemershipRepository extends JpaRepository<ActIdMembershipEntity, String> {
    @Query ("select new com.example.activiti_restful.dtos.UserMembership(i.userId, count(i)) from ActIdMembershipEntity i where i.userId ='kermit'")
    UserMembership countMemberships(String userId);
}

我的资源分类:

@Service
public class MembershipService {
@Autowired
private MemershipRepository repository;

public long count() {
    return repository.count();
}

我的自定义JSON对象类:

@RestController
public class MembershipResource {
@Autowired
private MembershipService membershipService;

@GetMapping("/membership")
public long list() {return membershipService.count();}
}

MySQL表: act_id_membership

2 个答案:

答案 0 :(得分:1)

使用CrudRepository按照repositories documentation提供了一种称为 count()的方法,该方法是实现JpaRepository的{​​{3}}的一种。

根据Superinterfaces说:

  

long count();返回实体数。

然后,您应该使用CrudRepository方法。除了通过CrudRepository documentation删除大写MembershipREPOSITORY之外,还必须按照MembershipRepository的方式使用。

 @Repository
 public interface MembershipRepository extends JpaRepository <ActIdMembershipEntity, String> {
 }

并在您的服务中使用它:

 @Service
 public class MembershipService {

     @Autowired
     private MembershipRepository repo;

     public long count() {
         return repo.count();
     }
 }

已更新

根据您的要求: 在控制器中:

@RestController
public class MembershipResource {

    @Autowired
    private MembershipService membershipService;

    @GetMapping("/membership")
    public List<Object> list() { return membershipService.countMemberships();
    }
}

服务中:

@Service
public class MembershipService {

    @Autowired
    private MemershipRepository repository;

    public List<Object> countMemberships() {
        return repository.countMemberships();
    }

}

在存储库中:

@Repository
public interface MemershipRepository extends JpaRepository<ActIdMembershipEntity, String> {
    @Query ("select i.userId, count(i) from ActIdMembershipEntity i where i.userId ='kermit'")
    List<Object> countMemberships();
}

答案 1 :(得分:1)

*>实际上,我希望它返回一个[[{name:kermit,value:6}]之类的json格式。现在只返回数字6。我该怎么做?谢谢!

首先,创建一个包装数据的类:

public class UserMembership {

 private String userId;

 private long numberOfUsers;

 public UserMembership(String userId, long numberOfUsers) {
       this.userId = userId;
       this.numerOfUsers = numberOfUsers;     
 }

}

然后

@Repository 
public interface MembershipRepository extends JpaRepository <ActIdMembershipEntity, String> {
     @Query ("select new *****.UserMembership(i.userId, count(i)) from ActIdMembershipEntity i where i.userId = :userId")
     UserMembership countMemberships(String userId);
}

*****:您的完整包裹名称

希望有帮助!