6月28日 SSH 周四

package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dto.Brand;
import com.dto.Shop;
import com.util.PageUtils;

public class ShopDao extends HibernateDaoSupport{

    public List findShopList(PageUtils pu) {
        // TODO Auto-generated method stub
        Query query = getSession().createQuery("from Shop").setFirstResult(pu.getStartIndex()).setMaxResults(pu.getPageSize());
        List list = query.list();
        return list;
    }

    public Integer getCount() {
        String sql=" select count(*) from t_shop ";
        SQLQuery query = getSession().createSQLQuery(sql);
        Integer count = Integer.valueOf(query.uniqueResult().toString());
//      Query query = getSession().createQuery("select count(*) from Shop");
//      Integer count = Integer.valueOf(query.toString());
        return count;
    }

    public List findBrandList() {
        // TODO Auto-generated method stub
        return getHibernateTemplate().find("from Brand");
    }

    public Shop getShop(Shop shop) {
        // TODO Auto-generated method stub
        Shop s = (Shop) getHibernateTemplate().get(Shop.class, shop.getId());
        return s;
    }

    public int saveOrUpdate(Shop shop) {
        // TODO Auto-generated method stub
        try {
            getHibernateTemplate().saveOrUpdate(shop);
            return 1;
        } catch (DataAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    public void delete(Shop shop) {
        // TODO Auto-generated method stub
        getHibernateTemplate().delete(shop);
    }

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


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'update.jsp' starting pagetitle>

    <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">
    

  <script type="text/javascript" src="js/jquery-1.8.2.js">script>
    <script type="text/javascript">
        var id = "${param.id}";
        if(id==""){
            id=-1;
        }
        $.post(
            "s_getData",
            {"shop.id":id},
            function (data){
                var brandList = data.brandList;
                for ( var i in brandList) {
                    $("select").append("+brandList[i].bname+"");
                }

                var shop = data.shop;
                $("[name='shop.id']").val(shop.id);
                $("[name='shop.name']").val(shop.name);
                $("[name='shop.price']").val(shop.price);
                $("[name='shop.datea']").val(shop.datea);
                $("[name='shop.num']").val(shop.num);
                $("[name='shop.b.bid']").val(shop.b.bid);
            },"json"
        );
        function sub(){
            $.post(
                "s_saveOrUpdate",
                $("form").serialize(),
                function (msg){
                    if(msg==1){
                        alert("提交成功");
                        location="s_list";
                    }
                    else{
                        alert("提交失败");
                    }
                }
            );
        }
    script>
  head>

  <body>
    <form>
        <input name="shop.id" type="hidden">
        name:<input name="shop.name"><br>
        price:<input name="shop.price"><br>
        datea:<input name="shop.datea"><br>
        num:<input name="shop.num"><br>
        brand:<select name="shop.b.bid">

        select>
    form>
    <input type="button" value="提交" onclick="sub()">
  body>
html>

言之无文,行而不远

你可能感兴趣的:(2018)