NHibernate如何动态查询?
2010-10-29 16:01 bugfly 阅读(493) 评论(0) 编辑 收藏 举报
代码
CREATE TABLE [dbo].[Users]
(
[ID] [int] NOT NULL IDENTITY(1, 1),
[Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[Password] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
using System;
using System.Collections.Generic;
using System.Text;
using OMIS.Infrastructure;
namespace OMIS.Domain.Entities
{
public class User
{
public virtual int UserID { get; set; }
public virtual string UserName { get; set; }
public virtual string UserPwd { get; set; }
}
}
代码
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OMIS.Domain" namespace="OMIS.Domain">
<class name ="OMIS.Domain.Entities.User" table="Users">
<id name="UserID" column="ID" type="Int32" unsaved-value="0">
<generator class ="native"></generator>
</id>
<property name="UserName" column ="Name" type="string" length="50" not-null="false"/>
<property name ="UserPwd" column="Password" type="string" length="50" not-null="false"/>
</class>
</hibernate-mapping>
代码
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
namespace OMIS.Infrastructure
{
public interface IRepository<T> where T : class
{
IList<T> findByHQL(string hql);
void add(T entity);
void Save(T entity);
void Delete(T entity);
}
}
代码
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace OMIS.Infrastructure
{
public class NHibernateHelper
{
private ISessionFactory m_factory;
public NHibernateHelper()
{
this.m_factory = this.getSessionFactory();
}
private ISessionFactory getSessionFactory()
{
return (new Configuration()).Configure().BuildSessionFactory();
}
public ISession getSession()
{
return this.m_factory.OpenSession();
}
}
}
代码
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
namespace OMIS.Infrastructure
{
public class Repository<T>:IRepository<T> where T:class
{
protected ISession m_session;
#region IRepository<T> Members
public Repository(ISession session)
{
this.m_session = session;
}
public IList<T> findByHQL(string hql)
{
return this.m_session.CreateQuery(hql).List<T>();
}
public void add(T entity)
{
throw new NotImplementedException();
}
public void Save(T entity)
{
this.m_session.Save(entity);
this.m_session.Flush();
}
public void Delete(T entity)
{
throw new NotImplementedException();
}
#endregion
}
}
代码
using System;
using System.Collections.Generic;
using System.Text;
using OMIS.Infrastructure;
using OMIS.Domain.Entities;
using NHibernate;
namespace OMIS.Repository
{
public class UserRepository:Repository<User>
{
public UserRepository(ISession session)
: base(session)
{
this.m_session = session;
}
public string getNameByID(int id)
{
return this.m_session.CreateQuery("from User where id=:id").SetInt32("id",id).List<User>()[0].UserName;
}
}
}
代码
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="OMIS.UnitTest">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=YANG\SQLEXPRESS;Initial Catalog=NHibernateTest;Integrated Security=True;Pooling=False
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="OMIS.Domain"/>
</session-factory>
</hibernate-configuration>
代码
using System;
using System.Collections.Generic;
using System.Text;
using OMIS.Domain.Entities;
using OMIS.Infrastructure;
namespace OMIS.Service
{
public class UserService
{
private IRepository<User> m_repository;
public UserService(IRepository<User> repository)
{
this.m_repository = repository;
}
public string getNameByID(int id)
{
return m_repository.findByHQL("from User as c where c.UserID='" + id + "'")[0].UserName;
}
}
}
代码
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NHibernate;
using OMIS.Domain.Entities;
using OMIS.Infrastructure;
using OMIS.Repository;
namespace OMIS.UnitTest
{
[TestFixture]
class UserRepositroyTest
{
private NHibernateHelper m_helper;
private ISession m_session;
private UserRepository m_respositroy;
[TestFixtureSetUp]
public void init()
{
this.m_helper = new NHibernateHelper();
this.m_session = this.m_helper.getSession();
this.m_respositroy = new UserRepository(m_session);
}
[TestFixtureTearDown]
public void release()
{
this.m_respositroy = null;
}
[Test]
public void test_getNameByID()
{
int test_id = 4;
string n_expected = "YaYa";
Assert.AreEqual(n_expected, this.m_respositroy.getNameByID(test_id));
}
}
}
代码
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NHibernate;
using OMIS.Domain.Entities;
using OMIS.Infrastructure;
using OMIS.Service;
namespace OMIS.UnitTest
{
[TestFixture]
class UserServiceTest
{
private NHibernateHelper m_helper;
private ISession m_session;
private UserService m_service;
[TestFixtureSetUp]
public void init()
{
this.m_helper = new NHibernateHelper();
this.m_session = this.m_helper.getSession();
this.m_service = new UserService(new Repository<User>(this.m_session));
}
[Test]
public void test_getNameByID()
{
string n_expected = "Admin";
int test_id = 4;
Assert.AreEqual(n_expected, this.m_service.getNameByID(test_id));
}
}
}
作者:桀骜的灵魂
出处:http://www.cnblogs.com/HuntSoul/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。