Springboot jpa 多条件模糊查询,部分字段和全部字段返回

文件结构:

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  "";
    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张帅三代

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值