public class BaseController<T> : Controller where T : class, new() { BaseDal<T> baseDal = new BaseDal<T>(); #region 通用基础方法 /// <summary> /// 新增 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual ActionResult Insert(T t) { int res = baseDal.Insert(t); return Content(res.ToString()); } /// <summary> /// 批量新增 /// </summary> /// <param name="list"></param> /// <returns></returns> public virtual ActionResult InsertBatch(List<T> list) { bool res = baseDal.InsertBatch(list); return Content(res.ToString()); } /// <summary> /// 修改 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual ActionResult Update(T t) { bool res = baseDal.Update(t); return Content(res.ToString()); } /// <summary> /// 更新非空字段部分字段 默认第一个字段为更新主键 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual ActionResult UpdateNotNull(T t) { bool res = baseDal.UpdateNotNull(t); return Content(res.ToString()); } /// <summary> /// 删除 /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual ActionResult Delete(T t) { bool res = baseDal.Delete(t); return Content(res.ToString()); } /// <summary> /// 批量删除 /// </summary> /// <param name="list"></param> /// <returns></returns> public virtual ActionResult DeleteBatch(List<T> list) { bool res = baseDal.DeleteBatch(list); return Content(res.ToString()); } /// <summary> /// 查询一个实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual ActionResult Get(string id) { T res = baseDal.Get(id); return Json(res, JsonRequestBehavior.AllowGet); } /// <summary> /// 查询全部 /// </summary> /// <returns></returns> public virtual ActionResult GetListAll() { List<T> res = baseDal.GetListAll(); return Json(res, JsonRequestBehavior.AllowGet); } #endregion #region 示例方法 需重写 /// <summary> /// 获取分页数据 /// Dapper扩展分页 /// </summary> /// <param name="page"></param> /// <param name="limit"></param> /// <param name="Id"></param> /// <returns></returns> public virtual ActionResult GetPage(int page, int limit, string Id) { long allRowsCount = 0; List<T> list = baseDal.GetPage(page, limit, out allRowsCount); LayUIDataResult<T> layUIDataResult = new LayUIDataResult<T>() { code = 0, count = allRowsCount, msg = "", data = list }; return Json(layUIDataResult, JsonRequestBehavior.AllowGet); } /// <summary> /// SQL分页示例 /// </summary> /// <param name="page"></param> /// <param name="limit"></param> /// <param name="Id"></param> /// <returns></returns> public virtual ActionResult GetPageListForSQL(int page, int limit, string Id) { JObject jObject = new JObject(); PageDataView<T> res = baseDal.GetPageListForSQL(jObject); LayUIDataResult<T> layUIDataResult = new LayUIDataResult<T>() { code = 0, count = res.TotalNum, msg = "", data = res.Items.ToList() }; return Json(layUIDataResult, JsonRequestBehavior.AllowGet); } /// <summary> /// SQL分页示例 主要用于 MVC控制器 Layui /// </summary> /// <param name="collection"></param> /// <returns></returns> [HttpPost] public virtual ActionResult GetPageListForSQL(FormCollection collection) { T t = new T(); TryUpdateModel<T>(t, collection); int page = Convert.ToInt32(collection["page"]); int limit = Convert.ToInt32(collection["limit"]); PageDataView<T> res = baseDal.GetPageListForSQL(page, limit, t); LayUIDataResult<T> layUIDataResult = new LayUIDataResult<T>() { code = 0, count = res.TotalNum, msg = "", data = res.Items.ToList() }; return Json(layUIDataResult, JsonRequestBehavior.AllowGet); } #endregion }