Adding MyBatis mapper files dynamically

I'm currently working in a project that we needed to implement such case. Assuming you are using MyBatis CDI in a EJB/CDI context, the bellow code will produce the MyBatis SqlSessionFactory for the MyBatis mapper files that are defined in a static way.

First, your Configuration.xml will only contain the database connection as bellow:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="UNPOOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@<HOST>:<PORT>:<DB_NAME>"/>
                <property name="username" value="<SCHEMA>"/>
                <property name="password" value="<PASSWORD>"/>
            </dataSource>
        </environment>
    </environments>
</configuration>



Second, the bellow class will produce the SqlSessionFactory after loading the mapper XML files dynamically.
import java.io.IOException;
import java.io.InputStream;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 
 * @author Belal
 *
 */
public class SessionFactoryProvider {
 
 private static final String mapperFiles[] = {"FirstTableMapper.xml", "SecondTableMapper.xml", "ThirdTableMapper.xml"};
 
 @Produces
 @ApplicationScoped
 public SqlSessionFactory produceFactory() { 
  String resource = "Configuration.xml";
  InputStream inputStream = null;
  try {
   inputStream = Resources.getResourceAsStream(resource);
  } catch (IOException e) {
   throw new RuntimeException("Fatal Error.  Cause: " + e, e);
  }
  SqlSessionFactory  sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  for(String mapperFile : mapperFiles) {
   InputStream in = getClass().getClassLoader().getResourceAsStream(mapperFile) ; 
   Configuration configuration = sqlSessionFactory.getConfiguration() ;  
   if (in != null){
    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, mapperFile, configuration.getSqlFragments());
    xmlMapperBuilder.parse();
   }
   else {
    System.err.println("Error file not loaded " + mapperFile);
   }
  }
  return sqlSessionFactory;
 }
}

Now, you can inject your mappers files in the service classes as bellow:
import javax.ejb.Stateless;
import javax.inject.Inject;
import com.ithinkisink.blog.mybatis.mapper;

/**
 * 
 * @author Belal
 *
 */
@Stateless
public class MyService {
 
 @Inject
 @Mapper
 FirstTableMapper firstTableMapper;
 
 @Inject
 @Mapper
 SecondTableMapper secondTableMapper;
 
 @Inject
 @Mapper
 ThirdTableMapper secondTableMapper;
 
 //body of the service
}

Don't forget to make sure you have added the MyBatis and MyBatis-CDI to your Maven dependencies (in case you are a Maven lover ;) ) as bellow:
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.2.8</version>
</dependency>
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-cdi</artifactId>
 <version>1.0.0-beta1</version>
</dependency>

Comments

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

PrimeFaces Push with Atmosphere

JavaOne 2015 Session: EJB 3.2/JPA 2.1 Best Practices with Real-Life Examples by Gohar