三层架构应用——无限树

今天跟大家来分享一下我最近写的一个关于显示部门的树形下拉列表。我是用三层架构来实现的,这也是加深大家对三层架构的宏观认识。
好,让我们开始今天的路程。
首先是建立数据表

 

CREATE TABLE [dbo].[MOA_Department](
 CREATE TABLE [dbo].[MOA_Department](
 [departmentId] [int] IDENTITY(1,1) NOT NULL,
 [departmentName] [nvarchar](50) NOT NULL,
 [parentDepartmentId] [int] NOT NULL,
 
 CONSTRAINT [PK_MOA_Department] PRIMARY KEY CLUSTERED
(
 [departmentId] ASC
)
)

 

其中departmentId是表的主键,代表部门的id;departmentName代表部门的名称;parentDepartmentId代表部门上一级的部门id。

建立存储过程

首先建立获取最高部门的存储过程

 

Create PROCEDURE proc_GetRootNodeDepartment
 
AS
BEGIN
  select departmentId,departmentName from MOA_Department where parentDepartmentId=0
END

其中部门父id为0的就代表这是根部门。

然后建立获取子部门的存储过程

Create PROCEDURE proc_GetChildNodeDepartment
  @parentId int
AS
BEGIN
  select departmentId,departmentName from MOA_Department where  parentDepartmentId=@parentId
END

Create PROCEDURE proc_GetRootNodeDepartment
 
AS
BEGIN
  select departmentId,departmentName from MOA_Department where parentDepartmentId=0
END

 

 

其中部门父id为0的就代表这是根部门。

然后建立获取子部门的存储过程

 

Create PROCEDURE proc_GetChildNodeDepartment
  @parentId int
AS
BEGIN
  select departmentId,departmentName from MOA_Department where  parentDepartmentId=@parentId
ENDCreate PROCEDURE proc_GetChildNodeDepartment
  @parentId int
AS
BEGIN
  select departmentId,departmentName from MOA_Department where  parentDepartmentId=@parentId
END

 

这个存储过程代表获取指定部门的下一级部门。

建立Model层

 

namespace Model
{
    public class Department
    {
        private int departmentId;

        public int DepartmentId
        {
            get { return departmentId; }
            set { departmentId = value; }
        }

        private string departmentName;

        public string DepartmentName
        {
            get { return departmentName; }
            set { departmentName = value; }
        }

        private int parentDepartment;

        public int ParentDepartment
        {
            get { return parentDepartment; }
            set { parentDepartment = value; }
        }

        private int employeeId;

        public int EmployeeId
        {
            get { return employeeId; }
            set { employeeId = value; }
        } 
    }
}

 

这里就不多说了,字段是跟数据表一一对应的。

建立DB层(直接与数据库打交道的公共类库)

 

namespace DB
{
    public class SqlHelper
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //获取连接字符串

        private static void parComm(SqlConnection conn, SqlCommand com, SqlParameter[] par, string sqlStr, CommandType type)
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.CommandText = sqlStr;
            com.CommandType = type;
            com.Connection = conn;

            if (par != null)
            {
                foreach (SqlParameter p in par)
                {
                    com.Parameters.Add(p);
                }
            }
        }

        public static SqlDataReader GetReader(string sqlStr, SqlParameter[] par, CommandType type) //用datareader来获取数据库中的数据,然后传递给DAL层
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand comm = new SqlCommand();

            parComm(conn, comm, par, sqlStr, type);

            return comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
    }
}

 

建立DAL层

 

namespace OADAL
{
    public class DepartmentDal
    {
        public Department GetRootDepartment()  //获得根部门
        {
            string sqlStr = "proc_GetRootNodeDepartment";  //设定存储过程名称

            SqlDataReader dr = SqlHelper.GetReader(sqlStr, null, CommandType.StoredProcedure); //调用存储过程,获得datareader

            Department item = new Department();

            if (dr.Read())
            {
                if (dr["departmentId"].ToString() != "")
                {
                    item.DepartmentId = Convert.ToInt32(dr["departmentId"].ToString());
                }

                if (dr["departmentName"] != null && dr["departmentName"].ToString() != string.Empty)
                {
                    item.DepartmentName = dr["departmentName"].ToString().Trim();
                }


            }
            else
            {
                dr.Close();
                return null;
            }

            dr.Close();
            return item;  //用自定义实体类传递到BLL层

        }

        public List<Department> GetChildNoteDepartment(int parentId)  //获得指定部门的下一级部门
        {
            string sqlStr = "proc_GetChildNodeDepartment";

            SqlParameter[] par = {
                                     new SqlParameter("@parentId",parentId)
                                 };

            List<Department> itemList = new List<Department>();

            SqlDataReader dr = SqlHelper.GetReader(sqlStr, par, CommandType.StoredProcedure);

            while (dr.Read())
            {
                Department item = new Department();

                if (dr["departmentId"].ToString() != "")
                {
                    item.DepartmentId = Convert.ToInt32(dr["departmentId"].ToString());
                }

                if (dr["departmentName"] != null && dr["departmentName"].ToString() != string.Empty)
                {
                    item.DepartmentName = dr["departmentName"].ToString().Trim();
                }

                itemList.Add(item);
            }

            if (itemList.Count > 0)
            {
                dr.Close();
                return itemList;
            }

            else
            {
                dr.Close();
                return null;
            }
        }
    }
}


建立BLL层

 

namespace OABLL
{
    public class DepartmentBll
    {
        
        public Department GetRootDepartment()  //获得根部门
        {
            DepartmentDal manager = new DepartmentDal();
            return manager.GetRootDepartment();
        }

        public List<Department> GetChildNoteDepartment(int parentId)  //获得指定部门的下一级部门
        {
            DepartmentDal manager = new DepartmentDal();
            return manager.GetChildNoteDepartment(parentId);
        }
    }
}


建立表现层

建立一个网页,从工具箱拖一个dropdownlist到页面上,取名department。编辑code-behind。

 

public partial class DepartmentManager_EditDepartment : System.Web.UI.Page
{

    private DepartmentBll manager = new DepartmentBll();  //实例化BLL层

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bingDepartment();
        }
    }

    private void bingDepartment()
    {
        this.department.Items.Clear();

        ListItem liFirst = new ListItem();
        liFirst.Text = "请选择";
        liFirst.Value = "0";
        this.department.Items.Add(liFirst);

        Department item = new Department();
        item = manager.GetRootDepartment();  //获取根部门

        ListItem li = new ListItem();
        li.Text = ">>" + item.DepartmentName;
        li.Value = item.DepartmentId.ToString();

        department.Items.Add(li);  //添加跟部门到dropdownlist

        addDepartmentChild(item.DepartmentId, 2);  //绑定子部门
    }

    private void addDepartmentChild(int parentId, int width)  //绑定指定部门的下一级部门,width代表空多少格
    {
        List<Department> itemList = new List<Department>(); //实例化model
        itemList = manager.GetChildNoteDepartment(parentId);  //获取指定部门的下一级部门
        string str = "";

        for (int i = 0; i < width; i++)  //指定空多少格
        {
            str += "&nbsp;";
        }

        if (itemList != null)
        {
            foreach (Department item in itemList)  //绑定部门到dropdownlist
            {
                ListItem li = new ListItem();
                li.Value = item.DepartmentId.ToString();
                li.Text = HttpUtility.HtmlDecode(str) + ">>" + item.DepartmentName;
                this.department.Items.Add(li);

                addDepartmentChild(item.DepartmentId, (width + 2));

            }
        }
    }
}

 

编译运行程序,就看到一个树形的下拉列表菜单。

posted on 2010-01-01 17:49  谭英智  阅读(465)  评论(0编辑  收藏  举报

导航