Spring Hiberate 入门实例

 

    Spring 和 Hiberate操作数据库的入门例子,主要实现了,增删改查的功能,极其简单的例子。

Spring Hiberate 入门实例

数据库连接的设置在Web.config和spring_bean_dao中进行了设置

View Code
 1 <?xml version="1.0"?>

 2 <configuration>

 3     <configSections>

 4         <sectionGroup name="spring">

 5             <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>

 6             <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>

 7         </sectionGroup>

 8         <section name="SpringOverrideProperty" type="System.Configuration.NameValueSectionHandler"/>

 9         <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

10     </configSections>

11     <SpringOverrideProperty>

12         <add key="DbProvider.ConnectionString" value="Data Source=.;Database=SpringHiberate;User ID=sa;Password=sa2008;Trusted_Connection=False"/>

13         <add key="SystemInit.IsDebug" value="true"/>

14         <add key="SystemInit.Level" value="4"/>

15     </SpringOverrideProperty>

16     <!-- Spirng.Net 配置 -->

17     <spring>

18         <context>

19             <resource uri="config://spring/objects"/>

20             <resource uri="assembly://SpringHiberate/SpringHiberate/spring_bean_dao.xml"/>

21         </context>

22         <objects xmlns="http://www.springframework.net"/>

23     </spring>

24     <appSettings/>

25     <connectionStrings/>

26     <system.web>

27         <compilation debug="true" targetFramework="4.0"/>

28         <authentication mode="Windows"/>

29         <!--

30           如果在执行请求的过程中出现未处理的错误,

31           则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,

32           开发人员通过该节可以配置

33           要显示的 html 错误页

34           以代替错误堆栈跟踪。

35 

36       <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

37           <error statusCode="403" redirect="NoAccess.htm" />

38           <error statusCode="404" redirect="FileNotFound.htm" />

39       </customErrors>

40       -->

41         <httpModules>

42             <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>

43         </httpModules>

44         <httpHandlers>

45             <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>

46         </httpHandlers>

47         <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

48 </configuration>
View Code
 1 <?xml version="1.0" encoding="utf-8" ?>

 2 <objects xmlns="http://www.springframework.net"

 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 4          xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">

 5   <object id="hello" type="SpringHiberate.spring.Hello">

 6     <property name="Name" value="你好中国"></property>

 7   </object>

 8 

 9   <object id="DbProvider" type="SpringHiberate.dao.SQLProvider, SpringHiberate">

10     <property name="ConnectionString" value="Data Source=.;Database=SpringHiberate;User ID=sa;Password=sa2008;Trusted_Connection=False" />

11   </object>

12 

13   <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">

14     <property name="DbProvider" ref="DbProvider" />

15     <property name="MappingAssemblies">

16       <list>

17         <value>SpringHiberate</value>

18       </list>

19     </property>

20     <property name="HibernateProperties">

21       <dictionary>

22         <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />

23         <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />

24         <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />

25         <entry key="show_sql" value="true" />

26       </dictionary>

27     </property>

28   </object>

29 

30   <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">

31     <property name="DbProvider" ref="DbProvider" />

32     <property name="sessionFactory" ref="sessionFactory" />

33   </object>

34 

35   <object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">

36     <property name="TransactionManager" ref="HibernateTransactionManager" />

37     <property name="TransactionAttributeSource">

38       <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />

39     </property>

40   </object>

41 

42   <object id="UserDao" type="SpringHiberate.dao.UserDao">

43     <property name="SessionFactory" ref="SessionFactory" />

44   </object>

45 

46   <object type="~/Default.aspx">

47     <property name="Hello" ref="hello"/>

48     <property name="UserDao" ref="UserDao" />

49   </object>

50 </objects>

开发环境使用的VS2010和SQL2008 

SpringHiberate.rar

你可能感兴趣的:(spring)