标签:style class java http tar ext
产品的增删改查
l Model: Products.cs
public class Products
{
public System.Guid Id { get; set; }
public System.String Name { get; set; }
public System.String ImagePath { get; set; }
public System.String Msg { get; set; }
public System.Guid CategoryId { get; set; }
}
l 三层架构:
DAL层:
public Products ToModel(DataRow row)
{
Products model = new Products();
model.Name = (System.String)SqlHelper.FromDBNull(row["Name"]);
model.Id = (System.Guid)SqlHelper.FromDBNull(row["Id"]);
model.ImagePath = (System.String)SqlHelper.FromDBNull(row["ImagePath"]);
model.Msg = (System.String)SqlHelper.FromDBNull(row["Msg"]);
model.CategoryId = (System.Guid)SqlHelper.FromDBNull(row["CategoryId"]);
return model;
}
//ProductCaName类别名称专用ToModel
public ProductCaName ToModelCaName(DataRow row)
{
ProductCaName model = new ProductCaName();
model.Id = (System.Guid)SqlHelper.FromDBNull(row["Id"]);
model.Name = (System.String)SqlHelper.FromDBNull(row["Name"]);
model.CategoryId = (System.Guid)SqlHelper.FromDBNull(row["CategoryId"]);
model.CategoryName = (System.String)SqlHelper.FromDBNull(row["CategoryName"]);
return model;
}
//ProductCaName类别名称专用ListAll
public ProductCaName[] ListAllCaName()
{
DataTable table = SqlHelper.ExecuteReader(@"select p.Id as Id,p.Name as Name,p.CategoryId as CategoryId
,c.Name as CategoryName from T_Products p left join
T_ProductCategories c on p.CategoryId=c.Id");
ProductCaName[] items = new ProductCaName[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
ProductCaName item = ToModelCaName(table.Rows[i]);
items[i] = item;
}
return items;
}
public Products[] ListAll()
{
DataTable table = SqlHelper.ExecuteReader("select * from T_Products");
Products[] items = new Products[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
Products item = ToModel(table.Rows[i]);
items[i] = item;
}
return items;
}
public Products GetById(Guid id)
{
DataTable table = SqlHelper.ExecuteReader("select * from T_Products where Id=@Id", new SqlParameter("@Id", id));
if (table.Rows.Count <= 0)
{
return null;
}
else if (table.Rows.Count == 1)
{
return ToModel(table.Rows[0]);
}
else
{
throw new Exception();
}
}
public void Insert(Products model)
{
SqlHelper.ExecuteNonQuery(@"INSERT INTO T_Products
(Id,Name,ImagePath,Msg,CategoryId)
values (@Id,@Name,@ImagePath,@Msg,@CategoryId)"
, new SqlParameter("@Id", SqlHelper.ToDBValue(model.Id))
, new SqlParameter("@Name", SqlHelper.ToDBValue(model.Name))
, new SqlParameter("@ImagePath", SqlHelper.ToDBValue(model.ImagePath))
, new SqlParameter("@Msg", SqlHelper.ToDBValue(model.Msg))
, new SqlParameter("@CategoryId", SqlHelper.ToDBValue(model.CategoryId))
);
}
public void DeleteById(Guid id)
{
SqlHelper.ExecuteNonQuery("delete from T_Products where Id=@Id"
, new SqlParameter("@Id", id));
}
public void Update(Products model)
{
SqlHelper.ExecuteNonQuery(@"Update T_Products set Name=@Name,
ImagePath=@ImagePath,
Msg=@Msg,
CategoryId=@CategoryId where Id=@Id"
, new SqlParameter("@Name", SqlHelper.ToDBValue(model.Name))
, new SqlParameter("@ImagePath", SqlHelper.ToDBValue(model.ImagePath))
, new SqlParameter("@Msg", SqlHelper.ToDBValue(model.Msg))
, new SqlParameter("@CategoryId", SqlHelper.ToDBValue(model.CategoryId))
, new SqlParameter("@Id", model.Id));
}
//获得指定Id的图片路径(ImagePath)
public string GetImagePathById(Guid id)
{
Products products = GetById(id);
return products.ImagePath;
}
}
BLL层:
public class ProductsBLL
{
public Products[] ListAll()
{
ProductsDAL productsdal = new ProductsDAL();
return productsdal.ListAll();
}
//显示类别名字专用
public ProductCaName[] ListAllCaName()
{
ProductsDAL productsdal = new ProductsDAL();
return productsdal.ListAllCaName();
}
//根据指定Id返回Model
public Products GetById(Guid id)
{
ProductsDAL productsdal = new ProductsDAL();
return productsdal.GetById(id);
}
public Products ToModel(DataRow model)
{
ProductsDAL productsdal = new ProductsDAL();
return productsdal.ToModel(model);
}
public void Insert(Products model)
{
ProductsDAL productsdal = new ProductsDAL();
productsdal.Insert(model);
}
//产品更新
public void Update(Products model)
{
ProductsDAL productsdal = new ProductsDAL();
productsdal.Update(model);
}
//获得指定Id的图片文件路径
public string GetImagePathById(Guid id)
{
ProductsDAL productsdal = new ProductsDAL();
return productsdal.GetImagePathById(id);
}
public void DeleteById(Guid id)
{
ProductsDAL productsdal = new ProductsDAL();
productsdal.DeleteById(id);
}
}
UI层:
模板:
ProductList.htm
#parse("Admin/Head.htm")
<form action="../Admin/ProductList.ashx" method="post">
<input type="hidden" name="IsPostBack"/>
<p><a href="ProductEdit.ashx?Action=AddNew">新增产品</a></p>
<table>
<thead>
<tr><td>编辑 </td><td>删除 </td><td>名称 </td><td>类别名称 </td></tr>
</thead>
<tbody>
#foreach($p in $Model.Products)
<tr><td><a href="ProductEdit.ashx?Action=Edit&Id=$p.Id">编辑</a></td>
<td><a href="ProductEdit.ashx?Action=Delete&Id=$p.Id" >删除</a></td>
<td><a href="">$p.Name</a></td>
<td>$p.CategoryName</td></tr>
#end
</tbody>
</table>
</form>
#parse("Admin/Foot.htm")
ProductEdit.htm
#parse("Admin/Head.htm")
<script type="text/javascript" src="../js/ckeditor/ckeditor.js"></script>
<form method="post" action="../Admin/ProductEdit.ashx" enctype="multipart/form-data">
<input type="hidden" name="IsPostBack" value="true"/>
<input type="hidden" name="Action" value="$Model.Action"/>
<input type="hidden" name="Id" value="$Model.Product.Id"/>
<p>名称:<input type="text" name="Name" value="$Model.Product.Name"/></p>
<p>分类:<select name="CategoryId">
#foreach($c in $Model.Categories)
#if($c.Id==$Model.Product.CategoryId)
<option value="$c.Id" selected>$c.Name</option>
#else
<option value="$c.Id">$c.Name</option>
#end
#end
</select>
</p>
<p>产品图片:<input type="file" name="ProductImage"/>
#if($Model.Action=="Edit")
<img src="../$Model.Product.ImagePath" width="150" height="100" />
#end
</p>
<p>产品描述:<textarea id="Msg" name="Msg">$Model.Product.Msg</textarea></p>
<script type="text/javascript">
var msg = document.getElementById("Msg");
CKEDITOR.replace(msg);
</script>
<p><input type="submit" value="保存" /></p>
</form>
#parse("Admin/Foot.htm")
一般处理程序的处理逻辑:
l ProductList.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
// var data = new { Title = "管理员产品管理" };
ProductsBLL productsbll = new ProductsBLL();
ProductCaName[] products = productsbll.ListAllCaName();
var data = new { Title = "管理员产品管理", Products = products };
string html = CommonHelper.RenderHtml("Admin/ProductList.htm", data);
context.Response.Write(html);
}
l ProductEdit.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
ProductCategoriesBLL productCa=new ProductCategoriesBLL();
ProductCategories[] categories = productCa.ListAll();
bool isPostBack = !string.IsNullOrEmpty(context.Request["IsPostBack"]);
string action = context.Request["Action"];
if (isPostBack)
{
//新增保存
if (action == "AddNew")
{
//数据的合法性检查(服务器端和客户端都要做)数据格式合法性、是否为空、
//todo
string name = context.Request["Name"];
System.Guid categoryId = new Guid(context.Request["CategoryId"]);//将字符串转换为Guid的形式
/*------------------------图片的上传处理-------------------------------------------*/
//要设定enctype="multipart/form-data"
HttpPostedFile productImg = context.Request.Files["ProductImage"];//获得浏览器上传的文件信息
//productImg.SaveAs("d:/");//图片要保存到项目的文件夹(子文件夹)下,才可以通过web来访问图片
//为了不把路径写死,方便移植采用下面方法
string filename =
DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(productImg.FileName);//有bug的,一毫秒内多个人上传多个文件
//保证无bug的方法是用Guid作为文件名
productImg.SaveAs(context.Server.MapPath("~/UI/uploadfile/" + filename));//文件保存路径//得到当前时间的年月日小时分秒毫秒格式
//productImg.SaveAs("");//图片要保存到项目的文件夹或者子文件夹下(服务器硬盘上),才可以通过web来访问图片
//不能将图片保存路径写死,如果项目移植操作不便
//mappath可以把一个相对于网站根目录的文件或者文件夹的路径转换为在服务器磁盘上的物理全路径
//获得浏览器上传的文件信息(input type="file" 上传上来的信息)
//产品描述
/*------------------------图片的上传处理完成-------------------------------------------*/
string msg = context.Request["Msg"];
ProductsBLL productsbll = new ProductsBLL();
System.Guid id = System.Guid.NewGuid();
/*----------------------定义DataRow并赋值-------------------------*/
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(Guid)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("ImagePath", typeof(string)));
dt.Columns.Add(new DataColumn("CategoryId", typeof(Guid)));
dt.Columns.Add(new DataColumn("Msg", typeof(string)));
DataRow dr;
dr = dt.NewRow();
dr["Name"] = name;
dr["ImagePath"] = "uploadfile/" + filename;
dr["CategoryId"] = categoryId;
dr["Msg"] = msg;
dr["Id"] = id;
dt.Rows.Add(dr);
/*--------------------------------------------------------------------*/
Products products = productsbll.ToModel(dr);
productsbll.Insert(products);
context.Response.Redirect("ProductList.ashx");
}
else if (action == "Edit")//编辑保存
{
System.Guid id = new Guid(context.Request["Id"]);
string name = context.Request["Name"];
System.Guid categoryId = new Guid(context.Request["CategoryId"]);
/*----------------------图片处理-------------------------*/
HttpPostedFile productImg = context.Request.Files["ProductImage"];
/*--------------------------------------------------*/
string msg=context.Request["Msg"];
if (CommonHelper.HasFile(productImg))//用户选择了新的图片
{
/*----------------------定义DataRow并赋值-------------------------*/
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(Guid)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("ImagePath", typeof(string)));
dt.Columns.Add(new DataColumn("CategoryId", typeof(Guid)));
dt.Columns.Add(new DataColumn("Msg", typeof(string)));
DataRow dr;
dr = dt.NewRow();
dr["Name"] = name;
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfffffff")
+ Path.GetExtension(productImg.FileName);//有bug的,一毫秒内多个人上传多个文件
productImg.SaveAs(context.Server.MapPath("~/UI/uploadfile/" + filename));
dr["ImagePath"] = "uploadfile/" + filename;
dr["CategoryId"] = categoryId;
dr["Msg"] = msg;
dr["Id"] = id;
dt.Rows.Add(dr);
/*--------------------------------------------------------------------*/
ProductsBLL productsbll = new ProductsBLL();
Products products = productsbll.ToModel(dr);
productsbll.Update(products);
context.Response.Redirect("ProductList.ashx");
}
else//用户没有选择新图片,还用之前的产品图片
{
/*----------------------定义DataRow并赋值-------------------------*/
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(Guid)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("ImagePath", typeof(string)));
dt.Columns.Add(new DataColumn("CategoryId", typeof(Guid)));
dt.Columns.Add(new DataColumn("Msg", typeof(string)));
DataRow dr;
dr = dt.NewRow();
dr["Name"] = name;
ProductsBLL productsbll = new ProductsBLL();
dr["ImagePath"] = productsbll.GetImagePathById(id);
dr["CategoryId"] = categoryId;
dr["Msg"] = msg;
dr["Id"] = id;
dt.Rows.Add(dr);
/*--------------------------------------------------------------------*/
Products products = productsbll.ToModel(dr);
productsbll.Update(products);
context.Response.Redirect("ProductList.ashx");
}
}
else
{
context.Response.Write("Action错误!" + action);
}
}
else
{
if (action == "AddNew")
{
var data = new
{
Title = "新增产品",
Action = action,
Product = new { Id = 0, Name = "", CategoryId = 0, Msg = "" },
Categories = categories
};
string html = CommonHelper.RenderHtml("Admin/ProductEdit.htm", data);
context.Response.Write(html);
}
else if(action=="Edit")
{
System.Guid id = new Guid(context.Request["Id"]);
ProductsBLL productsbll = new ProductsBLL();
Products products = productsbll.GetById(id);
var data = new {Title="编辑产品",
Action=action,
Product=products,
Categories = categories};
string html = CommonHelper.RenderHtml("Admin/ProductEdit.htm", data);
context.Response.Write(html);
}
else if (action == "Delete")
{
ProductsBLL productsbll = new ProductsBLL();
System.Guid id = new Guid(context.Request["Id"]);
productsbll.DeleteById(id);
context.Response.Write("Id=" + id + "的产品删除成功!");
context.Response.Redirect("ProductList.ashx");
}
else
{
context.Response.Write("Action错误!" + action);
}
}
}
20140625三层架构实现产品的增删改查,布布扣,bubuko.com
标签:style class java http tar ext
原文地址:http://www.cnblogs.com/seclusively/p/3807207.html