|
@@ -4,8 +4,18 @@ import cn.iselab.mooctest.user.dao.UserDao;
|
|
import cn.iselab.mooctest.user.model.User;
|
|
import cn.iselab.mooctest.user.model.User;
|
|
import cn.iselab.mooctest.user.service.UserService;
|
|
import cn.iselab.mooctest.user.service.UserService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
|
+import org.springframework.data.jpa.domain.Specifications;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @author sean
|
|
* @author sean
|
|
* @date 2018-03-04.
|
|
* @date 2018-03-04.
|
|
@@ -40,4 +50,30 @@ public class UserServiceImpl implements UserService {
|
|
public User update(User user) {
|
|
public User update(User user) {
|
|
return userDao.save(user);
|
|
return userDao.save(user);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<User> findByCondition(Map<String,String> condition) {
|
|
|
|
+ Specification<User> where = Specifications.where(getUserWhereClause(condition));
|
|
|
|
+ return userDao.findAll(where);
|
|
|
|
+ }
|
|
|
|
+ private Specification<User> getUserWhereClause(Map<String, String> condition) {
|
|
|
|
+ return new Specification<User>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
|
|
|
+ Predicate predicate = cb.disjunction();
|
|
|
|
+
|
|
|
|
+ Field[] fields = User.class.getDeclaredFields();
|
|
|
|
+ for (Field field : fields) {
|
|
|
|
+ String fieldName = field.getName();
|
|
|
|
+ if (condition.containsKey(fieldName)) {
|
|
|
|
+ predicate.getExpressions().add(
|
|
|
|
+ cb.like(root.get(fieldName), "%" + condition.get(fieldName) + "%")
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return predicate;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ }
|
|
}
|
|
}
|