自己动手写ORM框架(一):目标效果预览(二)

2014-11-24 11:47:21 · 作者: · 浏览: 31
StudentDAL dal = new StudentDAL();
return dal.FindAll();
}
public void Save(StudentEntity entity)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Save(entity);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
}
}
public void Remove(object id)
{
IDbTransaction trans = null;
try
{
trans = TransactionManager.CreateTransaction();
StudentDAL dal = new StudentDAL(trans);
dal.Remove(id);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally {
trans.Dispose();
}
}
}
}
在实体类中配置[Table(Name="Student")],对应数据库中的表名:Student
在实体类中配置[Id(Name=”studentid”,Strategy = GenerationType.SEQUENCE)],表示当前属性是Student表中的主键ID,Name=”studentid”表示该属性Stuid对应Student表列studentid,Strategy表示主键生成策略,这里是自动增长。
在实体类中配置[Column(Name="studentno")],表示当前属性Stuno对应Student表中的列名:studentno(默认属性名=列名)
在实体类中配置[Column(IsInsert=false)],表示当前列值不插入到数据库(默认插入)
在实体类中配置[Column(IsUpdate=false)],表示当前列值不更新到数据库(默认更新)
(实体类映射配置和一些命名参考了JAVA中的JPA)