IBatis.net介绍
1. DAL层
[csharp]
public class AccountService
{
public int TestInsertOne(Accounts account)
{ Object obj =Mapper.GetMaper.Insert("Account.sql_InsertOne", account); return (int)obj;
}
public Accounts GetAccount(int id) {
return (Accounts)Mapper.GetMaper.QueryForObject("Account.sql_selectByid", id);
}
public IList<Accounts> GetAccountList()
{ return Mapper.GetMaper.QueryForList<Accounts>("Account.sql_selectAll", null);
}
2. Model 层:
[csharp]
public class Accounts
{
public int Id { get; set; }
public string Item { get; set; }
public float Money { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public int Day { get; set; }
public DateTime CreateOn { get; set; }
public string Level { get; set; }
}
3. 映射层
[html]
<?xml version="1.0" encoding="utf-8" ?><sqlMap namespace="Account" xmlns="https://ibatis.apache.org/mapping" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
<alias>
<!-- alias:取别名 assembly:表示类所在的文件 type:表示该类的完整的名称 -->
<typeAlias alias="Account" assembly="IBatisDemo.Model.dll" type="IBatisDemo.Model.Accounts" /> </alias>
<resultMaps>
<resultMap id="Account-result" class="Account">
<result property="Id" column="id"/>
<result property="Item" column="Item"/>
<result property="Year" column="Year"/>
<result property="Month" column="Month"/>
<result property="Day" column="Day"/>
<result property="CreateOn" column="CreateOn"/>
<result property="Level" column="Level"/>
</resultMap>
</resultMaps>
<statements>
<select id="sql_selectByid" resultMap="Account-result">
select * from Accounts
<dynamic prepend="where">
<isParameterPresent property="id" prepend="">
[id] = #id#
</isParameterPresent>
</dynamic>
</select>
<select id="sql_selectAll" resultMap="Account-result">
select * from Accounts </select>
<insert id="sql_InsertOne" parameterClass="Account">
insert into Accounts (Item,Money,Year,Month,Day,CreateOn,Level)
values (#Item#, #Money#, #Year#, #Month#, #Day#, #CreateOn#, #Level# )
<selectKey type="post" resultClass="int" property="Id">
SELECT CAST(@@IDENTITY as int) as Id
</selectKey>
</insert>
</statements>
</sqlMap>
上一篇: 解决jsp参数传递乱码的问题