Spring. NET + nhib.pdf + ASP. mvc4.0 + easyui practice (1)

Source: Internet
Author: User

Spring. NET + nhib.pdf + ASP. MVC + easyui practices

Directory

Spring. NET + nhib.pdf + ASP. MVC + easyui practices (1)

Spring. NET + nhib.pdf + ASP. MVC + easyui practices (2)

Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (3)

Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (4)

Tools used: vs2012 and PostgreSQL

1. Create a New mvc4.0 project named financewebsys and select. NET Framework 4.5.

2. Create a database, Database Name: financewebdb, and table Creation

CREATE TABLE T_user_depinfo(  id serial NOT NULL PRIMARY KEY,  depid int,  depname varchar(50))

3. Add a new project to the Project

Model: model; database layer: idao, nhibernatedao; business logic layer: ibll, BLL

4.01model

(1) Create userdepinfo. CS

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FinanceWebSys.Model{    public class UserDepInfo    {        /// <summary>        /// ID        /// </summary>        public virtual int Id { get; set; }        /// <summary>        /// DepID        /// </summary>        public virtual int DepID { get; set; }        /// <summary>        /// DepName        /// </summary>        public virtual string DepName { get; set; }    }}

(2) Create the nhib1_ ing file userdepinfo. HBM. xml

You can use codesmith, mygeneration, and other tools to generate data, or manually

(Note: all XML mappings need to define the XML namespace. The XSD file can be a distribution package of nhib.pdfSRC> nhibernate-mapping.xsd.

TIPS: To enable the smart prompt function for editing ing files and configuration files. XSDCopy files<Vs 2012 installation directory> \ XML \ Schemas.)

<?xml version="1.0" encoding="utf-8" ?>

(Note: Right-click the user. HBM. xml file and choose --> to set [generate operation] to embedded resource. The Assembly must be named accurately)

5. Write the data layer interface idao and reference the model (use nuget to add reference)

(1) Use the repository mode to write irepository. CS (use ilist or iqueryable for more information)

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace financewebsys. idao {public interface irepository <t> where T: class {// <summary> /// obtain the object /// </Summary> /// <Param name = "ID"> </param> /// <returns> </returns> t get (Object ID ); /// <summary> /// obtain the object /// </Summary> /// <Param name = "ID"> </param> /// <returns> </returns> t load (Object ID ); /// <summary> /// insert object /// </Summary> /// <Param name = "entity"> </param> /// <returns> </returns> Object save (T entity ); /// <summary> /// modify the object /// </Summary> /// <Param name = "entity"> </param> void Update (T entity ); /// <summary> /// modify or save the object // </Summary> /// <Param name = "entity"> </param> void saveorupdate (T entity ); /// <summary> /// delete an object /// </Summary> /// <Param name = "ID"> </param> void Delete (Object ID ); /// <summary> /// delete an object set /// </Summary> /// <Param name = "idlist"> </param> void Delete (ilist <object> idlist ); /// <summary> /// obtain all sets /// </Summary> /// <returns> </returns> ilist <t> loadall (); /// <summary> /// retrieve all sets by PAGE /// </Summary> /// <Param name = "count"> total number of records </param> /// <Param name = "pageindex"> page number </param> // <Param name = "pagesize"> page size </param> /// <returns> </returns> ilist <t> loadallwithpage (out long count, int pageindex, int pagesize );}}

 

(2) write the iuserdepinforepository. CS interface and add the loadallbypage method.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using FinanceWebSys.Model;namespace FinanceWebSys.IDao{    public interface IUserDepInfoRepository : IRepository<UserDepInfo>    {        IList<UserDepInfo> LoadAllByPage(out long total, int page, int rows, string order, string sort);        IList<UserDepInfo> Get(string DepName);    }}

6. compile the data layer to implement nhibernatedao and add reference model, idao, spring. Data. nhibernate32 (other Dependencies will be automatically added with nuget)

(1) Compile repositorybase. CS

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using FinanceWebSys.IDao;using Spring.Data.NHibernate.Generic.Support;namespace FinanceWebSys.NhibernateDao{    public abstract class RepositoryBase<T>:HibernateDaoSupport,IRepository<T> where T:class    {        public virtual object Save(T entity)        {            return this.HibernateTemplate.Save(entity);        }        public virtual T Get(object id)        {            return this.HibernateTemplate.Get<T>(id);        }        public virtual T Load(object id)        {            return this.HibernateTemplate.Load<T>(id);        }        public virtual IList<T> LoadAll()        {            return this.HibernateTemplate.LoadAll<T>();           }        public virtual void Update(T entity)        {            this.HibernateTemplate.Update(entity);        }        public virtual void Delete(object id)        {            var entity=this.HibernateTemplate.Get<T>(id);            if (entity == null)            {                return;            }            this.HibernateTemplate.Delete(entity);        }        public virtual void Delete(IList<object> idList)        {            foreach(var item in idList)            {                var entity = this.HibernateTemplate.Get<T>(item);                if(entity==null)                {                    return;                }                this.HibernateTemplate.Delete(entity);            }        }        public virtual void SaveOrUpdate(T entity)        {            this.HibernateTemplate.SaveOrUpdate(entity);        }        public virtual IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)        {            IList<T> result = this.LoadAll();            count = result.LongCount();            return result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();        }    }}

 

(2) Compile userdepinforepository. CS

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using FinanceWebSys.IBLL;namespace FinanceWebSys.BLL{    public abstract class GenericManager<T>:IGenericManager<T> where T:class    {        public FinanceWebSys.IDao.IRepository<T> CurrentRepository { get; set; }        public virtual T Get(object id)        {            if (id == null)            {                return null;            }            return this.CurrentRepository.Get(id);        }        public virtual T Load(object id)        {            if(id==null)            {                return null;            }            return this.CurrentRepository.Load(id);        }        public virtual object Save(T entity)        {            if (entity == null)            {                return null;            }            return this.CurrentRepository.Save(entity);        }        public virtual void Update(T entity)        {            if (entity == null)            {                return;            }            this.CurrentRepository.Update(entity);        }        public virtual void Delete(object id)        {            if (id == null)            {                return;            }            this.CurrentRepository.Delete(id);        }        public virtual IList<T> LoadAll()        {            return this.CurrentRepository.LoadAll();        }        public virtual IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)        {            return this.CurrentRepository.LoadAllWithPage(out count, pageIndex, pageSize);        }        public virtual void Delete(IList<object> idList)        {            if (idList == null || idList.Count == 0)            {                return;            }            this.CurrentRepository.Delete(idList);        }        public virtual void SaveOrUpdate(T entity)        {            if (entity == null)            {                return;            }            this.CurrentRepository.SaveOrUpdate(entity);        }     }}

(2) Write userdepinfomanager. CS

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using FinanceWebSys.Model;using FinanceWebSys.IBLL;namespace FinanceWebSys.BLL{    public class UserDepInfoManager:GenericManager<UserDepInfo>,IUserDepInfoManager    {        public IList<UserDepInfo> LoadAllByPage(out long total, int page, int rows, string order, string sort)         {            return ((FinanceWebSys.IDao.IUserDepInfoRepository)(this.CurrentRepository)).LoadAllByPage(out total, page, rows, order, sort);        }    }}

 

The data layer and logic service layer have been basically written here, And the configuration problem is coming next. This part is quite troublesome. I will talk about it in the next blog.

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.