Spring.Net 是一个很强大的框架,在java里非常的火,NHibernate也是从java里移植过来的.不过和Java下面的用法有所区别.下面我们以一个实例来说说他们在.net下的用法.
新建一个项目:名字叫SpringHiberate.
首先,我们修改我们的配置文件web.config:
<?
xml version="1.0" encoding="utf-8"
?>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
configuration
>
<
configSections
>
<
sectionGroup
name
="spring"
>
<
section
name
="context"
type
="Spring.Context.Support.WebContextHandler, Spring.Web"
/>
<
section
name
="objects"
type
="Spring.Context.Support.DefaultSectionHandler, Spring.Core"
/>
</
sectionGroup
>
<
section
name
="SpringOverrideProperty"
type
="System.Configuration.NameValueSectionHandler"
/>
<
section
name
="nhibernate"
type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</
configSections
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
SpringOverrideProperty
>
<
add
key
="DbProvider.ConnectionString"
value
="Data Source=192.168.1.21;Database=FastSpring20;User ID=sa;Password=oilchem2007;Trusted_Connection=False"
/>
<
add
key
="SystemInit.IsDebug"
value
="true"
/>
<
add
key
="SystemInit.Level"
value
="4"
/>
</
SpringOverrideProperty
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<!--
Spirng.Net 配置
-->
<
spring
>
<
context
>
<
resource
uri
="config://spring/objects"
/>
<
resource
uri
="assembly://SpringHiberate/SpringHiberate/spring_bean_dao.xml"
/>
</
context
>
<
objects
xmlns
="http://www.springframework.net"
/>
</
spring
>
<
appSettings
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
connectionStrings
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
system.web
>
<
compilation
debug
="true"
/>
<
authentication
mode
="Windows"
/>
<
httpModules
>
<
add
name
="Spring"
type
="Spring.Context.Support.WebSupportModule, Spring.Web"
/>
</
httpModules
>
<
httpHandlers
>
<
add
verb
="*"
path
="*.aspx"
type
="Spring.Web.Support.PageHandlerFactory, Spring.Web"
/>
</
httpHandlers
>
</
system.web
>
</
configuration
>
然后我们添加一个Model类User.cs:
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.2"
>
<
class
name
="SpringHiberate.dao.model.User,SpringHiberate"
table
="FS_User"
lazy
="false"
>
<
id
name
="Id"
column
="id"
type
="Int32"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="Name"
column
="uname"
type
="String"
length
="50"
/>
<
property
name
="Email"
column
="uemail"
type
="String"
length
="50"
/>
</
class
>
</
hibernate-mapping
>
然后我们写一个SQLProvider类SQLProvider.cs.
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
objects xmlns
=
"
http://www.springframework.net
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation
=
"
http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd
"
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
id
=
"
DbProvider
"
type
=
"
SpringHiberate.dao.SQLProvider, SpringHiberate
"
>
<
property name
=
"
ConnectionString
"
value
=
"
Data Source=192.168.1.21;Database=FastSpring20;User ID=sa;Password=oilchem2007;Trusted_Connection=False
"
/>
</
object
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
id
=
"
SessionFactory
"
type
=
"
Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12
"
>
<
property name
=
"
DbProvider
"
ref
=
"
DbProvider
"
/>
<
property name
=
"
MappingAssemblies
"
>
<
list
>
<
value
>
SpringHiberate
</
value
>
</
list
>
</
property
>
<
property name
=
"
HibernateProperties
"
>
<
dictionary
>
<
entry key
=
"
hibernate.connection.provider
"
value
=
"
NHibernate.Connection.DriverConnectionProvider
"
/>
<
entry key
=
"
hibernate.dialect
"
value
=
"
NHibernate.Dialect.MsSql2000Dialect
"
/>
<
entry key
=
"
hibernate.connection.driver_class
"
value
=
"
NHibernate.Driver.SqlClientDriver
"
/>
<
entry key
=
"
show_sql
"
value
=
"
true
"
/>
</
dictionary
>
</
property
>
</
object
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
id
=
"
HibernateTransactionManager
"
type
=
"
Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12
"
>
<
property name
=
"
DbProvider
"
ref
=
"
DbProvider
"
/>
<
property name
=
"
sessionFactory
"
ref
=
"
sessionFactory
"
/>
</
object
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
id
=
"
TransactionInterceptor
"
type
=
"
Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data
"
>
<
property name
=
"
TransactionManager
"
ref
=
"
HibernateTransactionManager
"
/>
<
property name
=
"
TransactionAttributeSource
"
>
<
object
type
=
"
Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data
"
/>
</
property
>
</
object
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
id
=
"
UserDao
"
type
=
"
SpringHiberate.dao.UserDao
"
>
<
property name
=
"
SessionFactory
"
ref
=
"
SessionFactory
"
/>
</
object
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
object
type
=
"
~/Default.aspx
"
>
<
property name
=
"
UserDao
"
ref
=
"
UserDao
"
/>
</
object
>
</
objects
>
修改Default.aspx.cs文件里的内容:
新建一个项目:名字叫SpringHiberate.
首先,我们修改我们的配置文件web.config:
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
然后我们添加一个Model类User.cs:
1
using
System;
2![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
3
namespace
SpringHiberate.dao.model
4![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
5
public class User
6![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
7
private Int32 id;
8![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
9
public Int32 Id
10![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
11![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
get
{ return id; }
12![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
set
{ id = value; }
13
}
14
private string name;
15![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
16
public string Name
17![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
18![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
get
{ return name; }
19![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
set
{ name = value; }
20
}
21
private string email;
22![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
23
public string Email
24![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
25![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
get
{ return email; }
26![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
set
{ email = value; }
27
}
28
}
29
}
我们再在User.cs目录里添加一个文件User.hbm.xml,文件的生成操作为嵌入的资源
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/4fd96b3cf02f4c7b5c8964ac8167f7af.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Spring.Data.Common;
namespace SpringHiberate.dao
{
public class SQLProvider : IDbProvider
{
#region IDbProvider 成员
private string _connectionString = "" ;
public string ConnectionString
{
get
{
return this ._connectionString;
}
set
{
this ._connectionString = value;
}
}
public IDbCommand CreateCommand()
{
return null ;
}
public object CreateCommandBuilder()
{
return null ;
}
public IDbConnection CreateConnection()
{
return null ;
}
public IDbDataAdapter CreateDataAdapter()
{
return null ;
}
public IDbDataParameter CreateParameter()
{
return null ;
}
public string CreateParameterName( string name)
{
return null ;
}
public string CreateParameterNameForCollection( string name)
{
return null ;
}
public IDbMetadata DbMetadata
{
get {
return null ;
}
}
public string ExtractError(Exception e)
{
return null ;
}
public bool IsDataAccessException(Exception e)
{
return false ;
}
#endregion
}
}
在写一个UserDao类(UserDao.cs)来操作
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Spring.Data.Common;
namespace SpringHiberate.dao
{
public class SQLProvider : IDbProvider
{
#region IDbProvider 成员
private string _connectionString = "" ;
public string ConnectionString
{
get
{
return this ._connectionString;
}
set
{
this ._connectionString = value;
}
}
public IDbCommand CreateCommand()
{
return null ;
}
public object CreateCommandBuilder()
{
return null ;
}
public IDbConnection CreateConnection()
{
return null ;
}
public IDbDataAdapter CreateDataAdapter()
{
return null ;
}
public IDbDataParameter CreateParameter()
{
return null ;
}
public string CreateParameterName( string name)
{
return null ;
}
public string CreateParameterNameForCollection( string name)
{
return null ;
}
public IDbMetadata DbMetadata
{
get {
return null ;
}
}
public string ExtractError(Exception e)
{
return null ;
}
public bool IsDataAccessException(Exception e)
{
return false ;
}
#endregion
}
}
using
System;
using System.Collections;
using Spring.Data.NHibernate.Support;
using SpringHiberate.dao.model;
namespace SpringHiberate.dao
{
public class UserDao : HibernateDaoSupport
{
public void Save(User user)
{
HibernateTemplate.Save(user);
}
public void Delete(User user)
{
HibernateTemplate.Delete(user);
}
public void Update(User user)
{
HibernateTemplate.Update(user);
}
public IList FindAll()
{
return HibernateTemplate.LoadAll( typeof (User));
}
public User Find(Object ID)
{
return (User)HibernateTemplate.Load( typeof (User), ID);
}
}
}
现在代码部分基本写完,我们开始写spring的配置文件(spring_bean_dao.xml),文件的生成操作也为嵌入的资源,他配置了数据库操作以及DAO部分,页面的操作DAO也是依赖注入的
using System.Collections;
using Spring.Data.NHibernate.Support;
using SpringHiberate.dao.model;
namespace SpringHiberate.dao
{
public class UserDao : HibernateDaoSupport
{
public void Save(User user)
{
HibernateTemplate.Save(user);
}
public void Delete(User user)
{
HibernateTemplate.Delete(user);
}
public void Update(User user)
{
HibernateTemplate.Update(user);
}
public IList FindAll()
{
return HibernateTemplate.LoadAll( typeof (User));
}
public User Find(Object ID)
{
return (User)HibernateTemplate.Load( typeof (User), ID);
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SpringHiberate.spring;
using SpringHiberate.dao;
namespace SpringHiberate
{
public partial class _Default : System.Web.UI.Page
{
private UserDao userDao;
public UserDao UserDao
{
get { return userDao; }
set { userDao = value; }
}
protected void Page_Load( object sender, EventArgs e)
{
Response.Write(hello.Name);
SpringHiberate.dao.model.User u = new SpringHiberate.dao.model.User();
u.Name = " safa " ;
u.Email = " eicesoft@126.com " ;
userDao.Save(u);
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SpringHiberate.spring;
using SpringHiberate.dao;
namespace SpringHiberate
{
public partial class _Default : System.Web.UI.Page
{
private UserDao userDao;
public UserDao UserDao
{
get { return userDao; }
set { userDao = value; }
}
protected void Page_Load( object sender, EventArgs e)
{
Response.Write(hello.Name);
SpringHiberate.dao.model.User u = new SpringHiberate.dao.model.User();
u.Name = " safa " ;
u.Email = " eicesoft@126.com " ;
userDao.Save(u);
}
}
}
好了,完成了.很简单吧.下面运行一下,然后看看数据库是不是新建了一条数据.
Spring.Net简化了数据框架NHibernate的操作.对页面进行注入,实现了松耦合.
Spring.Net远远不止这些.其他的方面在以后的文章中继续介绍
项目的源码打包:/Files/eicesoft/SpringHiberate.zip