Skip to main content

Posts

Learning iBATIS - SqlMap API( Topics)

#1 Using an external parameter map Along with providing the same functionality as inline parameter mapping, using an external parameter amp has the added benefit of improved performance and additional validation at load time(which means that fewer errors slip through the cracks during testing for your users to find at runtime). here is an example: <parameterMap id="fullParameterMapExample" class="Account"> <parameter property="accountId" jdbcType="NUMBER" /> <parameter property="username" jdbcType="VARCHAR" /> <parameter property="password" jdbcType="VARCHAR" /> <parameter property="memberSince" jdbcType="TIMESTAMP" /> <parameter property="firstName" jdbcType="VARCHAR" /> <parameter property="lastName" jdbcType="VARCHAR" /> <parameter property="address1" jdbcType="VARCHAR"...

Learning iBATIS - The SqlMap API

The SqlMapClient interface has over 30 methods on it. #1 The queryForObject() methods The queryForObject() methods are used to get a single row from the database into a Java object, and come with two signatures: ■ Object queryForObject(String id, Object parameter) throws SQLException; ■ Object queryForObject(String id, Object parameter, Object result) throws SQLException; The second form is useful if you have an object that cannot be easily created because of a protected constructor or the lack of a default constructor. #2 The queryForList() methods The queryForList() methods are used to get one or more rows from the database into a List of Java objects, and like queryForObject(), they also come in two versions: ■ List queryForList(String id, Object parameter) throws SQLException; ■ List queryForList(String id, Object parameter, int skip, int max) throws SQLException; #3 The queryForMap() methods The queryForMap() methods return a Map (instead of a List)...

Learning iBATIS - the retrieving Operation

#1 Retrieving  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      More than anything else, iBATIS is an alternative to writing JDBC code. API like JDBC are powerful, but tend to be verbose and repetitive. Look at below example, 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(...

Learning iBATIS - The Web Application Layers

Presentation   -------      |                          |     \ /                        \/ Business Logic --->Business Object Model     |                          /\    \ /                          | Persistence   --------    |   \ / Database #1 The Business Object Model The BO serves as the foundation for the rest of the application. It is the object-oriented representation of the problem domain, and therefore the classes that make up the businss object model are sometimes called domain classes. All other layers use the business object model to represent data and perform certain business logic functions. Application designers usually start wi...

Learning iBATIS - Where iBATIS fits

Nearly any well-written piece of software uses a layered design. A layered design separates the techical responsibilities of an application into cohesive parts that isolate the implementation details of a particular technology or interface. Presentation   -------      |                          |     \ /                        \/ Business Logic --->Business Object Model     |                          /\    \ /                          | Persistence   --------    |   \ / Database iBATIS is a persistence layer framework. The persistence layer sits between the business logic layer of the application and the database. This separation is important to ...

Learning iBATIS - understanding iBATIS

iBATIS is a hybrid solution. It takes the best ideas from other solutions and creates synergy between them. It takes SQL out of the source code and into a place where we can work with it more naturally,we need to link it back to the software so that it can be executed in a way that is useful. iBATIS uses Extensible Markup Language(XML) to encapsulate SQL. Using XML< iBATIS maps the inputs and outputs of the statement. Most SQL statements have one or more parameters and produce some sort of tabulated results. That is, results are organized into a series of columns and rows. iBATIS allows you to easily map both parameters and results to properties of objects. Consider the next example:   <select id="categoryById" parameterClass="string" resultClass="category">       SELECT CATEGORYID, NAME, DESCRIPTION       FROM CATEGORY       WHERE CATEGORYID = #categoryId#   </select> Notice the XML element surroundi...

Learning iBATIS - Why iBATIS?

iBATIS and Java Dynamic SQL is currently the most popular means of accessing relational databases from modern languages. It has the advantage of flexibility. The SQL can be manipulated at runtime based on different parameters or dynamic application functions. The language such as Java includes a standard API for database access.  The following is a simple example of Dynamic SQL in Java: String name; Date hiredate; String sql = "SELECT emp_name, hire_date" + " FROM employee WHERE emp_num = ? "; Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement (sql); ps.setInt (1, 28959); ResultSet rs = ps.executeQuery(); while (rs.next) { name = rs.getString("emp_name"); hiredate = rs.getDate("hire_date"); } rs.close(); conn.close(); Without a doubt, Dynamic SQL is not elegant at all. The APIs are often complex and very verbose, Using these frameworks generally results in a lot of code, which is often...