最终实现后达到的效果,只需写少量代码就可实现CURD操作。
DAL层代码:
StudentDAL代码
public class StudentDAL
{
EntityManager entityManager = EntityManagerFactory.CreateEntityManager();
public StudentDAL() { }
public StudentDAL(IDbTransaction transaction)
{
entityManager.Transaction = transaction;
}
public List FindAll()
{
return entityManager.FindAll();
}
public int Save(StudentEntity entity)
{
return entityManager.Save(entity);
}
public int Update(StudentEntity entity)
{
return entityManager.Update(entity);
}
public int Remove(StudentEntity entity)
{
return entityManager.Remove(entity);
}
public int Remove(object id)
{
return entityManager.Remove(id);
}
public List FindById(object id)
{
return entityManager.FindById(id);
}
public List FindByProperty(string propertyName,object value)
{
return entityManager.FindByProperty(propertyName, value);
}
}
实体类与
数据库表的映射关系配置:
StudentEntity代码
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Orm.CustomAttributes;
namespace Entity
{
[Serializable]
[Table(name="Student")]
public class StudentEntity
{
private string stuid;
private string stuno;
private string name;
private int sex;
private int age;
private string address;
private string telphone;
[Id(Name=”stuid”,Strategy = GenerationType.SEQUENCE)]
public string Stuid
{
get { return stuid; }
set { stuid = value; }
}
[Column(Name = "studentno")]
public string Stuno
{
get { return stuno; }
set { stuno = value; }
}
[Column(IsInsert = true)]
public string Name
{
get { return name; }
set { name = value; }
}
[Column(IsUpdate = true)]
public int Sex
{
get { return sex; }
set { sex = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string Telphone
{
get { return telphone; }
set { telphone = value; }
}
}
}
BLL层代码:
StudentBLL代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL;
using Entity;
using System.Orm.DBTransaction;
namespace BLL
{
public class StudentBP
{
public List FindAll()
{