Hiberinate+json+jsp+jquery+ajax+mysql小项目

Web前端 来源:sinat_37109615 47℃ 0评论

一,项目环境:

系统;windows7

JDk版本:jdk1.7

开发工具:myeclipse 8.6.1

jquery:2.03

数据库:mysql5.7

二,项目效果:


点击"获取信息",得到json传入的值和数据库值

三/项目流程:


1/配置Hibernate的环境,并生成数据库users表的Bean类Users.java

数据库users表如图:

2.导入json包和jquery包如图:


3/页面代码:

数据库操作代码SqlDao.java:

package it.hua.dao;
import it.hua.factory.HibernateSessionFactory;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;


public class SqlDao {

    public static List getAll(){
    List list=new ArrayList();
      //获取Session对象
    Session s=HibernateSessionFactory.getSession();
      //创建查询
    Query q=s.createQuery("from Users");
    list=q.list();
    return list;
    }
public static void main(String[] args) {
// TODO Auto-generated method stub


}
}

jsp页面 index.jsp:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>" rel="nofollow" target="_blank" >
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="nofollow" target="_blank" >
-->
  
  <script type="text/javascript" src="js/jquery-2.0.3.min.js" rel="nofollow" target="_blank" ></script>
  </head>
  <body>
   <input type="submit" id="sub" value="获取信息"/>
   <table border="1" id="t">
   </table>
   <script type="text/javascript" src="js/js.js" rel="nofollow" target="_blank" ></script>
  </body>
</html>

jquery 页面js.js:

$("#sub").click(function(){
$.ajax({
    type:"post",
    url:"servlet/ajaxSer",
    dataType:"json",
    data:{
"name":"james",
"age":"23"
    },
    success:function(data){
    $("#t").append("<tr><td>json传入的值:</td><tr>");
    $("#t").append("<tr><td>姓名</td><td>年龄</td><tr>");
      //data[0]里存储的是map对象
    $("#t").append("<tr><td>"+data[0].xname+"</td><td>"+data[0].xage+"</td><tr>");
        $("#t").append("<tr><td>数据库传入的值:</td><tr>");
    $("#t").append("<tr><td>姓名</td><td>密码</td><tr>");
       //data[1]里存储的是list1对象
$.each(data[1],function(i,obj){
if(i>=0&i<=2){
$("#t").append("<tr><td>"+obj.uname+"</td><td>"+obj.upwd+"</td></tr>");
}
});
}
});
});

servlet端代码ajaxSer.java:

package it.hua.ser;
import it.hua.dao.SqlDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter out = response.getWriter();
           //获取到json传来的name和age
String name=request.getParameter("name");
String age=request.getParameter("age");
      List list=new ArrayList();
Map map=new HashMap();
map.put("xname", name);
map.put("xage",age);
              //将map放入list
list.add(map);
   List list1=new ArrayList();
            //获取到数据库表的list1
  list1=SqlDao.getAll();
           //将list1放入list
list.add(list1);
           //将list转码json
JSONArray json=JSONArray.fromObject(list);
            //发送数据
out.print(json);
out.flush();
out.close();
}