AgileRepository

2018-02-07 20:17:31来源:cnblogs.com作者:一个基于接口的Repository快速开发库 - Agile人点击

分享

AgileRepository

这是一个可以帮助你快速开发Repository的lib。有点像SpringData JPA根据方法名、注解来自动生成查询方法的功能。
对于一些简单的查询,只需要定义接口就行了,实现都不用。
Github: AgileRepository

依赖

AspectCore >= 0.2.4
Dapper >= 1.50.4
DapperExtensions >= 1.6.3
System.Threading.Tasks.Extensions >= 4.3.0

使用

public interface IUserRepository:IAgileRepository<Users>{    [QueryByMethodName]    IEnumerable<Users> QueryByUserName(string userName);}var repository = AgileRepository.Proxy.SingletonInstance<IUserRepository>();repository.QueryByUserName("admin"); 

配置

    AgileRepository.SetConfig(new AgileRepositoryConfig()    {            SqlMonitor = (sql, paramters ) =>            {                    Console.WriteLine(sql);            },            ConnectionName = "conn"    });

示例

根据sql查询

    [QueryBySql("SELECT * FROM USERS")]    IEnumerable<User> TestSql();    [QueryBySql("SELECT * FROM USERS where username=@userName")]    IEnumerable<User> TestSql1(string userName); 

根据方法名称查询

    [QueryByMethodName]    IEnumerable<User> QueryByUserName(string userName);    [QueryByMethodName]    IEnumerable<User> QueryByUserNameAndId(string userName, string id);    [QueryByMethodName]    IEnumerable<User> QueryByCreaterIsNull();    [QueryByMethodName]    IEnumerable<User> QueryByCreaterIsNotNull(); 

查询所有

    [QueryAll]    IEnumerable<User> QueryAll(); 

根据 sql Count

    [CountBySql("Select count(*) from users")]    int TestCount();    [CountBySql("Select count(*) from users where userName=@userName")]    int TestCount1(string userName); 

根据方法名Count

    [CountByMethodName]    int CountByUserName(string userName);    [CountByMethodName]    int CountByIdAndUserName(string id, string userName);

Count所有

    [CountAll]    int CountAll();

Insert

    [Insert]    int Insert(User user);    [Insert]    int Insert(IEnumerable<User> users);

Update

    [Update]    int Update(User user);    [Update]    int Update(IEnumerable<User> users);

    [Delete]    int Delete(User user);    [Delete]    int Delete(IEnumerable<User> users);    [DeleteByMethodName]    int DeleteByUserName(string userName);

执行非查询sql

    [ExecuteBySql("Delete from [users] where id =@id ")]    int Execute(string id);

支持的where关键字

KeyNameWhere
AndQueryByUserNameAndIdwhere UserName=@UserName And Id=@Id
OrQueryByUserNameOrIdwhere UserName=@UserName Or Id=@Id
IsNullQueryByUserNameIsNullwhere UserName Is Null
IsNotNullQueryByUserNameIsNotNullwhere UserName Is Not Null
GreaterThenQueryByAgeGreaterThenwhere Age>@Age
GreaterEqualQueryByAgeGreaterEqualwhere Age>=@Age
LessThenQueryByAgeLessThenwhere Age<@Age
LessEqualQueryByAgeLessEqualwhere Age<=@Age
NotQueryByAgeNotwhere Age!=@Age
InQueryByUserNameInwhere UserName in @UserName
NotInQueryByUserNameNotInwhere UserName Not in @UserName
LikeQueryByUserNameLikewhere UserName Like @UserName
NotLikeQueryByUserNameNotLikewhere UserName Not Like @UserName

相关文章

    无相关信息

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台