Java Reference
In-Depth Information
<select id="SelectEmployee" parameterClass="int"
resultMap="EmployeeResult">
select *
from
Employees
where
EmployeeID = #value#
</select>
Notice how we were able to simplify the
SQL
statement, but at the cost of an addi-
tional
XML
element called
<resultMap>
. This is a trade-off that you'll make often,
but as with the Java version, sometimes the
<resultMap>
is necessary because it
provides added functionality to your results processing.
Now that we've mapped the statement, we can call it from C# as follows:
Employee emp =
sqlMap.QueryForObject<Employee>("SelectEmployee", 1);
// You can get a sense of the result by writing out to the
// console⦠which should return "1: Nancy Davolio"
Console.WriteLine(emp.ID + ": " + emp.FirstName + " " +
emp.LastName);
Nonquery statements are no different in i
BATIS.NET
. The following examples
show how we define an insert statement.
First, take a look at this code:
<insert id="InsertEmployee" parameterClass="Employee">
insert into Employees
( FirstName, LastName )
values
( #FirstName#, #LastName# )
<selectKey resultClass="int" property="ID" >
select @@IDENTITY
</selectKey>
</insert>
For the most part, this is just a simple insert statement. However, notice the
bolded section of the statement. The embedded
<selectKey>
stanza should be
familiar to any i
BATIS
user: it is the mechanism used to acquire the values of gen-
erated primary key columns.
When we call this statement from C#, the generated key is returned from the
Insert()
method, and it is also set as the value of the
ID
property of the
Employee
instance passed to the
Insert()
method. Let's take a look at the example C# code
to make this clearer:
