First, create a new class library, named: DAO
Second, new interface: IRepository
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacerepository{ Public InterfaceIrepository<t> { voidDelete (T entity); T Get (ObjectID); ObjectSave (T entity); voidUpdate (T entity); }}
Implement the interface
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSpring.Data.NHibernate.Generic.Support;//Reference Spring.Data.NHibernate21.dllnamespacerepository{ Public classNhibernaterepository<t>: hibernatedaosupport,irepository<t> { Public voidDelete (T entity) { This. Hibernatetemplate.delete (entity); } PublicT Get (ObjectID) {return This. Hibernatetemplate.get<t>(ID); } Public ObjectSave (T entity) {return This. Hibernatetemplate.save (entity); } Public voidUpdate (T entity) { This. Hibernatetemplate.update (entity); } }}
In this case, the Hibernatedaosupport class is also inherited , and the Hibernatetemplate property in the class will be used later in the configuration file. References to this class require the introduction of a Spring.Data.NHibernate21.dll file.
Iii. Configuration Files
<?XML version= "1.0" encoding= "Utf-8"?><Objectsxmlns= "Http://www.springframework.net"xmlns:db= "Http://www.springframework.net/database"> <!--For us to configure data access in other applications such as: we add a database connection to the Mvcdemo project, this configuration: <configuration> <databasesetting s> <add key= "Db.datasource" value= "Lippor-pc\lippor"/> <add key= "Db.user" value= "sa"/> <add key= "Db.password" value= "123"/> <add key= "db.database" value= "Spring Netdb "/> </databaseSettings> </configuration> can pass the data over - <Objecttype= "Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.core"> < Propertyname= "configsections"value= "Databasesettings"/> </Object> <!--related configuration of database and NHibernate - <Db:providerID= "Dbprovider"provider= "SqlServer-2.0"connectionString= "Server=${db.datasource};d Atabase=${db.database};uid=${db.user};p Wd=${db.password};"/> <!--sessionfactory objects, which include some of the more important properties - <ObjectID= "Nhibernatesessionfactory"type= "Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21"> <!--For configuration of database connections, use the settings in Dbprovider directly, so that you do not need to provide Hibernate with connection strings and drivers - < Propertyname= "Dbprovider"ref= "Dbprovider"/> <!--The assembly containing the mapped file, the HBM assembly name that needs to be analyzed - < Propertyname= "Mappingassemblies"> <List> <value>Model</value> </List> </ Property> < Propertyname= "Hibernateproperties"> <Dictionary> <entryKey= "Hibernate.connection.provider"value= "NHibernate.Connection.DriverConnectionProvider"/> <entryKey= "dialect"value= "NHibernate.Dialect.MsSql2008Dialect"/> <entryKey= "Hibernate.connection.driver_class"value= "NHibernate.Driver.SqlClientDriver"/> <entryKey= "Use_outer_join"value= "true"/> <entryKey= "Show_sql"value= "false"/> <!--Auto-Build table (reverse map) - <entryKey= "Hbm2ddl.auto"value= "Update"/> <entryKey= "Adonet.batch_size"value= "Ten"/> <entryKey= "Command_timeout"value= "$"/> <!--explicitly enable level two caching - <entryKey= "Cache.use_second_level_cache"value= "true"/> <!--start the query cache - <entryKey= "Cache.use_query_cache"value= "false"/> <entryKey= "Query.substitutions"value= "True 1, False 0, yes ' Y ', no ' N"/> <entryKey= "Proxyfactory.factory_class"value= "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/> </Dictionary> </ Property> <!--declarative transaction integration with Spring - < Propertyname= "Exposetransactionawaresessionfactory"value= "true" /> </Object> <ObjectID= "Hibernatetemplate"type= "Spring.Data.NHibernate.Generic.HibernateTemplate"> < Propertyname= "Sessionfactory"ref= "Nhibernatesessionfactory" /> < Propertyname= "Templateflushmode"value= "Auto" /> < Propertyname= "Cachequeries"value= "true" /> </Object> <ObjectID= "Dao.users"type= "repository.nhibernaterepository< Model.users>, Repository "> < Propertyname= "Hibernatetemplate"ref= "Hibernatetemplate"/> </Object> <ObjectID= "Dao.roles"type= "repository.nhibernaterepository< Model.roles>, Repository "> < Propertyname= "Hibernatetemplate"ref= "Hibernatetemplate"/> </Object> <ObjectID= "Dao.permission"type= "repository.nhibernaterepository< Model.permission>, Repository "> < Propertyname= "Hibernatetemplate"ref= "Hibernatetemplate"/> </Object></Objects>
There is such a comment in teacher Liu's podcast:
Thesession control in NHibernate depends on sessionfactory,spring.net providing the Localsessionfactoryobject class for unified management sessionfactory. Where the Mappingassemblies property is the name of the entity assembly, multiple names can be filled in. Hibernateproperties is configured for NHibernate, the dialect attribute is the dialect of the database, because it is a SQL Server 2K database, so NHibernate.Dialect.MsSql2000Dialect is used. The Proxyfactory.factory_class property is a deferred-loaded proxy class driver that must be configured in version NHibernate 2.1. The Hbm2ddl.auto property is configured to reverse the mapping table, and after we configure it for update, NHibernate helps us automatically generate the tables in the database based on the structure of the entity. "
Finally, set the properties of the configuration file:
Ssh.net data Access Object (DAO)