Apache Con Us2007 Apachei Batis
-
Upload
day -
Category
Technology
-
view
1.935 -
download
4
Transcript of Apache Con Us2007 Apachei Batis
![Page 2: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/2.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/3.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/4.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/5.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/6.jpg)
• Apache iBatis fits in-between– Small library– Simple setup– Easy to use– Uses data maps
6
Why do I need iBatis?
![Page 7: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/7.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/8.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/9.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/10.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/11.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/12.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/13.jpg)
13
Caveats
• No transparent persistence• No change detection• Explicit mapping required• No implicit operations (like cascaded
updates, deletes etc.)
![Page 14: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/14.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/15.jpg)
15
Data Mapper Concept
![Page 16: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/16.jpg)
16
Apache iBatis
• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary
![Page 17: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/17.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/18.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/19.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/20.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/21.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/22.jpg)
22
Apache iBatis
• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary
![Page 23: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/23.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/24.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/25.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/26.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/27.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/28.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/29.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/30.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/31.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/32.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/33.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/34.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/35.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/36.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/37.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/38.jpg)
Parameters and Results
• Primitive types• Maps• Beans• Inline or extra definition• Quick Start: Maps + primitive types inline
![Page 39: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/39.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/40.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/41.jpg)
41
Updates, Inserts and Deletes
• sqlMap.update("updatePerson", person);• sqlMap.insert("insertPerson", person);• sqlMap.delete("deletePerson", 5);
![Page 42: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/42.jpg)
• Mapping relationships• Joins• Lazy loading• Transactions, Batches• Stored Procedures• Caching• And more…
42
Additional Features
![Page 43: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/43.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/44.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/45.jpg)
45
Apache iBatis
• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary
![Page 46: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/46.jpg)
• Central XML configuration– Properties– Data source
46
<sqlMapConfig> <properties/> <settings/> <typeAlias/> * <transactionManager> <dataSource/> </transactionManager>
<sqlMap/> *</sqlMapConfig>
Data Base Configuration
![Page 47: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/47.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/48.jpg)
• Transaction manager• JDBC• JTA• EXTERNAL
48
<transactionManager type="JDBC"> <dataSource …
Transaction Manager
![Page 49: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/49.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/50.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/51.jpg)
• 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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/52.jpg)
52
Apache iBatis
• Quick Intro• Quick Start Guide• The Data Mapping• The Database Configuration• Summary
![Page 53: Apache Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/53.jpg)
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 Con Us2007 Apachei Batis](https://reader034.fdocuments.in/reader034/viewer/2022052619/5558bcaad8b42a7e298b5113/html5/thumbnails/54.jpg)
ThanksQ&A
Visit our booth for infos, discussions and jobs!