Restful.Data 经过 博客园 和 CSDN 的宣传,目前参与该项目的人数已达到 114 人(群号:338570336),虽大部分并不参与实际开发,但他们所提出的意见和建议以及在试用过程中发现的问题是非常宝贵的,在此对这帮兄弟的热心参与表示感谢。
Restful.Data 群欢迎任何对技术交流感兴趣的朋友加入,我们群拥有众多热心的技术达人,他们会帮你解答一切工作中所遇到的问题,我们共同期待你们的光临,尤其是萌妹纸哦。
Restful.Data 目前已完成 MySql、Oracle 和 SqlServer 三种数据库的 Provider 开发工作,意味着你可以使用 Restful.Data 操作这三种数据库。
Restful.Data 最新源代码已经发布到 github,地址:https://github.com/linli8/Restful。
Restful.Data 最新 NuGet 包已经发布,可以在 NuGet 管理器上搜索 Restful.Data 下载最新版本试用。NuGet 地址:https://www.nuget.org/packages/Restful.Data/。
有很多朋友询问有没有完整的使用文档,下面我分不同的场景来描述具体如何使用。
1、连接字符串配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <clear/> <add name="MySql" connectionString="server=192.168.1.101;database=Restful;user id=linli8;password=linli8" providerName="Restful.Data.MySql"/> <add name="SqlServer" connectionString="" providerName="Restful.Data.SqlServer"/> <add name="Oracle" connectionString="Data Source=linli8;User ID=linli8;Password=linli8" providerName="Restful.Data.Oracle"/> </connectionStrings> </configuration>
2、注册驱动程序工厂
// MySql SessionProviderFactories.Register<MySqlSessionProviderFactory>(); // SqlServer // SessionProviderFactories.Register<SqlServerSessionProviderFactory>(); // Oracle // SessionProviderFactories.Register<OracleSessionProviderFactory>();
3、设置默认数据库连接
SessionFactory.Default = "MySql";
此设置并非必须,如果在配置文件中配置了多个数据库连接,可通过此设置来告知 CreateDefaultSession 选择使用哪个数据库连接。
3、不带参数的查询操作
using( ISession session = SessionFactory.CreateDefaultSession() ) { string sql = "select count(*) from Person"; int count = session.ExecuteScalar<int>( sql ); sql = "select * from Person"; IDataReader reader = session.ExecuteDataReader( sql ); DataTable dt = session.ExecuteDataTable( sql ); DataSet ds = session.ExecuteDataSet( sql ); }
4、匿名参数查询和增删改操作
using( ISession session = SessionFactory.CreateDefaultSession() ) { string sql = "select count(*) from Person where Name = ? and CreateTime < ?"; int count = session.ExecuteScalar<int>( sql, 1, DateTime.Now ); sql = "select * from Person where Name = ? and CreateTime < ?"; IDataReader reader = session.ExecuteDataReader( sql, 1, DateTime.Now ); DataTable dt = session.ExecuteDataTable( sql, 1, DateTime.Now ); DataSet ds = session.ExecuteDataSet( sql, 1, DateTime.Now ); sql = "insert into Person( Name, CreateTime ) values ( ?, ? )"; int i = session.Insert( sql, "testname", DateTime.Now ); }
5、命名参数查询和增删改操作
using( ISession session = SessionFactory.CreateDefaultSession() ) { IDictionary<string,object> parameters = new Dictionary<string,object>(); parameters.Add( "@Name", "testname" ); parameters.Add( "@CreateTime", DateTime.Now ); string sql = "select count(*) from Person where Name = @Name and CreateTime < @CreateTime"; int count = session.ExecuteScalar<int>( sql, parameters ); sql = "select * from Person where Name = @Name and CreateTime < @CreateTime"; IDataReader reader = session.ExecuteDataReader( sql, parameters ); DataTable dt = session.ExecuteDataTable( sql, parameters ); DataSet ds = session.ExecuteDataSet( sql, parameters ); sql = "insert into Person( Name, CreateTime ) values ( @Name, @CreateTime )"; int i = session.Insert( sql, parameters ); }
6、分页查询
using( ISession session = SessionFactory.CreateDefaultSession() ) { string sql = "select count(*) from Person"; DataPage dp = session.ExecuteDataPage( sql, 1, 20, "CreateTime DESC" ); DataTable dt = dp.Data; }
上面所提到的参数查询方式对分页查询均适用。
7、存储过程调用
using( ISession session = SessionFactory.CreateDefaultSession() ) { CommandParameter parameter1 = new CommandParameter(); parameter1.Direction = ParameterDirection.Input; parameter1.ParameterName = "@Name"; parameter1.Size = 20; parameter1.Value = "testname"; CommandParameter parameter2 = new CommandParameter(); parameter2.Direction = ParameterDirection.Input; parameter2.ParameterName = "@CreateTime"; parameter2.Size = 20; parameter2.Value = DateTime.Now; session.ExecuteStoredProcedure( "storedProcedureName", new List<CommandParameter>(){ parameter1, parameter2 } ); }
8、事务处理
using( ISession session = SessionFactory.CreateDefaultSession() ) { using( DbTransaction transaction = session.BeginTransaction() ) { // ... transaction.Commit(); } }
9、LINQ 查询
using( ISession session = SessionFactory.CreateDefaultSession() ) { var queryable = session.Find<Person>() .Where( s => s.Id == 1 ) .Where( s => s.Name.Contains( "test" ) ) .OrderBy( s => s.CreateTime ) .OrderBy( s => s.Name ) .Skip( 0 ).Take( 1 ) .Select( s => new { Name = s.Name, CreateTime = s.CreateTime } ); // 断言 Assert.AreEqual( 1, queryable.Count() ); // 打印 SQL Console.WriteLine( session.Provider.ExecutedCommandBuilder ); }
10、实体新增
// 新增和更新时,对象务必使用 EntityHelper 创建实体代理,否则无法跟踪属性变化 var person = EntityHelper.CreateProxy<Person>(); person.Name = "test"; //person.Age = 20; person.Money = 100; person.CreateTime = DateTime.Now; person.IsActive = false; using( ISession session = SessionFactory.CreateDefaultSession() ) { // 直接插入实体 int i = session.Insert( person ); // 输出生成的SQL语句 Console.WriteLine( session.Provider.ExecutedCommandBuilder ); int id = session.GetIndentifer<int>();// Lambda 表达式插入 i = session.Insert<Person>() .Set( s => s.Name, "test" ) .Set( s => s.Age, 20 ) .Set( s => s.Money, 200 ) .Set( s => s.CreateTime, DateTime.Now ) .Set( s => s.IsActive, true ) .Execute(); // 输出生成的SQL语句 Console.WriteLine( session.Provider.ExecutedCommandBuilder ); }
11、实体更新
using( ISession session = SessionFactory.CreateDefaultSession() ) { // Find 方法返回的对象都是原始对象而非代理对象 var person = session.Find<Person>().Where( s => s.Id == 1 ).Single(); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); // 根据原始对象创建代理对象 person = person.ToEntityProxy(); person.Name = "test01"; person.Age = 31; person.Money = 200; // 直接更新实体代理 int i = session.Update( person ); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); person = session.Find<Person>().Where( s => s.Id == 2 ).Single(); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); person = EntityHelper.CreateProxy<Person>(); person.Name = "test02"; person.Age = 22; person.Money = 101; person.CreateTime = DateTime.Now; person.IsActive = false; // 使用 Lambda 表达式设置 where 条件 i = session.Update<Person>() .Set( person ) .Where( s => s.Id == id ) .Execute(); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); person = session.Find<Person>().Where( s => s.Id == 3 ).Single(); // 使用 Lambda 表达式设置 更新字段 和 where 条件 i = session.Update<Person>().Set( s => s.Name, "test03" ) .Set( s => s.Age, 23 ) .Set( s => s.Money, 102 ) .Set( s => s.CreateTime, DateTime.Now ) .Set( s => s.IsActive, true ) .Where( s => s.Id == 3 ) .Execute(); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); }
12、实体删除
using( ISession session = SessionFactory.CreateDefaultSession() ) { var person = session.Find<Person>().Where( s => s.Id == 1 ).Single(); // 直接删除实体 int i = session.Delete( person ); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); // 使用 Lambda 表达式设置 where 条件 i = session.Delete<Person>().Where( s => s.Id == 1 ).Execute(); Console.WriteLine( session.Provider.ExecutedCommandBuilder ); }