[FasterTable(TableName = "tb_user")] //自动映射表的别名
public class User
{
[FasterIdentity] //自增长ID
[FasterKey] //设为主键
public int UserId { get; set; }
[FasterColumn(ColumnName = "user_name")] //设置列的别名
[FasterKey] //多个主键
public string UserName { get; set; } = "zq";
public string Password { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
1、获取实体列表。IEnumerable GetList(this IDbConnection connection, string strWhere = "", object param = null)
connection.GetList<User>(" where userid>@id", new { id = 10 })
connection.Get<User>(new { UserId = 1, UserName = "张强1" });
connection.Add(new User
{
UserName = "张强",
Password = "123456",
Email = "237183141@qq.com",
Phone = "18516328675"
});
//批量新增
List<User> userList = new List<User>();
for (int i = 0; i < 10000; i++)
{
userList.Add(new User
{
UserName = "张强" + (i + 1),
Password = "123456",
Email = "237183141@qq.com",
Phone = "18516328675"
});
}
connection.BulkInsert(userList);
// 反射生产的sql语句,然后执行一次,大大提升了批量插入的效率。
insert into tb_user(user_name,password,email,phone)values(@username0,@password0,@email0,@phone0);
insert into tb_user(user_name,password,email,phone)values(@username1,@password1,@email1,@phone1);
insert into tb_user(user_name,password,email,phone)values(@username2,@password2,@email2,@phone2);
.........
insert into tb_user(user_name,password,email,phone)values(@username9999,@password9999,@email9999,@phone9999);
connection.Update<User>(new { UserId = 1, UserName = "张强1" });
connection.Remove<User>(new { UserId = 1, UserName = "张强1" });
List<User> userList = new List<User>();
for (int i = 0; i < 100; i++)
{
userList.Add(new User
{
UserName = "张强" + (i + 1),
Password = "123456",
Email = "237183141@qq.com",
Phone = "18516328675"
});
}
connection.BulkRemove(userList);
8、分页查询。Tuple<int, IEnumerable> GetPageList(this IDbConnection connection, string order, string strWhere = "", object param = null, int pageNum = 1, int PageSize = 10)
var result=connection.GetPageList<User>("userid,username desc"," where userid>@id",new {id=10},2,20);
//当前符合条件的记录数
int count=result.Item1;
// 第21条-40条 实体列表
var list=result.Item2;
connection.GetValue<int>("select count(*) from tb_user where userid>@userid",new {userid=10});
connection.ExecuteQuery<User>("select * from tb_user where userid>@userid",new {userid=10});
3、获取动态类型数据集。IEnumerable ExecuteQueryDynamic(this IDbConnection connection, string strSql, object param = null)
connection.ExecuteQueryDynamic("select * from tb_user where userid>@userid",new {userid=10});
connection.ExecuteNonQuery("delete from tb_user where userid>@userid",new {userid=10});
5、执行无返回值的存储过程。int ExecuteNonQuerySP(this IDbConnection connection, string storeProcedure, IDbDataParameter[] parameters = null)
IDbDataParameter[] parameters =
{
new SqlParameter("@user_id",2)
};
query = _dbConnection.ExecuteQuerySP<User>("sp_test", parameters);
// 获取返回值
IDbDataParameter[] outparameters =
{
new SqlParameter { ParameterName = "@count",DbType=DbType.Int32, Direction = ParameterDirection.Output }
};
_dbConnection.ExecuteNonQuerySP("sp_test_out", outparameters);
var count = outparameters[0].Value;
6、执行查询存储过程。IEnumerable ExecuteQuerySP(this IDbConnection connection, string storeProcedure, IDbDataParameter[] parameters = null)
connection.ExecuteQuerySP<User>("sp_test_no_params");
//1、获取容器
Container container = new Container();
//2、注册类型
container.RegisterType<IUserRepository, UserService>();
//3、创建实例
user = container.Resolve<IUserRepository>();