Top Banner
October 30, 2004 JUG Sardegna Seminar @ DIEE 1 JDBC vs iBATIS A case study Fabrizio Gianneschi Atlantis S.p.A. [email protected]
25

JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

Apr 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 1

JDBC vs iBATIS

A case study

Fabrizio Gianneschi

Atlantis [email protected]

Page 2: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 2

About iBATIS

• Open source framework that reduces the complexity to read/save Java objects from a database (persistence)

• Decouples Java code from SQL

• Avoids the necessity to use JDBC

• It is NOT an object-relational mapper (suchas Hibernate)

• Simplifies the developer’s life ☺

Page 3: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 3

10 Reasons to use iBatis

1. Works with any database that has a JDBC driver

(no plugins required)

2. Configurable caching (including dependencies)

3. Local and Global transaction support and

management (JTA)

4. Simple XML mapping document structure

5. Supports Map, Collection, List and Primitive

Wrappers (Integer, String etc.)

Source: http://www.ibatis.com

Page 4: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 4

10 Reasons to use iBatis

6. Supports JavaBeans classes (get/set methods)

7. Supports complex object mappings (populating

lists, complex object models etc.)

8. Object models are never perfect (no changes

required!)

9. Database designs are never perfect (no

changes required!)

10. You already know SQL, why waste time

learning something else?

Source: http://www.ibatis.com

Page 5: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 5

Components

iBATIS is divided into two main components:

•• SQLMapsSQLMaps

Permits to read/save Java objects into relational

DBMS without using JDBC and without mixing

Java and SQL code

•• DAODAO

“Is an abstraction layer that hides the details of

your persistence solution and provides a common

API to the rest of your application” (src: iBATIS)

Page 6: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 6

SQLMaps

Page 7: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 7

SQLMaps in pills

• Based on XML configuration files; you have to give a

name (or an id) to each query used in the application

• Query input parameters could be primitives or simple

classes (Integer, String…) or HashMap

• In complex cases, application classes could be used

directly (es. VO, DTO)

• Particular DB situations are handled using column /

property mappings

• The same principles are also valid for output parameters

Page 8: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 8

<?xml version="1.0" encoding="UTF-8" ?>

<sqlMap namespace="Persona">

<select id="findPersonaById“ resultClass =

“com.gruppoatlantis.www.verbali.dto.PersonaDTO“>

SELECT *

FROM persone_view

WHERE idpersona=#value#

</select>

Simple Mapping

Page 9: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 9

<?xml version="1.0" encoding="UTF-8" ?>

<sqlMap namespace="Persona">

<resultMap id="personaMap“

class="com.gruppoatlantis.www.verbali.dto.PersonaDTO">

<result property="id" column="idpersona"/>

<result property="cognome" column="cognome"/>

<result property="nome" column="nome"/>

<result property="matricola" column="matricola"/>

<result property=“livello" column=“eta“ jdbcType="INTEGER“

nullValue="0" />

</resultMap>

<select id="findPersonaById" resultMap="personaMap">

SELECT *

FROM persone_view

WHERE idpersona=#value#

</select>

Complex

Mapping

Page 10: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 10

SQLMaps in pills• To execute a query within Java classes, you have to use an

instance of the classcom.ibatis.sqlmap.client.SqlMapClient

• The most used methods are

queryForList and queryForObject on which you pass

the name of query and an object as a parameters

As an example:

SQLMapClient sqlMap = …;

String queryName = “findPersonaById“;

Integer id = new Integer(15);

PersonaDTO p = (PersonaDTO) sqlMap.queryForObject(queryName, id);

Page 11: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 11

SQLMaps case study

Page 12: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 12

Scenario

• Simple Intranet application based on J2EEtechnology (JSP, JSTL, Servlet), in production since January 2004, that process and mantains the reports of the corporate’s internal meetings.

• Data access through DAO/JDBC

• PostgreSQL database (14 tables, 3 views, 9 sequences, 1 function)

Page 13: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 13

Front controller

(/ctrl)

web.xml

:AbstractAction

DAOFactory

:AbstractDAO

DB

JSP

request

response

1. use

2. create

3. use

4. create

5. return DAO

6. use

7. return

DTO8. handle

DTO

9. return

URL

10. forward

11. return

Architecture (extract)

Page 14: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 14

LOC count before iBATIS*AbstractDAO 26

AttivitaPostgresDAO 524

CategoriaPostgresDAO 151

MascheraDAO 221

OdgPostgresDAO 131

PersonaPostgresDAO 388

ProgettoPostgresDAO 112

RiunionePostgresDAO 514

RuoloPostgresDAO 75

StatoAttivitaPostgresDAO 124

TOTAL 2266

* Only DAO Layer

Page 15: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 15

JDBC Java method (1/3)

public List findXyZ(String par1) {

Connection conn = null;

List retColl = null;

try {

conn = getConnection(); //implemented on superclass

String query = “SELECT * FROM table WHERE a=?;”;

PreparedStatement ps = conn.prepareStatement(

query, ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

ps.setString(1 , par1); // sets query parameters

ResultSet rs = ps.executeQuery();

/(… continue …)

Page 16: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 16

retColl = new ArrayList(); // result collection

while ( rs.next() ) { // iterate over query results

// an object for each row

MyClass obj = new MyClass();

obj.setField1( rs.getInt(1) );

obj.setField2( rs.getDate(2) );

obj.setFieldN( …. );

retColl.add(obj);

}

rs.close(); // release resources

ps.close();

return retColl;

//(… continuea …)

JDBC Java method (2/3)

Page 17: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 17

//…

} catch (SQLException ex) {

// error handling

} finally {

// implemented on superclass

closeConnection(conn);

}

} // End of method

JDBC Java method (3/3)

Page 18: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 18

iBATIS Java method

public List findXyZ(String par1) {

try{

return sqlMap.queryForList(“findXyZ”,par1);

}catch(SQLException ex){

// error handling

}

} // End of method

Page 19: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 19

JDBC-iBATIS Comparison

1. Obtain the db Connection

2. Create the statement

3. Set the input parameters

4. Execute the statement

5. Create the result Collection

6. For each row fetched:

a) Create an object

b) Set object’s properties using row’s colums

c) Add the object to the Collection

7. Release resources

8. Close the Connection

9. Return the Collection

1. Obtain the SqlMap object

a) Prepare complex parameters

(optional)

2. Invoke the query passing the

parameters

3. Return the result

JDBC iBATIS

Page 20: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 20

LOC count

JDBC iBATIS

AbstractDAO 26 0

AttivitaPostgresDAO 524 199

CategoriaPostgresDAO 151 66

MascheraDAO 221 73

OdgPostgresDAO 131 51

PersonaPostgresDAO 388 165

ProgettoPostgresDAO 112 26

RiunionePostgresDAO 514 202

RuoloPostgresDAO 75 34

StatoAttivitaPostgresDAO 124 48

TOTALE 2266 864

-62%of Java code

Page 21: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 21

JDBC vs iBATIS

2266 2266

864

1471

0

500

1000

1500

2000

2500

Java Java + XML

LOC count

JDBC

iBATIS

Page 22: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 22

Performance

• Comparable with the “classic” approach

enabling some options(http://raibledesigns.com/page/rd?anchor=persistence_options_with_existing_sql)

• Advanced Object Caching

• Lazy loading

• Stored procedures support

• The SQL code is visible and accessible for

optimization

Page 23: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 23

Conclusions

• The introduction of iBATIS into the project

has been extremely favorable in terms of

LOC count reduction and general clarity

• The business logic and the old SQL queries

were not modified.

• The learning curve was very good (< 1 day

for the first working DAO)

Page 24: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 24

iBATIS – Hibernate comparison

� ��Documentation

� ��Community

� � �� �Features

�� �Simplicity

� � ��OR mapping

�� � �Suitable in the middle/end

of the project

�� �Suitable since the

beginning of the project

HibernateiBATIS

Page 25: JDBC vs iBATIS - Java User Group Sardegna...October 30, 2004 JUG Sardegna Seminar @ DIEE 10 SQLMaps in pills • To execute a query withinJava classes, you have to use an instance

October 30, 2004 JUG Sardegna Seminar @ DIEE 25

References

Official references:

• http://www.ibatis.com

• http://www.ibatis.com/common/sqlmaps.html

• http://opensource.thoughtworks.com/projects/ibatis.jsp

Articles:

• http://www.sixty4bit.com/mt/archives/2003/12/17/persistence_layer_re

view.html

• http://www.developer.com/db/article.php/3346301