标签(空格分隔): .NET Develop
一个轻量级ORM工具,基于Dapper.Net
DbContext.SetConfig("YourSqlConnectionString", TimeOut);
表示类的表名,不设置则默认为类名
生成的SQL语句将忽略这一个属性
表示自增列,Insert以及Update时将忽略,不修改
class Customer{
[Key] //标记为Key特性,或者ID的,表示为主键
public int? ID { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
}
var model = new Customer()
{
Name = "Yours Name",
Age = 18
};
model.Insert();
var model = new Customer()
{
Name = "Yours Name Update",
ID = 1
};
model.Update(false); //更新ID为1的属性不为null的实体,此处只更新Name值
model.Update(); //更新所有属性
var model = new Customer()
{
ID = 1
};
model.Delete(); //删除ID=1的
var model = new Customer()
{
Age = 18
};
var collection = model.Query<Customer>(); //返回Age=18的结果集
var model2 = new Customer()
{
ID = 1
};
var result = model2.Get<Customer>(); //返回ID=1的结果
<?xml version="1.0" encoding="utf-8" ?>
<mapper xsi:noNamespaceSchemaLocation="../DTD/SqlMapper.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<select id="testsql">
select name, age from user
<trim prefix="where" prefixOverrides="and">
<if test="args.name.length === 10">
and name = @name
</if>
<if test="args.name.length !== 10">
and id = @name
</if>
<if test="args.keyword !== null">
and name like '%' + @keyword + '%'
</if>
and status = 1
</trim>
</select>
</mapper>
SqlMapper.Init(@"Your WWWroot", "MySqlMapper.xml");
var sql = SqlMapper.Get("testsql", new { name = "123456789", keyword = (string)null });
// select name, age from user where id = @name and status = 1
Console.WriteLine(sql);
var collections = new BaseModel().Query<Customer>("sqlMapperId", new { Name = "name" });