ajax动态查询数据库数据并显示在前台的方法
今天做了一个ajax动态查询数据库的小Demo,又重新学习了一下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>
结果就是在Total Users 动态查询数据库中的数据并更新........
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05