如何在Java中实现MySQL索引查询?

时间:2019-01-22 11:20:01

标签: mysql hibernate spring-mvc

@Override
public List<User> findUsersByCriteria(String firstName, String lastName, String state, String city, String phoneNumber, String emailAddress, String instructorLevel, String workPlace, String status) throws NCIDAOException {
    StringBuilder findUserByCriteria = new StringBuilder("CREATE INDEX idx_(user_id,first_name,last_name, ON user(user_id)");

    int statuss = 0;
    if (status != null && !status.isEmpty()) {
        if (status.matches("Active")) {
            statuss = 0;
        }
        if (status.matches("InActive")) {
            statuss = 1;
        }
    }
    LOG.info("Dao  is called...{}", instructorLevel);
    StringBuilder findByCriteria = new StringBuilder("select * from user where");

    if (firstName != null && !firstName.isEmpty()) {
        findByCriteria.append(" user.first_name like '%" + firstName + "%' ");
    } else {
        findByCriteria.append(" user.first_name is not null");
    }
    if (lastName != null && !lastName.isEmpty()) {
        findByCriteria.append(" and user.last_name like '%" + lastName + "%' ");
    } else {
        findByCriteria.append(" and user.last_name is not null");
    }
    if (emailAddress != null && !emailAddress.isEmpty()) {
        findByCriteria.append(" and user.email_address like '%" + emailAddress + "%' ");
    } else {
        findByCriteria.append(" and user.email_address is not null");
    }
    if (phoneNumber != null && !phoneNumber.isEmpty()) {
        findByCriteria.append(" and user.user_id in (select user_profile_id from user_profile where phone_no='"
                + phoneNumber + "')");
    }
    if (state != null && !state.isEmpty() && !state.equals("All")) {
        findByCriteria.append(
                " and user.user_id in (select user_profile_id from user_profile where state='" + state + "')");
    }
    if (city != null && !city.isEmpty()) {
        findByCriteria.append(
                " and user.user_id in (select user_profile_id from user_profile where city='" + city + "')");
    }
    if (instructorLevel != null && !instructorLevel.isEmpty()) {
        findByCriteria
                .append(" and user.user_id in (select user_id from instructor where Instructor_user_id in (select instructor_user_id from instructor_user_subroles where sub_role_id = '"
                        + instructorLevel + "'))");
    }
    if (workPlace != null && !workPlace.isEmpty()) {
        findByCriteria.append(" and user.user_id in (select Instructor_user_id from instructor where work_place='"
                + workPlace + "')");
    }
    if (status != null && !status.isEmpty() && !status.equals("Both")) {
        findByCriteria.append(" and user.is_expired =" + statuss + " ");

    }else if (status.equals("Both")) {
        findByCriteria.append(" and user.is_expired is not null");

    } else {
        findByCriteria.append(" and user.is_expired like '%" + 0 + "%'");
    }
    findByCriteria.append(" order by user.user_id desc ");
    LOG.info("Final Query for find by criteria: " + findByCriteria.toString());

    javax.persistence.Query queryList = em.createNativeQuery(findByCriteria.toString(), User.class);
    LOG.info("find users by criteria Daoooo 10 {}", queryList.getResultList().toString());
    return queryList.getResultList();

}

为了获得更好的性能,我们在数据库中创建了mysql索引,但是我们无法使用Java程序的方法来实现该索引查询。我们已经使用了Hibernate,MySQL,Spring MVC和AWS RDS。

0 个答案:

没有答案