今天做了一個(gè)ajax動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)的小Demo,又重新學(xué)習(xí)了一下ajax的一些知識(shí)。在此和大家分享一下......
啥都別說了,先上代碼
Controller層
查詢總用戶數(shù)
@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>
![](/d/20211017/5fa8c63b6b795fa1e48c6b40c954c49b.gif)
結(jié)果就是在Total Users 動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù)并更新........
以上這篇ajax動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)數(shù)據(jù)并顯示在前臺(tái)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Ajax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法
- json獲取數(shù)據(jù)庫(kù)的信息在前端頁(yè)面顯示方法
- AJAX驗(yàn)證數(shù)據(jù)庫(kù)內(nèi)容并將值顯示在頁(yè)面
- Ajax返回的json遍歷取值并顯示到前臺(tái)的方法