博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IBatisNet1.5 -- 映射文件--Statements
阅读量:6582 次
发布时间:2019-06-24

本文共 8791 字,大约阅读时间需要 29 分钟。

       前段时间写了一篇关于配置文件的,由于最近太忙了,一直没接着往下写,现在抽出时间来继续写IBatisNet1.5学习及使用经历. 
       通过上一篇文章,大概了解了Ibatisnet中SqlMap的配置,但仅仅有SqlMap我们还不能进行IBatisnet的操作,我们还需要根据数据库表来编写映射文件.  

      Ibatisnet的xml映射文件主要包括这几个部分,resultMaps,parameterMaps,下面分别说一下IBatisnet的映射文件中的Elements及其Attributes.

一、Statements
    简单的说,这一部分里放的是对数据库操作的SQL语句及存储过程等,它包括以下子元素:

Statement Element Attributes Child Elements Methods
<statement>
            id            parameterClass            resultClass            listClass            parameterMap            resultMap            cacheModel
All dynamic elements
Insert            Update            Delete            All query methods
<insert>
            id            parameterClass            parameterMap
All dynamic elements            
Insert            Update            Delete
<update>
            id            parameterClass            parameterMap            extends
All dynamic elements            
Insert            Update            Delete
<delete>
            id            parameterClass            parameterMap            extends
All dynamic elements            
Insert            Update            Delete
<select>
            id            parameterClass            resultClass            listClass            parameterMap            resultMap            cacheModel            extends
All dynamic elements            
All query methods
<procedure>
            id            parameterMap            resultClass            resultMap            cacheModel
All dynamic elements
Insert            Update            Delete            All query methods
 然后说一下,这些Statement Element的Attributes
 1、id
       statement的标识,每个statement必须的attribute,还需注意的是,在所有的sqlmap中,它都是唯一的。
2、parameterMap
       parameterMap的值会按照顺序匹配statement中的sql语句中的?(占位符)。如下:
None.gif
<
parameterMaps
>
None.gif    
<
parameterMap 
id
="simpleinsert"
 class
="Employees"
>
None.gif        
<
parameter 
property
="id"
/>
None.gif        
<
parameter 
property
="empcode"
/>
None.gif    
</
parameterMap
>
None.gif
</
parameterMaps
>
 
None.gif
None.gif
<
statement 
id
="insertEmployee"
 parameterMap
="simpleinsert"
>
None.gif      insert into Employees (Id, EmpCode) values (?,?);
None.gif
</
statement
>

上面代码中的parameterMap中我们定义了两个参数id,empcode,而在下面的statement中的insert语句的第一个?就会被id的值所取代,第二个?的值为empcode的值。当然IBatisnet中并不是这样一种参数的定义方式,另外还有两种inline,named方式。

3、parameterClass
      还是看上面的例子,如果我们没有对statement指定parameterMap,我们还可以通过指定parameterClass并使用inline parameter来完成同样的目的。如下:

None.gif
<
statement 
id
="insertEmployee"
 parameterClass
="OPS.Domain.Employees,OPS.Domain"
>
None.gif      insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
None.gif
</
statement
>

parameterClass也可以直接写成Employees。

None.gif
<
statement 
id
="insertEmployee"
 parameterClass
="Employees"
>
None.gif      insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
None.gif
</
statement
>

4、resultMap

      通过设置resultMap,我们可以控制如何从查询的结果集中提取数据。比如我们只需要结果集中的某几列就可以来设置resultMap中的result来完成。

None.gif
<
resultMaps
>
None.gif        
<
resultMap 
id
="SelectResult"
 class
="Employees"
>
None.gif            
<
result 
property
="Id"
 column
="id"
 
/>
None.gif            
<
result 
property
="EmpCode"
 column
="empcode"
 
/>
None.gif            
<
result 
property
="EmpPassword"
 column
="emppassword"
 
/>
None.gif            
<
result 
property
="EmpName"
 column
="empname"
 
/>
None.gif            
<
result 
property
="EmpSex"
 column
="empsex"
 
/>
None.gif            
<
result 
property
="EmpAge"
 column
="empage"
 
/>
None.gif            
<
result 
property
="EmpMail"
 column
="empmail"
 
/>
None.gif            
<
result 
property
="EmpPhoneNo"
 column
="empphoneno"
 
/>
None.gif            
<
result 
property
="Description"
 column
="description"
 
/>
None.gif            
<
result 
property
="IsAdmin"
 column
="isadmin"
 
/>
None.gif            
<
result 
property
="IsDelete"
 column
="isdelete"
 
/>
None.gif        
</
resultMap
>
None.gif
</
resultMaps
>

 

None.gif
<
statement 
id
="SelectEmployees"
 resultMap
="Employees"
>
None.gif      select * from Employees
None.gif
</
statement
>

上面这个statement的查询结果就返回Employees的所有列,如果你只想返回其中几列,只需要更改resultMap就可以了。

5、resultClass
      对于控制查询结果的返回值,我们也可以通过设置resultClass来代替resultMap。指定的resultClass将自动对应到查询结果的列。

None.gif
<
select 
id
="SelectEmployees"
 parameterClass
="int"
 resultClass
="Employees"
>
None.gif            Select 
None.gif                  id as Id,
None.gif                  empcode as EmpCode,
None.gif                  emppassword as EmpPassword,
None.gif                  empname as EmpName,
None.gif                  empsex as EmpSex,
None.gif                  empage as EmpAge,
None.gif                  empmail as EmpMail,
None.gif                  empphoneno as EmpPhoneNo,
None.gif                  description as Description,
None.gif                  isadmin as IsAdmin,
None.gif                  isdelete as IsDelete
None.gif            From Employees
None.gif
</
select
>

 Employees.cs应包括Id,EmpCode,EmpPassword,EmpName,EmpSex,EmpAge,EmpMail,EmpPhoneNo,Description,IsAdmin,IsDelete这些属性。

ContractedBlock.gif
ExpandedBlockStart.gif
Employee.cs
None.gifusing System;
None.gif
None.gif
namespace OPS.Domain
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Employees 
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    [Serializable]
InBlock.gif    
public class Employees
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Employees()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
InBlock.gif        
private int id;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// Id
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return id;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{id = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string empcode;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpCode
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpCode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empcode;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empcode = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string emppassword;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpPassword
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpPassword
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return emppassword;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{emppassword = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string empname;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpName
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empname;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empname = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string empsex;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpSex
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpSex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empsex;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empsex = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private int? empage;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpAge
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public int? EmpAge
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empage;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empage = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string empmail;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpMail
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpMail
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empmail;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empmail = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string empphoneno;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// EmpPhoneNo
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string EmpPhoneNo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return empphoneno;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{empphoneno = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string description;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// Description
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string Description
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return description;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{description = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string isadmin;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// IsAdmin
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string IsAdmin
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return isadmin;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{isadmin = value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
private string isdelete;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////<sumary>
InBlock.gif        
/// IsDelete
ExpandedSubBlockEnd.gif        
///</sumary>
InBlock.gif        public string IsDelete
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{
return isdelete;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{isdelete = value;}
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

     

6、listClass
      为了提供返回IList of Objects的能力,statement中可以使用强类型的自定义集合类来做为listClass Attribute的值。这个类必须实现System.Collections.CollectionBase。当然statement还需指定resultClass,使得DataMapper知道如何在集合中处理这种类型。使用方法如下:

ContractedBlock.gif
ExpandedBlockStart.gif
EmployeeCollection
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.Text;
None.gif
None.gif
namespace OPS.Domain
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class EmployeeCollection : CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public EmployeeCollection() dot.gif{ }
InBlock.gif
InBlock.gif        
public Employees this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (Employees)List[index]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ List[index] = value; }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public int Add(Employees value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return List.Add(value);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public void AddRange(Employees[] value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < value.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Add(value[i]);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public void AddRange(EmployeeCollection value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < value.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Add(value[i]);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public bool Contains(Employees value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return List.Contains(value);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public void CopyTo(Employees[] array, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.CopyTo(array, index);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public int IndexOf(Employees value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return List.IndexOf(value);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public void Insert(int index, Employees value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Insert(index, value);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
public void Remove(Employees value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Remove(value);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

 

None.gif
<
select id
=
"
SelectEmployees
"
 parameterClass
=
"
int
"
 resultClass
=
"
Employess
"
 listClass
=
"
EmployeeCollection
"
>
None.gif            Select 
None.gif                  id 
as
 Id,
None.gif                  empcode 
as
 EmpCode,
None.gif                  emppassword 
as
 EmpPassword,
None.gif                  empname 
as
 EmpName,
None.gif                  empsex 
as
 EmpSex,
None.gif                  empage 
as
 EmpAge,
None.gif                  empmail 
as
 EmpMail,
None.gif                  empphoneno 
as
 EmpPhoneNo,
None.gif                  description 
as
 Description,
None.gif                  isadmin 
as
 IsAdmin,
None.gif                  isdelete 
as
 IsDelete
None.gif            From Employees
None.gif
</
select
>

7、cacheModel
      如果你希望将查询结果集装入缓存,你可以通过设置statement的cacheModel来实现。首先需要配置缓存的模式,如下:

None.gif
<
cacheModel 
id
="employee-cache"
 implementation
="LRU"
>
None.gif  
<
flushInterval 
hours
="24"
/>
None.gif  
<
flushOnExecute 
statement
="insertEmployee"
/>
None.gif  
<
flushOnExecute 
statement
="updateEmployee"
/>
None.gif  
<
flushOnExecute 
statement
="deleteEmployee"
/>
None.gif  
<
property 
name
="size"
 value
="1000"
 
/>
None.gif
</
cacheModel
>
None.gif
关于缓存模式,后面会专门介绍。
在statement中使用缓存:
None.gif
<
statement 
id
="selectAllEmployees"
 cacheModel
="employee-cache"
>
None.gif  select * from Employees
None.gif
</
statement
>
8、extends
      继承的属性也非常的有用,有很多SQL的操作的主题都一样,只是,Where子句不同,或Order By的内容不同等等,我们都可以将主体独立出来,作为BASE,然后写不同的条件或排序规则来继承它。如下:
None.gif
    
<
select 
id
="SelectEmployees"
 parameterClass
="int"
 resultMap
="SelectResult"
>
None.gif            Select 
None.gif                  id,
None.gif                  empcode,
None.gif                  emppassword,
None.gif                  empname,
None.gif                  empsex,
None.gif                  empage,
None.gif                  empmail,
None.gif                  empphoneno,
None.gif                  description,
None.gif                  isadmin,
None.gif                  isdelete
None.gif            From Employees
None.gif        
</
select
>
None.gif
None.gif    
<!--
员工列表
-->
None.gif    
<
select 
id
="EmployeeList"
 parameterClass
="int"
 resultMap
="SelectResult"
 extends
="SelectEmployees"
>
None.gif      Where isdelete='n'
None.gif    
</
select
>
None.gif
None.gif    
<!--
员工明细
-->
None.gif    
<
select 
id
="EmployeeDetail"
 parameterClass
="int"
 resultMap
="SelectResult"
 extends
="SelectEmployees"
>
None.gif      Where id=#Id#
None.gif    
</
select
>
None.gif
None.gif    
<!--
登录验证
-->
None.gif    
<
select 
id
="Login"
 parameterClass
="int"
 resultMap
="SelectResult"
 extends
="SelectEmployees"
>
None.gif      Where empcode=#EmpCode# and emppassword=#EmpPassword# and isdelete='n'
None.gif    
</
select
>
None.gif
None.gif    
<!--
判断员工编号是否重复
-->
None.gif    
<
select 
id
="CheckEmpCode"
 parameterClass
="String"
 resultMap
="SelectResult"
 extends
="SelectEmployees"
>
None.gif      Where empcode=#EmpCode# and isdelete='n'
None.gif    
</
select
>
OK,先写到这里。
你可能感兴趣的文章
每日一记--cookie
查看>>
IOS 7 Study - UISegmentedControl
查看>>
八、通用类型系统
查看>>
JQuery的ajaxFileUpload的使用
查看>>
关于Integer类中parseInt()和valueOf()方法的区别以及int和String类性的转换.以及String类valueOf()方法...
查看>>
ios 控制器的生命周期
查看>>
JavaScript 特殊效果代码
查看>>
【?】codeforces721E Road to Home(DP+单调队列)
查看>>
MySQL 仅保留7天、一个月数据
查看>>
Diff Two Arrays
查看>>
下拉菜单
查看>>
[清华集训2014]玛里苟斯
查看>>
【MVC+EasyUI实例】对数据网格的增删改查(上)
查看>>
Project Euler 345: Matrix Sum
查看>>
你可能不知道的技术细节:存储过程参数传递的影响
查看>>
.htaccess 基础教程(四)Apache RewriteCond 规则参数
查看>>
UVM中的class--2
查看>>
ORACLE 存储过程异常捕获并抛出
查看>>
root用户重置其他密码
查看>>
Oracle推断值为非数字
查看>>