关于ajax动态查询数据库数据并显示在前台的详细方法

Controller层

查询总用户数

 @RequestMapping(value = "/findTotalUsers.do",method = RequestMethod.GET)
 public @ResponseBody Long findTotalUsers(){
  ModelAndView modelAndView = new ModelAndView();
  Long sum = personService.findTotalUsers();
  System.out.println(sum+"....................................");
  modelAndView.addObject("sum",sum);
  return sum;
 }

Service层

public Long findTotalUsers() {
  return personDao.findTotalUsers();
 }

Dao层

public Long findTotalUsers() {
  String hql = "select count(*) from Person";
  return (Long) this.getSessionFactory().getCurrentSession().createQuery(hql).uniqueResult();
 }

ajax代码

<script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>
 <script type="text/javascript">
  $(document).ready(
    function ajaxRePost(url,params){
     var message = "";
     var options={
      type:"GET",
      url:"${pageContext.request.contextPath}/person/findTotalUsers.do",
      data:{},
      async:false,
      success:function (msg) {
       message=msg;
      }
     };
     $.ajax(options);
     alert(message);
//     debugger;
     $("#count").text(message);
     return message;
    }
  )
 
 </script>

20180821150457

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片