Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Showing posts with label iBATIS. Show all posts
Showing posts with label iBATIS. Show all posts

Wednesday, May 16, 2007

iBATIS - part 1

Very basic and simple iBATIS tutorial for a simple SELECT:

Download iBATIS from http://ibatis.apache.org/

Unpack the tar ball and install the jar file (it's now a single file - something like ibatis-2.3.0.677.jar) somewhere on your classpath...or if using with Tomcat or another servlet container, add it to "/WEB-INF/lib" in your for the webapp.

Database (MySQL)

mysql> describe book;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | | |
| authors | varchar(50) | YES | | NULL | |
| editor | varchar(50) | YES | | NULL | |
| chapters | text | YES | | NULL | |
| page_count | int(11) | YES | | NULL | |
| status | char(1) | NO | | P | |
| price | decimal(5,2) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+

mysql> SELECT * FROM book;
+----+----------------------+---------+--------+----------+------------+--------+-------+
| id | title | authors | editor | chapters | page_count | status | price |
+----+----------------------+---------+--------+----------+------------+--------+-------+
| 1 | Learning Perl 3rd Ed | NULL | NULL | NULL | NULL | P | 29.99 |
| 2 | Learning Java 4th Ed | NULL | NULL | NULL | NULL | P | 29.99 |
+----+----------------------+---------+--------+----------+------------+--------+-------+


SqlMapConfig.xml

PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>

<!-- These settings control SqlMap configuration details -->
<settings
useStatementNamespaces="true"
/>

<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/bookdb"/>
<property name="JDBC.Username" value="yourusername"/>
<property name="JDBC.Password" value="yourpassword"/>
</dataSource>
</transactionManager>

<!-- Identify all SQL Map XML files to be loaded by this SQL map -->
<sqlMap resource="BookMap.xml" />

</sqlMapConfig>


BookMap.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Book">

<resultMap id="get-Result" class="Book">
<result property="id" column="id"/>
<result property="title" column="title"/>
</resultMap>

<select id="getById" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (id = #value#)
</select>

<select id="getAll" parameterClass="string" resultMap="get-Result">
SELECT * FROM book WHERE (status = 'P') ORDER BY title
</select>

</sqlMap>


Book.java

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.Date;

public class Book {

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getTitle()
{
return title;
}

public void setTitle(String title)
{
this.title = title;
}

public char getStatus()
{
return status;
}

public void setStatus(char status)
{
this.status = status;
}

private Integer id;
private String title;
private char status;
}


BookTest.java

import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.Resources;

import java.io.Reader;
import java.util.List;
import java.util.Iterator;

public class BookTest {
public static void main(String arg[]) throws Exception {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List bookList = sqlMap.queryForList("Book.getAll",null);
System.out.println("Selected " + bookList.size() + " records.");

if(bookList.size() == 0){
System.out.println("OOOPS - bookList size is 0\n");
}
Iterator books = bookList.iterator();
StringBuffer bookBuffer = new StringBuffer();
while(books.hasNext()){
Book bookX = (Book)books.next();
bookBuffer.append("ID: " + bookX.getId() + " TITLE: " + bookX.getTitle() + "\n");
}
String bookOptions = bookBuffer.toString();
System.out.println(bookOptions);
}
}



$ java BookTest
Selected 2 records.
ID: 2 TITLE: Learning Java 4th Ed
ID: 1 TITLE: Learning Perl 3rd Ed

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels