前几天写的,贴上来。
实体类。
package com.syl.demo.daomain; import lombok.Data; import javax.persistence.*; /**
* Created by 孙义朗 on 2017/10/31.
*/
@Entity
@Data
public class User {
@Id
private String userId;
private String userName;
private String password;
private String gender;//性别
private String age;
@ManyToOne
private School school;
@ManyToOne
private Grade grade;
}
User
package com.syl.demo.daomain; import lombok.Data; import javax.jws.soap.SOAPBinding;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; /**
* Created by 孙义朗 on 2017/10/31.
*/
@Data
@Entity
public class School {
@Id
private String schoolId;
private String schoolName; }
School
package com.syl.demo.daomain; import lombok.Data; import javax.persistence.*; /**
* Created by 孙义朗 on 2017/11/16.
*/
@Entity
@Data
public class Grade {
@Id
private String gradeId;
private String gradeName;
@ManyToOne
private School school; }
Grade
还有一个封装查询条件的类
package com.syl.demo.daomain; import lombok.Data; import javax.persistence.Id; /**
* Created by 孙义朗 on 2017/11/16 0016.
*/
@Data
public class Params {
private String userId;
private String userName;
private String gender;
private String age; private String gradeName; private String schoolName; }
Params
Dao层
package com.syl.demo.dao; import com.syl.demo.daomain.Grade;
import com.syl.demo.daomain.School;
import com.syl.demo.daomain.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; /**
* Created by 孙义朗 on 2017/11/16 0016.
*/
public interface UserRepository extends JpaRepository<User,String> {
List<User> findAll(); List<User> findAll(Specification<User> spc, Pageable pageable); }
UserRepository
Service及实现
package com.syl.demo.service; import com.syl.demo.daomain.Grade;
import com.syl.demo.daomain.Params;
import com.syl.demo.daomain.School;
import com.syl.demo.daomain.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import java.util.List; /**
* Created by 孙义朗 on 2017/11/16 0016.
*/
public interface UserService {
List<User> findAll(); List<User> findAll(int pageNum, int pageSize,User user); List<User> findAll(int pageNum, int pageSize, Params params); }
UserService
package com.syl.demo.service.impl; import com.syl.demo.dao.UserRepository;
import com.syl.demo.daomain.Grade;
import com.syl.demo.daomain.Params;
import com.syl.demo.daomain.School;
import com.syl.demo.daomain.User;
import com.syl.demo.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List; /**
* Created by 孙义朗 on 2017/11/16 0016.
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository; @Override
public List<User> findAll() {
return userRepository.findAll();
} //查询User,单表,多条件
@Override
public List<User> findAll(int pageNum, int pageSize, User user) {
Pageable pageable = new PageRequest(pageNum, pageSize);
List<User> uList = userRepository.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if (user.getUserId() != null && !user.getUserId().equals("")) {
predicates.add(cb.like(root.get("userId").as(String.class), "%" + user.getUserId() + "%"));
}
if (user.getUserName() != null && !user.getUserName().equals("")) {
predicates.add(cb.like(root.get("userName").as(String.class), "%" + user.getUserName() + "%"));
}
if (user.getGender() != null && !user.getGender().equals("")) {
predicates.add(cb.like(root.get("gender").as(String.class), "%" + user.getGender() + "%"));
}
if (user.getAge() != null && !user.getAge().equals("")) {
predicates.add(cb.like(root.get("age").as(String.class), "%" + user.getAge() + "%"));
}
Predicate[] pre = new Predicate[predicates.size()];
criteriaQuery.where(predicates.toArray(pre));
return cb.and(predicates.toArray(pre));
}
}, pageable); return uList;
} //查询User,多表,多条件
@Override
public List<User> findAll(int pageNum, int pageSize, Params params) {
Pageable pageable = new PageRequest(pageNum, pageSize);
List<User> uList = userRepository.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();
//根据userId 查询user
if (StringUtils.isNotBlank(params.getUserId())) {
list.add(cb.equal(root.get("userId").as(String.class), params.getUserId()));
}
//根据userName 模糊查询user
if (StringUtils.isNotBlank(params.getUserName())) {
list.add(cb.like(root.get("userName").as(String.class), "%" + params.getUserName() + "%"));
}
//根据gender 查询user
if (StringUtils.isNotBlank(params.getGender())) {
list.add(cb.equal(root.get("gender").as(String.class), params.getGender()));
}
//根据age>? 查询user
if (StringUtils.isNotBlank(params.getAge())) {
list.add(cb.gt(root.get("age").as(Integer.class), Integer.valueOf(params.getAge())));
}
//根据gradeName 查询user
if (StringUtils.isNotBlank(params.getGradeName())) {
Join<Grade, User> join = root.join("grade", JoinType.LEFT);
list.add(cb.equal(join.get("gradeName"), params.getGradeName()));
}
//根据schoolName 查询user
if (StringUtils.isNotBlank(params.getSchoolName())) {
Join<School, User> join = root.join("grade", JoinType.LEFT);
list.add(cb.equal(join.get("school").get("schoolName"), params.getSchoolName()));
}
Predicate[] pre = new Predicate[list.size()];
criteriaQuery.where(list.toArray(pre));
return cb.and(list.toArray(pre));
}
}, pageable); return uList;
} }
UserServiceImpl
控制层
package com.syl.demo.controller; import com.syl.demo.daomain.Grade;
import com.syl.demo.daomain.Params;
import com.syl.demo.daomain.School;
import com.syl.demo.daomain.User;
import com.syl.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; /**
* Created by 孙义朗 on 2017/11/16 0016.
*/
@RestController
public class UserController {
@Autowired
UserService userService; @GetMapping(value = "/getUser")
public List<User> getUser() {
List<User> uList = userService.findAll();
return uList;
} //查询User,单表,多条件
@PostMapping(value = "/getUser/{pageNum}/{pageSize}")
public List<User> getUser(@PathVariable("pageNum") Integer pageNum,
@PathVariable("pageSize") Integer pageSize,
@RequestBody User user) {
List<User> uList = userService.findAll(pageNum, pageSize, user);
return uList;
} //查询User,多表,多条件
@PostMapping(value = "/getUser2/{pageNum}/{pageSize}")
public List<User> getUser2(@PathVariable("pageNum") Integer pageNum,
@PathVariable("pageSize") Integer pageSize,
@RequestBody Params params) {
List<User> uList = userService.findAll(pageNum, pageSize, params);
return uList;
} }
UserController
完成。
postman测试效果如下
(单表查询http://www.cnblogs.com/arrrrrya/p/7831248.html)