@Oleksandr’s answer is correct, but the only way I could get it to work is as follows. I’m using Eclipselink on PostgreSQL.
public interface UserRepository extends JpaRepository<User, Long>
{
@Query("SELECT CASE WHEN COUNT(u) > 0 THEN 'true' ELSE 'false' END FROM User u WHERE u.username = ?1")
public Boolean existsByUsername(String username);
}