#1 Retrieving
<select id="getAddress"
parameterClass="int"
resultClass="Address">
SELECT
ADR_ID as id,
ADR_DESCRIPTION as description,
ADR_STREET as street,
ADR_CITY as city,
ADR_PROVINCE as province,
ADR_POSTAL_CODE as postalCode
FROM ADDRESS
WHERE ADR_ID = #id#
</select>
Address address = (Address) sqlMap.queryForObject("getAddress", new Integer(5));
public Employee getEmployee (int id) throws SQLException {
Employee employee = null;
String sql = "SELECT * FROM EMPLOYEE " +
"WHERE EMPLOYEE_NUMBER = ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
employee = null;
while (rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("ID"));
employee.setEmployeeNumber(rs.getInt("EMPLOYEE_NUMBER"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
employee.setTitle(rs.getString("TITLE"));
}
} finally {
try {
if (rs != null) rs.close();
} finally {
try {
if (ps != null) ps.close();
} finally {
if (conn != null) conn.close();
}
}
}
return employee;
}
It's easy to see the overhead created by the JDBC API. Every line is necessary, though, so there's no easy way to reduce it. At best, a few of the lines can be extracted into utility methods, most notably the closing of resources such as the PreparedStatement and the ResultSet.
Under the hood, iBATIS will run nearly the same JDBC code. iBATIS will get a connection to the database, set the parameters, execute the statement, retrieve the results, and close all of the resources. However, the amount of code that you need to write is significantly reduced.
As you've seen in earlier examples, it's a very simple single line of code:
Employee emp = (Employee) sqlMap.queryForObject("getEmployee", new Integer(5));
There's nothing to it. This line of code executes the statement, sets the parameters, and retrieves the results as a real java object. The SQL is encapsulated and externalized neatly in a XML File. iBATIS manages all of the resources behind the scenes, and the net effect is the same as the JDBC code we saw above.
- A sample SQL Mapping descriptor
<select id="getAddress"
parameterClass="int"
resultClass="Address">
SELECT
ADR_ID as id,
ADR_DESCRIPTION as description,
ADR_STREET as street,
ADR_CITY as city,
ADR_PROVINCE as province,
ADR_POSTAL_CODE as postalCode
FROM ADDRESS
WHERE ADR_ID = #id#
</select>
- Usage
Address address = (Address) sqlMap.queryForObject("getAddress", new Integer(5));
- How it works
public Employee getEmployee (int id) throws SQLException {
Employee employee = null;
String sql = "SELECT * FROM EMPLOYEE " +
"WHERE EMPLOYEE_NUMBER = ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
employee = null;
while (rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("ID"));
employee.setEmployeeNumber(rs.getInt("EMPLOYEE_NUMBER"));
employee.setFirstName(rs.getString("FIRST_NAME"));
employee.setLastName(rs.getString("LAST_NAME"));
employee.setTitle(rs.getString("TITLE"));
}
} finally {
try {
if (rs != null) rs.close();
} finally {
try {
if (ps != null) ps.close();
} finally {
if (conn != null) conn.close();
}
}
}
return employee;
}
It's easy to see the overhead created by the JDBC API. Every line is necessary, though, so there's no easy way to reduce it. At best, a few of the lines can be extracted into utility methods, most notably the closing of resources such as the PreparedStatement and the ResultSet.
Under the hood, iBATIS will run nearly the same JDBC code. iBATIS will get a connection to the database, set the parameters, execute the statement, retrieve the results, and close all of the resources. However, the amount of code that you need to write is significantly reduced.
As you've seen in earlier examples, it's a very simple single line of code:
Employee emp = (Employee) sqlMap.queryForObject("getEmployee", new Integer(5));
There's nothing to it. This line of code executes the statement, sets the parameters, and retrieves the results as a real java object. The SQL is encapsulated and externalized neatly in a XML File. iBATIS manages all of the resources behind the scenes, and the net effect is the same as the JDBC code we saw above.
Comments
Post a Comment