文件结构:
1.全部字段返回
controller层:
BookController.class
@RequestMapping("/Book")
@RestController
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value ="/getAllByOther",method = RequestMethod.GET)
public String getAllByOther(
@RequestParam("Status") String Status, //状态
@RequestParam("Code") String Code,//编码
@RequestParam("Author") String Author,//作者
@RequestParam("CountPage") Integer CountPage //书本页数
){
try{
List<Map<String, Object>> listBook= parkService.getAllByOther(Status,Code,Author,CountPage);
System.out.println(listBook) //输出结果
}catch (Exception e){
e.printStackTrace();
}
return "";
}
}
BookService.class
public interface BookService {
List<Map<String, Object>> getAllByOther(String Status,String Code, String Author,Integer CountPage)throws Exception;
}
BookServiceImpl.class
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public List<Map<String, Object>> getAllByOther(String Status, String Code, String Author, Integer CountPage) {
List<Map<String, Object>> res = null;
try{
res = bookDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<>();
criteriaQuery.multiselect();
if (Status!=null && Status!="") {
predicateList.add(cb.equal(root.get("Status"), Status)); //字段
}
if (Code!=null && Code!="") {
predicateList.add(cb.equal(root.get("Code"), Code));
}
if (Author!=null && Author!="") {
predicateList.add(cb.equal(root.get("Author"), Author));
}
if (CountPage!=null) {
predicateList.add(cb.equal(root.get("CountPage"), CountPage));
}
Predicate[] pre = new Predicate[predicateList.size()];
criteriaQuery.where(predicateList.toArray(pre));
return cb.and(predicateList.toArray(pre));
}
},new Sort(Sort.Direction.DESC,"id")); //根据id倒序
}catch(Exception e){
return null;
}
return res;
}
2.多条件查找返回部分指定的字段
在Dao中新建一个class文件,注意,不是接口文件。
BookExtendDao.class
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;
@Repository
@Transactional(readOnly = true) //这个Dao文件只用来查询数据,插入修改删除无法执行
public class BookExtendDao {
@Autowired
private EntityManager entityManager;
public List<Map> findInfo (String Status, String Code, String Author, Integer CountPage){
StringBuilder sqljoint=new StringBuilder(300);
//此处返回id和书本编码
sqljoint.append("select id,Code from t_park where 1=1 ");
if(Status!=null && Status!=""){
sqljoint.append(" and Status= '"+Status+"'");
}
if(Code!=null && Code!=""){
sqljoint.append(" and Code= "+Code);
}
if(Author!=null && Author!=""){
sqljoint.append(" and Author = "+Author);
}
if(CountPage!=null){
sqljoint.append(" and CountPage= "+CountPage);
}
sqljoint.append(" order by id desc");
String sql=sqljoint.toString();
Query query = null;
try {
query = entityManager.createNativeQuery(sql);
query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
} catch (Exception e) {
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
List list = query.getResultList();
return list;
}
}
BookService中新增方法
public interface BookService {
List<Map<String, Object>> getAllByOther(String Status,String Code, String Author,Integer CountPage)throws Exception;
//新增的方法
List<Map> findInfo(String Status,String Code, String Author,Integer CountPage)throws Exception;
}
BookServiceImpl中新增方法
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public List<Map<String, Object>> getAllByOther(String Status, String Code, String Author, Integer CountPage) {
List<Map<String, Object>> res = null;
try{
res = bookDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<>();
criteriaQuery.multiselect();
if (Status!=null && Status!="") {
predicateList.add(cb.equal(root.get("Status"), Status)); //字段
}
if (Code!=null && Code!="") {
predicateList.add(cb.equal(root.get("Code"), Code));
}
if (Author!=null && Author!="") {
predicateList.add(cb.equal(root.get("Author"), Author));
}
if (CountPage!=null) {
predicateList.add(cb.equal(root.get("CountPage"), CountPage));
}
Predicate[] pre = new Predicate[predicateList.size()];
criteriaQuery.where(predicateList.toArray(pre));
return cb.and(predicateList.toArray(pre));
}
},new Sort(Sort.Direction.DESC,"id")); //根据id倒序
}catch(Exception e){
return null;
}
return res;
}
//新增的方法
@Autowired
private BookExtendDao bookExtendDao ;
@Override
public List<Map> findInfo(String Status,String Code, String Author,Integer CountPage) throws Exception {
return bookExtendDao.findInfo(Status, Code, Author, CountPage);
}
}
BookController.class
@RequestMapping("/Book")
@RestController
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value ="/getAllByOther",method = RequestMethod.GET)
public String getAllByOther(
@RequestParam("Status") String Status, //状态
@RequestParam("Code") String Code,//编码
@RequestParam("Author") String Author,//作者
@RequestParam("CountPage") Integer CountPage //书本页数
){
try{
List<Map<String, Object>> listBook= parkService.getAllByOther(Status,Code,Author,CountPage);
System.out.println(listBook) //输出结果
}catch (Exception e){
e.printStackTrace();
}
return "";
}
//新增方法
@RequestMapping(value ="/getPart",method = RequestMethod.GET)
public String getPart(
@RequestParam("Status") String Status,
@RequestParam("Code") String Code,
@RequestParam("Author") String Author,
@RequestParam("CountPage") Integer CountPage
){
try{
List<Map> simpleInfo = bookService.findInfo(Status, Code, Author, CountPage);
System.out.println(simpleInfo);//结果
}catch (Exception e){
}
return "";
}
}