IBATIS

0
1111

Why IBATIS ORM?

  1. The database config property file is already integrated so there is no need to build and read your own
  2. The mapping of db columns to the POJO is done at the config file so no need to do all the getters and setters in the code
  3. the SQL code is modular stored and managed, the code is easy to maintain as we can build central factory patterns for the entities
  4. The config lets the user have full control over SQL and the power of the database
  5. if cascaded inserts are needed, Hibernate does do these join inserts under the hood on the mapping but you can spend lots of time trouble shooting if do not understand the data model and the data well.

Vertical Scaling Pattern

  1. Create Java project and add Jars for Ibatis and Access Database then test to see the project is clean
  2. Add the config file for Ibatis
    1. Configure the SqlMapConfig.xml
    2. <?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>
      <properties resource=”jdbc.properties” />
      <settings
      cacheModelsEnabled=”true”
      enhancementEnabled=”true”
      lazyLoadingEnabled=”true”
      maxRequests=”32″
      maxSessions=”10″
      maxTransactions=”5″
      useStatementNamespaces=”false”
      />
      <transactionManager type=”JDBC” >
      <dataSource type=”SIMPLE”>
      <property name=”JDBC.Driver” value=”${accessDriver}”/>
      <property name=”JDBC.ConnectionURL” value=”${accessUrl}”/>
      <property name=”JDBC.Username” value=”${accessUsername}”/>
      <property name=”JDBC.Password” value=”${accessPassword}”/>
      </dataSource>
      </transactionManager><!–
      <transactionManager type=”JDBC” >
      <dataSource type=”JNDI”>
      <property name=”DataSource” value=”java:comp/env/jdbc/pemdb”/>
      </dataSource>
      </transactionManager>
      –><!– List all sql Maps out here .. –>
      <sqlMap resource=”EventTimestamp.xml” />
      </sqlMapConfig>
  3. Create the Utill Class to read the config file
    1. public SqlMapClient getSqlMapClient() throws DataAccessExceptions {
      if (sqlMapClient == null) {
      String sqlMapFile = “SqlMapConfig.xml”;
      try {
      Reader reader = Resources.getResourceAsReader(sqlMapFile);
      sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
      System.out.println(“got client map”);
      } catch (IOException ex) {
      throw new DataAccessExceptions(“Unable to create sqlMap Client”);
      }
      }
      return sqlMapClient;
      }
  4. Test you can open and close the session to the data base
  5. Create a POJO for entity
  6. Concrete implementation of class and interface for the entity
      1. if (sqlMap != null) {
        try {
        list = sqlMap.queryForList(sqlMapId); //value from 2.5 above
    1. Args Class
  7. Create mapping file for POJO to the database columns
    1. Define the Database mapping Dimension.xml
      1. <sqlMap namespace=”APPNAMESPACE”>
      2. <typeAlias alias=”eventtsDaoArg” type=”dao.EventDaoArg”/>  //ARGS
      3. <typeAlias alias=”eventts” type=”domains.EventTimestamp”/> //POJO
      4. <resultMap id=”eventtsResult” class=”eventts”> // RESULTS to POJO MAPPING
        <result property=”event_time_stamp” column=”event_timestamp”/>
        </resultMap>
      5. <select id=”get_event_timestamp” parameterClass=”eventtsDaoArg” resultMap=”eventtsResult” cacheModel=”EventtsCache”>
        <![CDATA[
        select event_timestamp
        from event_timestamp
        ]]>
        </select>
  8. Create Junit test case to get the list of entities

details on mapping: https://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2_en.pdf