Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 22 - Including Existing MyBatis Mappers

To include existing MyBatis mapper files, you'll need to place them into a special directory for HotRod to find them and include them in the main configuration file.

It may happen you have already developed multiple MyBatis mapper files that are well tested and fine tuned. Even though you could re-write them for HotRod, it may be better to use them as they are to avoid introducing any errors.

Example Source Code

package examples;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.hotrod.runtime.tx.TxManager;

import daos.ClientDAO;

/**
 * Example 22 - Including Existing MyBatis Mappers
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example22 {

  public static void main(String[] args) throws SQLException {

    System.out.println("=== Running Example 22 - Including Existing MyBatis Mappers ===");

    // 1. Use a standard MyBatis mapper query
    // Example: Search clients from state VA

    TxManager txm = ClientDAO.getTxManager();
    SqlSession sqlSession = txm.getSqlSession();
    List<ClientDAO> clients = sqlSession.selectList("daos.custom.search.searchClient", "CA");
    System.out.println("1. Use a standard MyBatis mapper query. Clients found=" + clients.size());
    for (ClientDAO c : clients) {
      System.out.println(" ID=" + c.getId() + " - Name: " + c.getName());
    }

    System.out.println(" ");
    System.out.println("=== Example 22 Complete ===");

  }

}

How to Run this example

The Example 22 is included in the download package. To run this example please refer to the section How to Run the Examples above, but with a variation from the standard instructions:

Case #1: Search clients using existing MyBatis mapper

This example runs the pre-written MyBatis mapper daos.custom.search.searchClient to search for clients.