mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
combine website examples.
This commit is contained in:
82
Examples/website/FreeSql.Site.DAL/BaseDAL.cs
Normal file
82
Examples/website/FreeSql.Site.DAL/BaseDAL.cs
Normal file
@ -0,0 +1,82 @@
|
||||
//using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public class BaseDAL<T> where T : BaseEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 新增方法
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual long Insert(T model)
|
||||
{
|
||||
var runsql = DataBaseType.MySql.DB().Insert<T>(model);
|
||||
return runsql.ExecuteIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改方法
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update(T model)
|
||||
{
|
||||
var runsql = DataBaseType.MySql.DB().Update<T>().SetSource(model);
|
||||
return runsql.ExecuteAffrows() > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除方法
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete(long id)
|
||||
{
|
||||
return DataBaseType.MySql.DB().Delete<T>(id).ExecuteDeleted().Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条数据
|
||||
/// </summary>
|
||||
/// <param name="where"></param>
|
||||
/// <returns></returns>
|
||||
public virtual T GetByOne(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return DataBaseType.MySql.DB().Select<T>()
|
||||
.Where(where).ToOne();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询方法
|
||||
/// </summary>
|
||||
/// <param name="where"></param>
|
||||
/// <param name="orderby"></param>
|
||||
/// <returns></returns>
|
||||
public virtual (List<T> list, long count) Query(Expression<Func<T, bool>> where,
|
||||
Expression<Func<T, T>> orderby = null, PageInfo pageInfo = null)
|
||||
{
|
||||
//设置查询条件
|
||||
var list = DataBaseType.MySql.DB().Select<T>()
|
||||
.Where(where);
|
||||
|
||||
BaseEntity baseEntity = new BaseEntity();
|
||||
//设置排序
|
||||
if (orderby != null) list = list.OrderBy(nameof(baseEntity.CreateDt) + " desc ");
|
||||
|
||||
var count = list.Count();
|
||||
//设置分页操作
|
||||
if (pageInfo != null && pageInfo.IsPaging)
|
||||
list.Skip((pageInfo.PageIndex - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize);
|
||||
|
||||
//执行查询
|
||||
return (list.ToList(), count);
|
||||
}
|
||||
}
|
||||
}
|
40
Examples/website/FreeSql.Site.DAL/Db.cs
Normal file
40
Examples/website/FreeSql.Site.DAL/Db.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using FreeSql.Site.DAL.Helper;
|
||||
using System;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public static class Db
|
||||
{
|
||||
public static System.Collections.Generic.Dictionary<string, IFreeSql> ConnectionPool = new System.Collections.Generic.Dictionary<string, IFreeSql>();
|
||||
|
||||
private static string getConnectionString(string sDatabaseType)
|
||||
{
|
||||
return AppSettingsManager.Get($"DbContexts:{sDatabaseType}:ConnectionString");
|
||||
}
|
||||
|
||||
private static IFreeSql SelectDBType(string dbtype)
|
||||
{
|
||||
if (!ConnectionPool.ContainsKey(dbtype))
|
||||
{
|
||||
ConnectionPool.Add(dbtype, new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.MySql, getConnectionString(dbtype))
|
||||
.Build());
|
||||
}
|
||||
return ConnectionPool[dbtype];
|
||||
}
|
||||
|
||||
public static IFreeSql DB(this DataBaseType t)
|
||||
{
|
||||
return SelectDBType(t.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public enum DataBaseType
|
||||
{
|
||||
MySql,
|
||||
SqlServer,
|
||||
PostgreSQL,
|
||||
Oracle,
|
||||
Sqlite
|
||||
}
|
||||
}
|
14
Examples/website/FreeSql.Site.DAL/DocumentCommentDAL.cs
Normal file
14
Examples/website/FreeSql.Site.DAL/DocumentCommentDAL.cs
Normal file
@ -0,0 +1,14 @@
|
||||
//using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public class DocumentCommentDAL : BaseDAL<DocumentComment>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
90
Examples/website/FreeSql.Site.DAL/DocumentContentDAL.cs
Normal file
90
Examples/website/FreeSql.Site.DAL/DocumentContentDAL.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public class DocumentContentDAL : BaseDAL<DocumentContent>
|
||||
{
|
||||
///// <summary>
|
||||
///// 新增
|
||||
///// </summary>
|
||||
///// <param name="model"></param>
|
||||
///// <returns></returns>
|
||||
//public long Insert(DocumentContent model)
|
||||
//{
|
||||
// return DataBaseType.MySql.DB().Insert<DocumentContent>(model).ExecuteIdentity();
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 修改
|
||||
///// </summary>
|
||||
///// <param name="model"></param>
|
||||
///// <returns></returns>
|
||||
//public bool Update(DocumentContent model)
|
||||
//{
|
||||
// var runsql = DataBaseType.MySql.DB().Update<DocumentContent>().SetSource(model);
|
||||
// return runsql.ExecuteAffrows() > 0;
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 删除
|
||||
///// </summary>
|
||||
///// <param name="id"></param>
|
||||
///// <returns></returns>
|
||||
//public bool Delete(long id)
|
||||
//{
|
||||
// return DataBaseType.MySql.DB().Delete<DocumentContent>(id).ExecuteDeleted().Count > 0;
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 获取一条数据
|
||||
///// </summary>
|
||||
///// <param name="where"></param>
|
||||
///// <returns></returns>
|
||||
//public DocumentContent GetByOne(Expression<Func<DocumentContent, bool>> where)
|
||||
//{
|
||||
// return DataBaseType.MySql.DB().Select<DocumentContent>()
|
||||
// .Where(where).ToOne();
|
||||
//}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 获取一条数据
|
||||
///// </summary>
|
||||
///// <param name="where"></param>
|
||||
///// <returns></returns>
|
||||
//public long Count(Expression<Func<DocumentContent, bool>> where)
|
||||
//{
|
||||
// return DataBaseType.MySql.DB().Select<DocumentContent>()
|
||||
// .Where(where).Count();
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 查询功能
|
||||
///// </summary>
|
||||
///// <param name="where"></param>
|
||||
///// <param name="orderby"></param>
|
||||
///// <returns></returns>
|
||||
//public (List<DocumentContent> list, long count) Query(Expression<Func<DocumentContent, bool>> where,
|
||||
// Expression<Func<DocumentContent, DocumentContent>> orderby = null, PageInfo pageInfo = null)
|
||||
//{
|
||||
// //设置查询条件
|
||||
// var list = DataBaseType.MySql.DB().Select<DocumentContent>()
|
||||
// .Where(where);
|
||||
|
||||
// //设置排序
|
||||
// if (orderby != null) list = list.OrderBy(b => b.CreateDt);
|
||||
|
||||
// var count = list.Count();
|
||||
// //设置分页操作
|
||||
// if (pageInfo != null && pageInfo.IsPaging)
|
||||
// list.Skip(pageInfo.PageIndex * pageInfo.PageSize).Limit(pageInfo.PageSize);
|
||||
|
||||
// //执行查询
|
||||
// return (list.ToList(), count);
|
||||
//}
|
||||
}
|
||||
}
|
15
Examples/website/FreeSql.Site.DAL/DocumentTypeDAL.cs
Normal file
15
Examples/website/FreeSql.Site.DAL/DocumentTypeDAL.cs
Normal file
@ -0,0 +1,15 @@
|
||||
//using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public class DocumentTypeDAL : BaseDAL<DocumentType>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
18
Examples/website/FreeSql.Site.DAL/FreeSql.Site.DAL.csproj
Normal file
18
Examples/website/FreeSql.Site.DAL/FreeSql.Site.DAL.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\FreeSql\FreeSql.csproj" />
|
||||
<ProjectReference Include="..\FreeSql.Site.Entity\FreeSql.Site.Entity.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,44 @@
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置管理器
|
||||
/// </summary>
|
||||
public static class AppSettingsManager
|
||||
{
|
||||
private static IConfiguration _configuration;
|
||||
|
||||
static AppSettingsManager()
|
||||
{
|
||||
BuildConfiguration();
|
||||
}
|
||||
|
||||
private static void BuildConfiguration()
|
||||
{
|
||||
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", false).AddJsonFile("appsettings.Development.json", true);
|
||||
_configuration = builder.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定节点信息
|
||||
/// </summary>
|
||||
/// <param name="key">节点名称,多节点以:分隔</param>
|
||||
public static string Get(string key)
|
||||
{
|
||||
return _configuration[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定节点信息
|
||||
/// </summary>
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
string json = Get(key);
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
}
|
||||
}
|
37
Examples/website/FreeSql.Site.DAL/Helper/EnumHelper.cs
Normal file
37
Examples/website/FreeSql.Site.DAL/Helper/EnumHelper.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Site.DAL.Helper
|
||||
{
|
||||
public class EnumHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 枚举类型转换为字符串
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="en"></param>
|
||||
/// <returns></returns>
|
||||
public static string EnumConvertToString<T>(T en)
|
||||
{
|
||||
//方法一
|
||||
//return color.ToString();
|
||||
|
||||
//方法二
|
||||
return Enum.GetName(en.GetType(), en);
|
||||
}
|
||||
public static T StringConvertToEnum<T>(string str)
|
||||
{
|
||||
T result = default(T);
|
||||
try
|
||||
{
|
||||
result = (T)Enum.Parse(typeof(T), str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
14
Examples/website/FreeSql.Site.DAL/TemplateExampleDAL.cs
Normal file
14
Examples/website/FreeSql.Site.DAL/TemplateExampleDAL.cs
Normal file
@ -0,0 +1,14 @@
|
||||
//using FreeSql.Site.Entity;
|
||||
using FreeSql.Site.Entity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace FreeSql.Site.DAL
|
||||
{
|
||||
public class TemplateExampleDAL : BaseDAL<TemplateExample>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user