Search code examples
javamysqlxmlnetbeans-7ibatis

how to work with iBatis


I am so new in ibatis. I have read some feature on it and tried to work but it's not happening for me. May be I am facing placement problem here for xml files. Here is my work flow >>> I have taken a project in netbeans and keep ibatis-2.3.4.726.jar under Libraries package with Mysql JDBC Driver. Then I have created Employee.xml and SqlMapConfig.xml files and keep them under Web Pages/WEB-INF package. Then I have created Employee.java and IbatisInsert.java files and keep them under source packages/default-package folder. Now when I run IbatisInsert.java file it shows the following messages >>>

Exception in thread "main" java.io.IOException: Could not find resource SqlMapConfig.xml at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:110) at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:95) at com.ibatis.common.resources.Resources.getResourceAsReader(Resources.java:161) at IbatisInsert.main(IbatisInsert.java:20)

I am not understanding where to keep files and how to configure ibatis. I have searched google but not understanding. Can anyone please help me on this please ??!!! here are my xml files and java files belows :::

employee.xml >>>

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

<sqlMap namespace="Employee"> 
<insert id="insert" parameterClass="Employee">

   insert into EMPLOYEE(first_name, last_name, salary)
   values (#first_name#, #last_name#, #salary#)

   <selectKey resultClass="int" keyProperty="id">
      select last_insert_id() as id
   </selectKey>

</insert> 

</sqlMap>

SqlMapConfig.xml >>>

    <!--<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/ibatisExample"/>-->

<?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://localhost:3306/ibatisexample"/>
          <property name="JDBC.Username" value="root"/>
          <property name="JDBC.Password" value="123"/>
        </dataSource>
      </transactionManager>
     <sqlMap resource="Employee.xml"/> 
</sqlMapConfig>

<property name="JDBC.AutoCommit" value="true"/>

<property name="Pool.MaximumActiveConnections" value="10"/>

<property name="Pool.MaximumIdleConnections" value="5"/>

<property name="Pool.MaximumCheckoutTime" value="150000"/> 

<property name="Pool.MaximumTimeToWait" value="500"/> 

<property name="Pool.PingQuery" value="select 1 from Employee"/> 

<property name="Pool.PingEnabled" value="false"/>

employee.java >>>

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sumon.bappi
 */
public class Employee {
  private int id;
  private String first_name; 
  private String last_name;   
  private int salary;  

  /* Define constructors for the Employee class. */
  public Employee() {}

  public Employee(String fname, String lname, int salary) {
    this.first_name = fname;
    this.last_name = lname;
    this.salary = salary;
  }
} /* End of Employee */

IbatisInsert.java >>>

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sumon.bappi
 */
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 IbatisInsert{
  public static void main(String[] args)
   throws IOException,SQLException{
   Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
   SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

   /* This would insert one record in Employee table. */
   System.out.println("Going to insert record.....");
   Employee em = new Employee("Zara", "Ali", 5000);

   smc.insert("Employee.insert", em);

   System.out.println("Record Inserted Successfully ");

  }
} 

my file placement as like the following image >>>

ibatis_file_placement


Solution

  • In order to allow reading SqlMapConfig.xml file , just put it under a config directory beside the source folder in the default package path and add the directory to your classpath.

    Another thing i noticed you are missing in your SqlMapConfig.xml is the mappers .. The major role of this configuration file is to map the location of the xml resources as well as defining the data source parameters.

    So you may need to add something like this at the end of your file:

    <mappers>
          <mapper resource="PATH_TO_YOUR_DIR/Employee.xml />
    </mappers>