54
Apache iBatis Getting Up to Speed Carsten Ziegeler [email protected] Day Software

Apache iBatis (ApacheCon US 2007)

Embed Size (px)

DESCRIPTION

Presentation from the ApacheCon US 2007 in Atlanta.

Citation preview

Page 1: Apache iBatis (ApacheCon US 2007)

Apache iBatis

Getting Up to SpeedCarsten [email protected]

Day Software

Page 2: Apache iBatis (ApacheCon US 2007)

• Apache Software Foundation Member– Cocoon, Excalibur, Pluto, Felix, Incubator,

Sling, Sanselan– PMC: Cocoon, Incubator, Portals, Felix,

Excalibur (Chair)

• Senior Developer at Day Software• Article/Book Author, Technical Reviewer• JSR 286 spec group (Portlet API 2.0)

About Carsten Ziegeler

Page 3: Apache iBatis (ApacheCon US 2007)

• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary

3

Apache iBatis

Visit our booth for

infos, discussions an

d jo

bs!

Page 4: Apache iBatis (ApacheCon US 2007)

4

A Common Problem

• Reading from / writing to a database in Java• Mapping of result sets to objects• Mapping of objects to tables/columns• Object Relational Mapping (ORM)

Page 5: Apache iBatis (ApacheCon US 2007)

5

Why do I need iBatis?

• Several ORM solutions out there– Hibernate, Apache OpenJPA, Apache OJB– Very sophisticated, but "heavy" solutions– Usable for literally everything– Setting up, configuring, using takes time

• What about javax.sql/JDBC? – ….if you think that SQL and Java code mixes

well

Page 6: Apache iBatis (ApacheCon US 2007)

• Apache iBatis fits in-between– Small library– Simple setup– Easy to use– Uses data maps

6

Why do I need iBatis?

Page 7: Apache iBatis (ApacheCon US 2007)

7

Use the Right Tool!

• Not every tool is suited for every use case!– Analyze requirements– Decide for the tool– Constantly check decision

• Apache iBatis is not suited for all ORM problems

Page 8: Apache iBatis (ApacheCon US 2007)

8

Some Typical Scenarios for iBatis

• Reading dictionaries (like i18n translations)• Performing complex and nested queries

– Full SQL support

• Reading configuration• Writing log statements• Displaying query results

Page 9: Apache iBatis (ApacheCon US 2007)

9

The Project

• Popular tool – Abstracting JDBC – Working at the SQL level

• Started by Clinton Begin– Part of Apache since 2005– Top level project

• Current version 2.x (> 3 years old)– Next major version in design discussions

Page 10: Apache iBatis (ApacheCon US 2007)

10

The Project

• Two frameworks– iBatis Data Mapper– iBatis Data Access Objects

• Implementations for different languages– Java, .Net, Ruby/Rails

• Focus on the Java Data Mapper

Page 11: Apache iBatis (ApacheCon US 2007)

11

How Does it Work?

• SQL statements are defined in XML• Statements can contain placeholders• Placeholders are replaced on execution• SQL query result is mapped to objects

Page 12: Apache iBatis (ApacheCon US 2007)

12

Some Advantages

• Retain the full power of SQL– Functions like avg, sum, count etc.– Mapping across tables

• Single point of configuration• Short integration time• Very good documentation• Caching of query results

Page 13: Apache iBatis (ApacheCon US 2007)

13

Caveats

• No transparent persistence• No change detection• Explicit mapping required• No implicit operations (like cascaded

updates, deletes etc.)

Page 14: Apache iBatis (ApacheCon US 2007)

14

File Name Description Required

ibatis-common.jar iBATIS Common Utilities YES

ibatis-sqlmap.jar iBATIS Data Mapper Framework YES

ibatis-dao.jar iBATIS Data Access Framework NO

Installation

• Download and add to classpath• Optional 3rd party jars for additional stuff

– Refer to the documentation

Page 15: Apache iBatis (ApacheCon US 2007)

15

Data Mapper Concept

Page 16: Apache iBatis (ApacheCon US 2007)

16

Apache iBatis

• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary

Page 17: Apache iBatis (ApacheCon US 2007)

17

Four Basic Steps

1. Write Java beans (optional)2. Create mapping configuration3. Specify the SQL configuration4. Write the app code executing the

statements

Page 18: Apache iBatis (ApacheCon US 2007)

• Use primitive types for properties• Alternative: maps

18

public class Person { private long personId; private String name; private java.util.Date dateOfBirth; // getters and setters: ...

Person.java

1. Write Java Beans (optional)

Page 19: Apache iBatis (ApacheCon US 2007)

19

2. Create Mapping Configuration

<sqlMap namespace="Person">

<select id="getPerson" resultClass="samples.Person" parameterClass="long"> SELECT id as personId , p_name as name , date_of_birth as dateOfBirth FROM person_t pers WHERE pers.id = #value# </select></sqlMap>

Person.xml

Page 20: Apache iBatis (ApacheCon US 2007)

20

3. Specify the SQL Configuration <sqlMapConfig>

<transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property ... ... </dataSource> </transactionManager>

<sqlMap resource="samples/maps/Person.xml"/>

</sqlMapConfig>

sql-map-config.xml

Page 21: Apache iBatis (ApacheCon US 2007)

21

4. Write the App Code

public Person getPerson(long id) { Person p = null; try { // Configure iBatis Reader reader = Resources.getResourceAsReader ("samples/sql-map-config.xml"); SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

// execute p = (Person)sqlMap.queryForObject("getPerson", id); } catch (Exception e) { // do something useful here } return p;}

DAO.java

Page 22: Apache iBatis (ApacheCon US 2007)

22

Apache iBatis

• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary

Page 23: Apache iBatis (ApacheCon US 2007)

The Data Mapping

• Define mapping between Java objects and SQL

• XML configuration file(s)• iBatis offers many possibilities/features

– The following is just an overview– Check out the docs– (Often maps are sufficient)

Page 24: Apache iBatis (ApacheCon US 2007)

• Mapping Java beans to SQL statements• More than one config file possible

24

<sqlMap> <typeAlias/> * <parameterMap/> * <resultMap/> * <statement/> * <select | insert | update | delete | procedure/> *</sqlMap>

Mapping Elements

Page 25: Apache iBatis (ApacheCon US 2007)

• Mapped statements– SQL– Parameter maps (input)– Result maps (output)

25

<select id="getAllUsers" resultMap="User"> SELECT id, username, password, active FROM user_t</select>

Data Mapper Concept

Page 26: Apache iBatis (ApacheCon US 2007)

• Unique identifier• Result and parameter class• SQL statements (it's XML!)

26

<select id="getOlderThan" resultClass="Person" parameterClass="date"> <![CDATA[ SELECT id as personID , ... FROM person_t pers WHERE date_of_birth > #value# ]]></select>

Querying the Database

Page 27: Apache iBatis (ApacheCon US 2007)

• Primitive types can directly be used for input– e.g. String, Integer, Date, etc.– #value# references the actual value

27

<statement id="getPerson" parameterClass="java.lang.Integer"> SELECT * FROM product_t

WHERE product_id = #value#</statement>

sqlMapClient.queryForObject("getPerson", new Integer(1));

Parameters: Primitive Types

Page 28: Apache iBatis (ApacheCon US 2007)

28

Parameters: Beans

• Mapping description: bean -> input– Properties– Optional type definition: Java and JDBC– Optional null values– Optional type handler for conversion

<parameterMap id="parameterMapName" [class="someDomainClass"]> <parameter property="propertyName" [jdbcType="VARCHAR"] [javaType="string"] [nullValue="-1"] [typeHandler="someHandler"]/> <parameter .../></parameterMap>

Page 29: Apache iBatis (ApacheCon US 2007)

• Properties are applied in order of definition

29

<parameterMap id="personMap" class="samples.Person"> <parameter property="name"/> <parameter property="dateOfBirth"/> <parameter property="address"/></parameterMap> <insert id="insertPerson" parameterMap="personMap"> INSERT INTO person_t (name, date_of_birth, address) VALUES (?, ?, ?)</insert>

Parameters: Beans

Page 30: Apache iBatis (ApacheCon US 2007)

• Less verbose form of parameter maps– Inline property names

30

Parameters: Inline Beans

<insert id="insertPerson" parameterMap="samples.Person"> INSERT INTO person_t (name, date_of_birth, address) VALUES (#name#, #dateOfBirth#, #address#)</insert>

Page 31: Apache iBatis (ApacheCon US 2007)

31

Parameters: Inline Beans

• Support for types and null values

<insert id="insertPerson" parameterMap="samples.Person"> INSERT INTO person_t (name, date_of_birth, address) VALUES (#name#, #dateOfBirth#, #address:VARCHAR:NO_ADDR#)</insert>

Page 32: Apache iBatis (ApacheCon US 2007)

• Alternative to custom beans: maps– java.util.HashMap etc.– #key# references the value stored for key

32

<statement id="getAddresses" parameterClass="java.util.Map"> SELECT * FROM addresses_t WHERE person_id = #personID# AND category_information = #category#

</statement>

params.put("personID", new Integer(1));params.put("category", "Business");sqlMapClient.queryForList("getAddresses", params);

Parameters: Maps

Page 33: Apache iBatis (ApacheCon US 2007)

• Mapping result sets to beans– Properties/columns– Optional type definition: Java and JDBC– Optional null values– Optional type handler for conversion

33

<resultMap id="resultMapName" class="someDomainClass" [extends="parent-resultMap"]> <result property="propertyName" column="COLUMN_NAME"

[javaType="int"] [jdbcType="NUMERIC"] [nullVlaue="-1"]

[typeHandler="someHandler"]/> <result .../></resultMap>

Result Maps

Page 34: Apache iBatis (ApacheCon US 2007)

• Direct mapping

34

<select id="getPerson" parameterClass="long" resultClass="samples.Person"> SELECT id AS personID , first_name AS firstName , email , ... FROM person_t pers WHERE pers.id = #value#</select>

public class Person { private long personID; private String firstName; private String email; ...

Result Maps: Inline

Page 35: Apache iBatis (ApacheCon US 2007)

• Primitive types can directly be used– e.g. String, Integer, Date, etc.– Result map or inline

35

<resultMap id="get-result" resultClass="java.lang.Integer"> <result property="value" column="NUMBER_OF_CUSTOMERS"/></resultMap>

<statement id="getPersonCount" resultClass="int" SELECT count(*) AS value FROM person_t</statement>

Primitive Results

Page 36: Apache iBatis (ApacheCon US 2007)

• Alternative to custom beans: maps– java.util.HashMap etc.

36

<resultMap id="result-map" resultClass="java.util.HashMap"> <result property="id" column="ID"/> <result property="title" column="TITLE"/> <result property="speaker" column="SPEAKER_NAME"/></resultMap>

<select id="getAllSessions" resultMap="result-map"> SELECT id, title, speaker_name FROM sessions_t</select> sqlmap.queryForList("getAllSessions")

{id=1, title=iBatis, speaker=Ziegeler}{id=2, title=JCR, speaker=Ziegeler}{id=3, title=Portals, speaker=Ziegeler}

Map Results

Page 37: Apache iBatis (ApacheCon US 2007)

37

Map Results Inline

• Inline definition

<select id="getAllSessions" resultClass="java.util.HashMap"> SELECT id, title, speaker_name speaker FROM sessions_t</select>

sqlmap.queryForList("getAllSessions")

{id=1, title=iBatis, speaker=Ziegeler}{id=2, title=JCR, speaker=Ziegeler}{id=3, title=Portals, speaker=Ziegeler}

Page 38: Apache iBatis (ApacheCon US 2007)

Parameters and Results

• Primitive types• Maps• Beans• Inline or extra definition• Quick Start: Maps + primitive types inline

Page 39: Apache iBatis (ApacheCon US 2007)

• Lightweight API– Configuration– Executing Queries– Updating (inserts and deletes)

39

Reader reader = Resources.getResourceAsReader ("samples/sql-map-config.xml");

SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

Using the Data Mapper

Page 40: Apache iBatis (ApacheCon US 2007)

40

Queries

• Single objects: sqlMap.queryForObject()• Lists: sqlMap.queryForList()• Maps: sqlMap.queryForMap()

– Key can be any mapped property

• Each query is run with:– Configured map identifier– Parameters (optional)

sqlmap.queryForList("getAllSessions")

{id=1, title=iBatis, speaker=Ziegeler}{id=2, title=JCR, speaker=Ziegeler}{id=3, title=Portals, speaker=Ziegeler}

Page 41: Apache iBatis (ApacheCon US 2007)

41

Updates, Inserts and Deletes

• sqlMap.update("updatePerson", person);• sqlMap.insert("insertPerson", person);• sqlMap.delete("deletePerson", 5);

Page 42: Apache iBatis (ApacheCon US 2007)

• Mapping relationships• Joins• Lazy loading• Transactions, Batches• Stored Procedures• Caching• And more…

42

Additional Features

Page 43: Apache iBatis (ApacheCon US 2007)

• Define fragments for reuse– Avoid duplicate definitions

43

<sql id="base-select"> SELECT id as personID , ... FROM person_t pers</sql>

<select id="getPerson" resultClass="person" parameterClass="long"> <include refid="base-select"/> WHERE pers.id = #value#</select><select id="getPersons" resultClass="person"> <include refid="base-select"/> ORDER BY date_of_birth ASC</select>

Fragments

Page 44: Apache iBatis (ApacheCon US 2007)

44

Dynamic SQL Statements

• Dynamic statements (if …)• Using parameters:

<select id="findPersons" resultMap=“person" parameterClass="java.util.Map"> SELECT id, first_name, last_name FROM person_t pers WHERE category = #category# $whereclause$ ORDER BY $orderlist$</select>

Map paramMap = new HashMap();paramMap.put("category", "Business"));paramMap.put("whereclause", "and id > 10");paramMap.put("orderlist", "date_of_birtsh DESC");

sqlmap.queryForList("findPersons", paramMap);

Page 45: Apache iBatis (ApacheCon US 2007)

45

Apache iBatis

• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary

Page 46: Apache iBatis (ApacheCon US 2007)

• Central XML configuration– Properties– Data source

46

<sqlMapConfig> <properties/> <settings/> <typeAlias/> * <transactionManager> <dataSource/> </transactionManager>

<sqlMap/> *</sqlMapConfig>

Data Base Configuration

Page 47: Apache iBatis (ApacheCon US 2007)

Configuration Properties

• Properties can be loaded from– The classpath: <properties resource=""/>– URL: <properties url=""/>

• Values can be referenced with Ant style syntax: ${key}

47

Page 48: Apache iBatis (ApacheCon US 2007)

• Transaction manager• JDBC• JTA• EXTERNAL

48

<transactionManager type="JDBC"> <dataSource …

Transaction Manager

Page 49: Apache iBatis (ApacheCon US 2007)

49

Data Source

• Data source factory and properties– SIMPLE – based on iBATIS connection

pooling implementation– DBCP – uses Jakarta DBCP– JNDI – retrieves data source from a JNDI

context– Own factories possible

Page 50: Apache iBatis (ApacheCon US 2007)

• Data source factory and properties

50

<dataSource type="SIMPLE"> <property value="${driver}" name="JDBC.Driver" /> <property value="${url}" name="JDBC.ConnectionURL" /> <property value="${username}" name="JDBC.Username" /> <property value="${password}" name="JDBC.Password" /></dataSource>

Data Source Sample

Page 51: Apache iBatis (ApacheCon US 2007)

• All SQL Map files must be referenced– Classpath– URL

51

<sqlMap resource="samples/Person.xml" /><sqlMap url="file:///d:/ibatis/Person.xml" />

SQL Maps Configuration

Page 52: Apache iBatis (ApacheCon US 2007)

52

Apache iBatis

• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary

Page 53: Apache iBatis (ApacheCon US 2007)

53

Apache iBatis

• Easy and quick start• Mapping Java beans to SQL at the SQL

level• XML configuration files for mapping and

db config• Very good documentation!• Check it out!

Page 54: Apache iBatis (ApacheCon US 2007)

ThanksQ&A

Visit our booth for infos, discussions and jobs!