Imports Castle.Windsor
Imports Castle.Windsor.Configuration.Interpreters
Imports Castle.DynamicProxy
Imports Castle.Facilities
Imports Castle.MicroKernel
Public Class DAOFactory
Dim container As New WindsorContainer(New XmlInterpreter("../BasicUsage.xml"))
Public Sub New()
'加入组件
container.AddComponent("user", GetType(UserDAO), GetType(UserDAO))
End Sub
Public Function GetUserOperator() As UserDAO
Return CType(container("user"), UserDAO) '使用组件
End Function
End Class
Imports NHibernate.Cfg
Imports NHibernate
Imports NHibernate.Tool.hbm2ddl.SchemaExport
Imports NHibernate.Connection
Imports System.Reflection.Assembly
Imports System.Type
Public Class UserDAO
Dim objCfg As New Configuration
Dim objUser As user
Dim transaction As ITransaction
Dim session As ISession
Dim factory As ISessionFactory
Dim lstUsers As IList
Private ReadOnly lockObj As New Object
'Public Sub New()
' 'System.Reflection.Assembly.GetExecutingAssembly()
' objCfg.AddAssembly("NHibernateDemo")
' factory = objCfg.BuildSessionFactory()
' session = factory.OpenSession()
' transaction = session.BeginTransaction()
'End Sub
Public Sub create()
objCfg.AddAssembly("NHibernateDemo")
factory = objCfg.BuildSessionFactory()
session = factory.OpenSession()
transaction = session.BeginTransaction()
End Sub
Public Function ListUsers(ByVal ID As String) As IList
Try
create()
lstUsers = session.Find("from user as t where t.ID='" & ID & "'")
transaction.Commit()
Catch ex As Exception
transaction.Rollback()
session.Disconnect()
End Try
Return lstUsers
End Function
Public Function GetUsers() As IList
Try
create()
lstUsers = session.CreateCriteria(GetType(user)).List()
transaction.Commit()
Catch ex As Exception
transaction.Rollback()
session.Disconnect()
End Try
Return lstUsers
End Function
Public Function GetUserByID(ByVal userID As String) As user
Try
create()
objUser = session.Load(GetType(user), userID)
Catch ex As Exception
transaction.Rollback()
session.Disconnect()
End Try
Return objUser
End Function
Public Function SaveOrUpdateUser(ByVal objuser As user) As Boolean
Dim bool As Boolean = True
Try
create()
session.Save(objuser)
transaction.Commit()
Catch ex As Exception
transaction.Rollback()
session.Disconnect()
bool = False
End Try
Return bool
End Function
Public Function DeleteUser(ByVal userID As String) As Boolean
Dim bool As Boolean = True
Try
create()
objUser = session.Load(GetType(user), userID)
session.Delete(objUser)
transaction.Commit()
Catch ex As Exception
transaction.Rollback()
session.Disconnect()
bool = False
End Try
Return bool
End Function
End Class
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateDemo.user, NHibernateDemo" table="TBL_USER_INFO">
<id name="ID" column="USER_ID" type="String" length="16">
<generator class="assigned" />
</id>
<property name="UserName" column="USER_NAME" type="String" length="50" not-null="true" />
<property name="PassWord" column="USER_PASSWORD" type="String" length="32" not-null="true" />
<property name="DepartCode" column="DEPARTMENT_CODE" type="String" length="16" not-null="true" />
<property name="CreateDate" column="CREATE_DATE" type="DateTime" not-null="true" />
<property name="SubDepartCode" column="SUBDEPARTMENT_CODE" type="String" length="16" />
<property name="StatusCode" column="STATUS_CODE" type="String" length="1" not-null="true" />
<property name="PassWordSign" column="PASSWORD_STATUS_CODE" type="String" length="1"/>
<!--<set name="groups" table="TBL_USER_MANAGEMENT">
<key column="USER_ID"/>
<many-to-many column="USER_GROUP_ID" class="cn.com.airchina.member.pojo.Group"/>
</set> -->
</class>
</hibernate-mapping>
Public Class user
Private _id As String
Private _userName As String
Private _password As String
Private _departCode As String
Private _subDepartCode As String
Private _statusCode As String
Private _passWordSign As String
Private _createDate As DateTime
Public Property ID() As String
Get
Return _id
End Get
Set(ByVal Value As String)
If Value <> _id Then
If Value = "" Then
_id = Nothing
Else
_id = Value
End If
End If
End Set
End Property
Public Property UserName() As String
Get
Return _userName
End Get
Set(ByVal Value As String)
If Value <> _userName Then
If Value = "" Then
_userName = Nothing
Else
_userName = Value
End If
End If
End Set
End Property
Public Property PassWord() As String
Get
Return _password
End Get
Set(ByVal Value As String)
If Value <> _password Then
If Value = "" Then
_password = Nothing
Else
_password = Value
End If
End If
End Set
End Property
Public Property DepartCode() As String
Get
Return _departCode
End Get
Set(ByVal Value As String)
If Value <> _departCode Then
If Value = "" Then
_departCode = Nothing
Else
_departCode = Value
End If
End If
End Set
End Property
Public Property SubDepartCode() As String
Get
Return _subDepartCode
End Get
Set(ByVal Value As String)
If Value <> _subDepartCode Then
If Value = "" Then
_subDepartCode = Nothing
Else
_subDepartCode = Value
End If
End If
End Set
End Property
Public Property StatusCode() As String
Get
Return _statusCode
End Get
Set(ByVal Value As String)
If Value <> _statusCode Then
If Value = "" Then
_statusCode = Nothing
Else
_statusCode = Value
End If
End If
End Set
End Property
Public Property PassWordSign() As String
Get
Return _passWordSign
End Get
Set(ByVal Value As String)
If Value <> _passWordSign Then
If Value = "" Then
_passWordSign = Nothing
Else
_passWordSign = Value
End If
End If
End Set
End Property
Public Property CreateDate() As DateTime
Get
Return _createDate
End Get
Set(ByVal Value As DateTime)
If Value <> _createDate Then
If CType(Value, String) = "" Then
_createDate = Nothing
Else
_createDate = Value
End If
End If
End Set
End Property
End Class
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<nhibernate>
<add key="hibernate.show_sql" value="true"/>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.Oracle9Dialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.connection.connection_string" value="user id=caac;data source=orcl08;password=caac;max pool size=300" />
</nhibernate>
<!-- This section contains the log4net configuration settings -->
<log4net info="true">
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority
如果只需要看看Sql设置INFO就够了,如果你要调试可以设置为DEBUG或ALL-->
<root>
<priority value="info" />
<appender-ref ref="rollingFile" />
</root>
</log4net>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<components>
<component id="user">
<parameters>
<target>log.txt</target>
</parameters>
</component>
</components>
</configuration>