spring boot 运用vue-Specification技术,实现动态查询功能

spring boot 运用vue-Specification技术,实现动态查询功能

Dao层&Mapper层(持久层)下的
IStudent.java


public interface IStudent extends JpaRepository<Student,Integer> {
List<Student> findAll(Specification<Student> specification);
    Long count(Specification<Student> specification);
    Page<Student> findAll(Specification<Student> specification, Pageable pageable);
}

Service层(逻辑层)下的ServiceStudent.java

@Service
public class ServiceStudent {
    @Resource
    IStudent studentDao;
	public List<Student> queryFlows(int pageNo, int pageSize, Student student) {
        List<Student> result = null;
        // 构造自定义查询条件
        Specification<Student> queryCondition = new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (student.getName() != null) {
                    predicateList.add(criteriaBuilder.like(root.get("name"), "%" +student.getName()+ "%"));
                }
                if (student.getSid()!=0) {
                    predicateList.add(criteriaBuilder.equal(root.get("sid"),student.getSid()));
                }
                if (student.getTelephone()!= null) {
                    predicateList.add(criteriaBuilder.equal(root.get("telephone"),student.getTelephone()));
                }
//                if (status != null) {
//                    predicateList.add(criteriaBuilder.equal(root.get("status"), status));
//                }
//                if (createTimeStart != null && createTimeEnd != null) {
//                    predicateList.add(criteriaBuilder.between(root.get("createTime"), createTimeStart, createTimeEnd));
//                }
                    return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
                }
        };

        // 分页和不分页,这里按起始页和每页展示条数为0时默认为不分页,分页的话按创建时间降序
        try {
            if (pageNo == 0 && pageSize == 0) {
                result = studentDao.findAll(queryCondition);
            } else {
                result = studentDao.findAll(queryCondition, PageRequest.of(pageNo - 1, pageSize, Sort.by(Sort.Direction.DESC, "sid"))).getContent();
            }
        } catch (Exception e) {
            //  LOGGER.error("--queryFlowByCondition-- error : ", e);
        }

        return result;
    }
}

controller(控制层)

@RequestMapping("/index")
    @ResponseBody
    public JsonResult index(Student student, HttpServletRequest request){

        //List<Student> data= studentService.findAll(Sort.by("sid"));
        System.out.print(request.getParameter("name"));
        System.out.print(request.getParameter("sid"));
        System.out.print(request.getParameter("telephone"));
        List<Student> data= serviceStudent.queryFlows(1,5,student);
        return new JsonResult<>(data, "获取学生列表成功");
    }

view(视图层)

<td>
                  姓名 <input type="text" name="name" v-model="like.name">
                   班级:
                   <select v-model="like.sid">
                       <option v-for="(v,index) in classes":value="v.cid">{{v.cname}}</option>
                   </select>
                   电话:<input type="text" name="telephone" v-model="like.telephone">
                   <button v-on:click="findName()">查询</button>
               </td>

vue代码

<script type="text/javascript">
    var vm=new Vue({
        el: "#root",
        data: {
        	like:{},
        },
        methods:{
 		findName:(function () {
                $.ajax({
                    url:"http://localhost:8080/student/index",
                    data:this.like,
                    success:function (result) {

                        if (result.code == 0) {
                            vm.list = [];
                            var len = result.data.length;
                            for (var i = 0; i < len; i++) {
                                vm.list.push(result.data[i]);
                            }
                        } else {
                            vm.message = result.msg;
                        }
                    }
                })
            }),
}
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

平平常常一般牛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值