Bouyei.DbFactory 项目框架地址 https://gitee.com/bouyei/Bouyei.DbFactory
sql 脚本增删查改生成例子
//生成简单查询脚本
ISqlProvider sqlProvider = SqlProvider.CreateProvider();
//group by
string sqlgroupby = sqlProvider.Select().Count().From()
.Where(x => x.uage == 1).GroupBy().SqlString;
//function
string sqlfun = sqlProvider.Select(new Max("age")).From().Where(x=>x.uage>20).SqlString;
//order by
var osql = sqlProvider.Select().From().OrderBy(SortType.Asc, "name").SqlString;
var dic = new Dictionary();
dic.Add("name", "hellow");
dic.Add("age", 0);
dic.Add("score", 1.0);
//MappedName 测试
var sqls = sqlProvider.Insert().Values(new UserDto[] { new UserDto() {
Pwd="ds",
UserName="d"
} }).SqlString;
//查询
var sql = sqlProvider.Select()
.From().Where(x => x.id == 1).Top(FactoryType.PostgreSQL, 10).SqlString;
//修改
sql = sqlProvider.Update()
.Set(new User() { uname = "bouyei" })
.Where(x => x.id == 1 || (x.uname == "b" && x.score == 2)).SqlString;
//删除
sql = sqlProvider.Delete()
.From().Where(x => x.uname == "bouyei").SqlString;
//插入
sql = sqlProvider.Insert()
.Values(new User[] {
new User() { uname ="hello", uage=12 }
,new User() { uname="bouyei",uage=23} }).SqlString;
2、增删查改操作例子
IAdoProvider dbProvider = AdoProvider.CreateProvider(connectionString, FactoryType.PostgreSQL);
var ext = dbProvider.Connect(connectionString);
var adort = dbProvider.Query(new Parameter()
{
CommandText = "select * from public.db_user"
});
//查询
var rt= dbProvider.Query(x => x.uage >= 20);
//单表分页 查询语法
var takert = dbProvider.PageQuery(x => x.uage == 30,0, 10);
//更改
var dic = new Dictionary();
dic.Add("name", "hellow");
dic.Add("age", 0);
dic.Add("score", 1.0);
dbProvider.Update(dic, x => x.id == 1);
//删除操作
dbProvider.Delete(x => x.uname == "bouyei");
//插入
var users = new User[] {
new User(){ uname="bouyei", score=100, uage=30 },
new User(){ uname="八渡", score=10, uage=20 }
};
dbProvider.Insert(users);
3、mapper实体,实体对象复制到新对象
User u = new User() {
uname="b",
id=1
};
User b = new User() {
uname="a",
id=2
};
//u对象定义的变量复制给b对象,条件是只复制name等变量名
EntityMapper.MapTo(u, b, FilterType.Include, "name");
4、实体或服务继承实现基本的增删查改操作无需写sql脚本
[MappedName("db_user")]
public class UserDto : BaseMapper
{
public string UserName { get; set; }
[Ignore]
public string Pwd { get; set; }
public void AddUser(UserDto user)
{
base.Insert(user);
}
public void DeleteById(string name)
{
base.Delete(x => x.UserName == name);
}
public void UpdateUser(UserDto dto)
{
base.Update(dto, x => x.UserName == "bouyei");
}
public List QueryUsers(int page=0,int size=10)
{
return base.Select(page, size, x => true);
}
}
public class fc3d:BaseMapper
{
public string fname { get; set; }
public long fcode { get; set; }
}
public class BaseMapper : TableMapper where T : class
{
public BaseMapper()
{
string connstr = "Host=127.0.0.1;Port=5432;User id=postgres;Password=bouyei;Database=postgres;";
var provider = AdoProvider.CreateProvider(connstr, FactoryType.PostgreSQL);
Initilize(provider);
}
}