当前位置:编程学习 > C#/ASP.NET >>

做了个spring+mvc3的练习,把代码贴在这里

Dao:

public class CustomerDao : HibernateDaoSupport, ICustomerDao
    {
 public IList FindAll()
        {
            
            //return (IList)HibernateTemplate.LoadAll<Customer>();
            return (IList)HibernateTemplate.Find<Orders>(" select Distinct o from Orders o left join o.Customer c left join o.myProducts p ");

            //return (IList)HibernateTemplate.Find<Orders>(" from Orders o inner join Customer c on o.CustomerId = c.CustomerId ");
        }

  public Customer SaveOrUpdate(Customer customer)
        {
            HibernateTemplate.SaveOrUpdate(customer);
            return customer;
        }
}

namespace Dto
{
    public class CustomerEditDto
    {
        public virtual string CustomerId { get; set; }
       
        public virtual string FirstName { get; set; }
       
        public virtual string LastName { get; set; }
      
        public virtual DateTime Birthday { get; set; }
    }
}

namespace Dto
{
    public class CustomerListDto
    {
        public List<CustomerListDetail> customerListDetail{ get; set; }
        public CustomerListDto()
        {
            customerListDetail = new List<CustomerListDetail>();
        }

    }

    public class CustomerListDetail
    {
        public virtual string CustomerId { get; set; }
        
        public virtual string FirstName { get; set; }
        
        public virtual string LastName { get; set; }

         public virtual string OrderId { get; set; }

         public virtual Decimal Summary { get; set; }
        
        public virtual DateTime OrderDate { get; set; }        
    }

}

service
 public CustomerListDto GetAll()
        {
            CustomerListDto customerListDto = new CustomerListDto();
            IList<Orders> orderList = (List<Orders>)CustomerDao.FindAll();
            foreach (Orders o in orderList)
            {
                CustomerListDetail listDetail = new CustomerListDetail();
                listDetail.CustomerId =o.Customer==null? "": o.Customer.CustomerId;
                listDetail.FirstName = o.Customer == null ? "" : o.Customer.FirstName;
                listDetail.LastName = o.Customer == null ? "" : o.Customer.LastName;
                listDetail.OrderId = o.OrderId;
                listDetail.Summary = o.Summary;
                listDetail.OrderDate = o.OrderDate;
                customerListDto.customerListDetail.Add(listDetail);

            }
            return customerListDto;
        }

 public CustomerEditDto UpdateCustomer(CustomerEditDto customerDto)
        {
            Customer customer = new Customer();

            customer.CustomerId = customerDto.CustomerId;
            customer.FirstName = customerDto.FirstName;
            customer.LastName = customerDto.LastName;
            customer.Birthday = customerDto.Birthday;

            CustomerDao.SaveOrUpdate(customer);
            return customerDto;
        }

public CustomerEditDto GetCustomer(string customerId)
        {
            CustomerEditDto dto = new CustomerEditDto();
            Customer customer =CustomerDao.FindById(customerId);
            dto.CustomerId = customer.CustomerId;
            dto.FirstName = customer.FirstName;
            dto.LastName = customer.LastName;
            dto.Birthday = customer.Birthday;
            return dto;
        }

control

 private ICustomerService mCustomerService;

        public HomeController() {
            mCustomerService = new CustomerService();
        }

        public ICustomerService CustomerService
        {
            get { return this.mCustomerService; }
            set { this.mCustomerService = value; }
        }

 public ActionResult Customer()
        {
            ViewBag.Message = "客户一览";
            Models.CustomerListModel customerListModel = new Models.CustomerListModel();
            ViewData["listData"] = mCustomerService.GetAll();

            return View("CustomerList",customerListModel);
        }
        
        public ActionResult EditCustomer(CustomerListModel listModel)
        {
            ViewBag.Message = "客户编辑";
            Models.CustomerEditModel customerEditModel = new Models.CustomerEditModel();

            if ("1" == listModel.buttonType)
            {
                CustomerEditDto editDto = mCustomerService.GetCustomer(listModel.editId);
                customerEditModel.CustomerId = editDto.CustomerId;
                customerEditModel.FirstName = editDto.FirstName;
                customerEditModel.LastName = editDto.LastName;
                customerEditModel.Birthday = editDto.Birthday;

                return View("EditCustomer", customerEditModel);
            }
            else
            {
                mCustomerService.DeleteCustomer(listModel.editId);
                return Customer();
            }
            
        }

        public ActionResult SaveCustomer(CustomerEditModel customerEditModel)
        {
            
            ViewBag.Message = "客户编辑";
             CustomerEditDto editDto = new CustomerEditDto();
            
             editDto.CustomerId = customerEditModel.CustomerId;
             editDto.FirstName = customerEditModel.FirstName;
             editDto.LastName = customerEditModel.LastName;
             editDto.Birthday = customerEditModel.Birthday;

             mCustomerService.UpdateCustomer(editDto);
             return Customer();
        }

view

@using Dto;
@model SpringProduct.Models.CustomerListModel

@{
    ViewBag.Title = "客户订单一览页面";
    CustomerListDto customerListDto = (CustomerListDto)ViewData["listData"];  
}

<h2>@ViewBag.Message</h2>

<p>
     @using (Html.BeginForm("Index", "Customer", FormMethod.Post, new { id = "form1" }))
     {
         <div class="editor-field">
           @Html.LabelFor(m => m.FirstName)
           @Html.TextBoxFor(m => m.FirstName, new { @style = "width:100px;" })
                 
          @Html.LabelFor(m => m.LastName)
          @Html.TextBoxFor(m => m.LastName, new { @style = "width:100px;" })  
          
          @Html.LabelFor(m => m.OrderDate)
          @Html.TextBoxFor(m => m.OrderDate, new { @style = "width:100px;" })         

                  <input type="submit" name="btnList" value="查询" id="btnList" />
            </div>
         
         
     }
</p>
 
<h2>一览</h2>
<div>

  @using (Html.BeginForm("EditCustomer", "Home", FormMethod.Post, new { id = "form4", name = "form4" }))
 {
        @Html.HiddenFor(m => m.editId)
        @Html.HiddenFor(m => m.buttonType)
        
 }

        @if (customerListDto.customerListDetail.Count > 0)
        {
        <table style="width:800px;border-collapse:collapse;border:none;" border="0">      
               
        <tr>
          <td style="border:1px solid gray;" >客户ID</td>
          <td style="border:1px solid gray;" >客户前名</td>
          <td style="border:1px solid gray;">客户后名</td>
          <td style="border:1px solid gray;">订单ID</td>
          <td style="border:1px solid gray;">订单日期</td>
           <td style="border:1px solid gray;">订单金额</td>
            <td style="border:1px solid gray;">操作</td>
        </tr>
        @foreach (var u in customerListDto.customerListDetail)
        {
            <tr>
                <td style="border:1px solid gray;">@u.CustomerId</td>
                <td style="border:1px solid gray;">@u.FirstName</td>
                <td style="border:1px solid gray;">@u.LastName</td>
                <td style="border:1px solid gray;">@u.OrderId</td>
                <td style="border:1px solid gray;">@u.OrderDate</td>
                <td style="border:1px solid gray;">@u.Summary</td>
                 <td style="border:1px solid gray;">
                    <input type="submit" name="btnEdit" onclick="form4.editId.value='@u.CustomerId';form4.buttonType.value='1';form4.submit();" value="编辑" id="btnEdit" />
                    <input type="submit" name="btnDel" onclick="form4.editId.value='@u.CustomerId';form4.buttonType.value='2';form4.submit();" value="删除" id="btnDel" />
                 </td>
            </tr>
        }

        </table>
        }
        
     
@model SpringProduct.Models.CustomerEditModel
           
@{

    ViewBag.Title = "CustomerEdit";
  
}

<h2>CustomerEdit</h2>

 @using (Html.BeginForm("SaveCustomer", "Home", FormMethod.Post, new { id = "form1" }))
 {
      <div class="editor-field">

           @Html.LabelFor(m => m.CustomerId)
                  @Html.TextBoxFor(m => m.CustomerId, new { @style = "width:100px;disabled:disabled" })

           @Html.LabelFor(m => m.FirstName)
                @Html.TextBoxFor(m => m.FirstName, new { @style = "width:100px;" })

           @Html.LabelFor(m => m.LastName)
                @Html.TextBoxFor(m => m.LastName, new { @style = "width:100px;" })

           @Html.LabelFor(m => m.Birthday)
                @Html.TextBoxFor(m => m.Birthday, new { @style = "width:100px;" })     

      </div>               
                  <input type="submit" name="btnList" value="保存" id="btnList" />
                  <input type="submit" name="btnAdd" onclick="flg.value='2'" value="删除" id="btnAdd" />
        
 }

  public class CustomerListModel
    {
        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "客户前名")]
        public string FirstName { get; set; }       

        public string editId { get; set; }

        public string buttonType { get; set; }

    }

    public class CustomerEditModel
    {
        public string CustomerId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "客户前名")]
        public string FirstName { get; set; }      

    }

   Global.asax:  ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory));

mvc spring nhibernate --------------------编程问答-------------------- 建议你贴在博客上。 --------------------编程问答-------------------- 什么都没有说,这么长的代码看完了头也痛了.
是想分享还是想问问题
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,