iBatis are like Hibernate, JDO and EJB as data persistence framework, which will target mapping for the SQL statement. It is a lightweight and durable framework Persistence API suitable POJO.iBatis with Hibernate, JDO different because it uses storage process and existing SQL to handle database.
This section we will tell you how to configure iBatis to run a small program. Now that all the knowledge of all the disposable it is difficult to explain, we simply put some of this tutorial is divided into separate statements examples. The cases are about how to read from the database data and results showed that at the command prompt on you. In the second example of how you will add more data to the database, after which the third example will show you how to iBatis from delete data records.
Now the first example will show you how to read records from the database, we need a database to execute the query, so we use this example as MySQL5.0 database.
Here we will have to retrieve some of the contact person's information, contact give the table structure is as follows:
CREATE TABLE `contact` (
`id` int(11) NOT NULL auto_increment,
`firstName` varchar(20) default NULL,
`lastName` varchar(20) default NULL,
`email` varchar(20) default NULL,
PRIMARY KEY (`id`)
);
According to Contact Form we need to create a POJO class, in our case, there is a vin database table Contact, includes four fields:
- id
- firstName
- lastName
Contact.java
public class Contact {
private String firstName;
private String lastName;
private String email;
private int id;
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
For mapping configuration we need to create SqlMapConfig.xml to specify the following information:
Ÿ statement for mapping namespace prefix
Ÿ U.S. database will use JDBC to access
Ÿ the JDBC driver for MySQL is "com.mysql.jdbc.Driver"
Ÿ connection URL to "jdbc: mysql: / / 192.168.10.112:3306 / vin"
Ÿ username and password for "root" and "root"
Ÿ U.S. SQL statement describes the "Contact.xml"
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<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://192.168.10.112:3306/vin"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
Mapping file give below, it is mainly responsible for the implementation of U.S. procedures for SQL query. Contact.xml code is as follows:
Now in order to display the data in the database we need to create a category - IbatisExample, it is read from the configuration and SqlMapConfig.xml at you all the data output of the console. IbatisExample.java code is as follows:
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class IbatisExample{
public static void main(String[] args)
throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
//Output all contacts
System.out.println("All Contacts");
List<Contact> contacts = (List<Contact>)
sqlMap.queryForList("Contact.getAll",null);
Contact contact = null;
for (Contact c : contacts) {
System.out.print(" " + c.getId());
System.out.print(" " + c.getFirstName());
System.out.print(" " + c.getLastName());
System.out.print(" " + c.getEmail());
contact = c;
System.out.println("");
}
}
}
In order to run the cases, you need to follow the following steps:
Ÿ at the MySQL database you create a Table Contact
Ÿ iBatis download the JAR files (ibatis-common-2.jar, ibatis-dao-2.jar, ibatis-sqlmap-2.jar), and placed it in your lib directory
Ÿ Setting classpath
Ÿ Create Contact.java its compiler
Ÿ Create Contact.java
Ÿ Create SqlMapConfig.xml
Ÿ IbatisExample.java create and compile
Ÿ IbatisExample document implementation
Output:
You should have the command prompt like this output:







