最近剛開始接觸hibernate+spring+mvc+Easyui框架,也是剛開通了博客,希望能記錄一下自己實踐出來的東西,讓其他人少走彎路。
轉讓正題,以個人淺薄的認識hibernate對於開發人員的作用主要是節省了寫sql連接數據庫的時間,而grid++report內部提供的類來看,卻是通過ajax直接連接數據庫,還是需要寫sql。感覺把grid++report用到這個框架里來的話,給人的感覺就是一匹快馬,拉了一輛舊車,雖然速度上影響不了多少,但是審美感覺上很不爽。作為一個完美主義的程序員來說,這是不允許的。然后我就賤賤的嘗試着怎么改掉它。關於hibernate+spring+mvc+Easyui框架的東西我就不說了,我只說一下grid++report的使用。
1、打開grid++report客戶端,連接數據庫並繪制報表。
2.繪制完成后使用記事本打開.grf文件,把里面的數據庫連接給清掉。
3.添加mvc的三個文件在html中載入.grf文件這個不細說,grid++的demo里多的是。
4.修改載入的數據源url
ReportViewer.Stop(); ReportViewer.DataURL = "user/load"; // var BeginDate = document.getElementById("txtBeginDate").value; // var EndDate = document.getElementById("txtEndDate").value; // var DataURL = encodeURI("xmlSummary.aspx?BeginDate=" + BeginDate + "&EndDate=" + EndDate); // ReportViewer.DataURL = DataURL; //更新查詢參數更新報表付標題,設置對應靜態框的“Text”屬性 //ReportViewer.Report.ControlByName("SubTitle").AsStaticBox.Text = "日期范圍:" + BeginDate + "至" + EndDate; ReportViewer.Start();
5.在control中編寫load方法獲取數據源,由於框架中使用hibernate獲得的數據源格式為IList<T>格式,而grid++中接收的是xml格式。所以需要方法把這給轉換一下。
度娘告訴我是這樣轉
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace Common.ToolsHelper { public class XMLHelperToList<T> where T : new() { #region 實體類轉成Xml /// <summary> /// 對象實例轉成xml /// </summary> /// <param name="item">對象實例</param> /// <returns></returns> public static string EntityToXml(T item) { IList<T> items = new List<T>(); items.Add(item); return EntityToXml(items); } /// <summary> /// 對象實例集轉成xml /// </summary> /// <param name="items">對象實例集</param> /// <returns></returns> public static string EntityToXml(IList<T> items) { //創建XmlDocument文檔 XmlDocument doc = new XmlDocument(); //創建根元素 XmlElement root = doc.CreateElement(typeof(T).Name + "s"); //添加根元素的子元素集 foreach (T item in items) { EntityToXml(doc, root, item); } //向XmlDocument文檔添加根元素 doc.AppendChild(root); return doc.InnerXml; } private static void EntityToXml(XmlDocument doc, XmlElement root, T item) { //創建元素 XmlElement xmlItem = doc.CreateElement(typeof(T).Name); //對象的屬性集 System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (pinfo != null) { //對象屬性名稱 string name = pinfo.Name; //對象屬性值 string value = String.Empty; if (pinfo.GetValue(item, null) != null) value = pinfo.GetValue(item, null).ToString();//獲取對象屬性值 //設置元素的屬性值 xmlItem.SetAttribute(name, value); } } //向根添加子元素 root.AppendChild(xmlItem); } #endregion #region Xml轉成實體類 /// <summary> /// Xml轉成對象實例 /// </summary> /// <param name="xml">xml</param> /// <returns></returns> public static T XmlToEntity(string xml) { IList<T> items = XmlToEntityList(xml); if (items != null && items.Count > 0) return items[0]; else return default(T); } /// <summary> /// Xml轉成對象實例集 /// </summary> /// <param name="xml">xml</param> /// <returns></returns> public static IList<T> XmlToEntityList(string xml) { XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch { return null; } if (doc.ChildNodes.Count != 1) return null; if (doc.ChildNodes[0].Name.ToLower() != typeof(T).Name.ToLower() + "s") return null; XmlNode node = doc.ChildNodes[0]; IList<T> items = new List<T>(); foreach (XmlNode child in node.ChildNodes) { if (child.Name.ToLower() == typeof(T).Name.ToLower()) items.Add(XmlNodeToEntity(child)); } return items; } private static T XmlNodeToEntity(XmlNode node) { T item = new T(); if (node.NodeType == XmlNodeType.Element) { XmlElement element = (XmlElement)node; System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (XmlAttribute attr in element.Attributes) { string attrName = attr.Name.ToLower(); string attrValue = attr.Value.ToString(); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo) { if (pinfo != null) { string name = pinfo.Name.ToLower(); Type dbType = pinfo.PropertyType; if (name == attrName) { if (String.IsNullOrEmpty(attrValue)) continue; switch (dbType.ToString()) { case "System.Int32": pinfo.SetValue(item, Convert.ToInt32(attrValue), null); break; case "System.Boolean": pinfo.SetValue(item, Convert.ToBoolean(attrValue), null); break; case "System.DateTime": pinfo.SetValue(item, Convert.ToDateTime(attrValue), null); break; case "System.Decimal": pinfo.SetValue(item, Convert.ToDecimal(attrValue), null); break; case "System.Double": pinfo.SetValue(item, Convert.ToDouble(attrValue), null); break; default: pinfo.SetValue(item, attrValue, null); break; } continue; } } } } } return item; } #endregion } }
然后自己獲取數據並Response到頁面上
方法如下。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO; using CLGL.Web.Controllers; using System.Text; using Wat.Common; using Domain; using System.IO.Compression; using Newtonsoft.Json; using System.Globalization; using Common.ToolsHelper; namespace CLGL.Web.Areas.GrfDemo.Controllers { public class UserController : Controller { // // GET: /GrfDemo/User/ Service.IUserManager userManage { get; set; } //User user = new User(); public ActionResult Index() { return View(); } public ActionResult Load() { IList<User> list = userManage.LoadAll(); string str = XMLHelperToList<User>.EntityToXml(list); Response.Write(str); Response.End(); return View(); } } }
運行后,結果: