1

I want to migrate from XML configuration to Java configuration.

sqlSessionFactory.getConfiguration().setEnvironment(new Environment("development", new org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory(), getDataSouroce()));

I managed to replace all <environments> section with Java configuration (I have removed <environments> from XML configuration file), but I can't get rid of:

 <mappers><mapper resource="mailbox/db/map/FileMapper.xml"/> </mappers>

I tried to write:

sqlSessionFactory.getConfiguration().addMapper(FileMapper.class);

but there are exceptions:

SqlSession sqlSession = MyBatisConnectionFactory.instance.getSqlSessionFactory().openSession();

    FileExample fe = new FileExample();
    Criteria f = fe.createCriteria().andIdBetween(0L, 5L);
    FileMapper mapper = (FileMapper) sqlSession.getMapper(FileMapper.class);
    List<File> allRecords = mapper.selectByExample(fe);

// Mapped Statements collection does not contain value for mailbox.db.dao.FileMapper.selectByExample

1 Answer 1

2

I am using below abstract mapper factory where DbUtil.getInstance().getDataSource() and registerMappers() are the key points.

public abstract class AbstractMapperFactory implements MapperFactory {

    private ThreadLocal<SqlSessionManager> sessionManagerThreadLocal = new ThreadLocal<SqlSessionManager>();

    public <T> T getMapper(Class<T> clazz) throws DaoException {
        if(sessionManagerThreadLocal.get() == null) {
            initialize();
        }
        return sessionManagerThreadLocal.get().getMapper(clazz);
    }

    public void closeSession() {
        if(sessionManagerThreadLocal.get() != null) {
            sessionManagerThreadLocal.get().close();
            sessionManagerThreadLocal.remove();
        }
    }

    private void initialize() throws DaoException {
        Environment environment = new Environment("env", new ManagedTransactionFactory(), DbUtil.getInstance().getDataSource());
        Configuration configuration = new Configuration(environment);
        registerMappers(configuration);
        sessionManagerThreadLocal.set(SqlSessionManager.newInstance(new SqlSessionFactoryBuilder().build(configuration))); 
    }

    protected abstract void registerMappers(Configuration configuration);

}

Where DbUtil.getInstance().getDataSource() is responsible for getting the java.sql.DataSource instance, whether it is managed or simple.

registerMappers() is an abstract method where subclass can register their mappers using code like below:

protected void registerMappers(Configuration configuration) {
    configuration.addMapper(PartMapper.class);
    configuration.addMapper(StatusMapper.class);
    configuration.addMapper(NoteTypeMapper.class);
    configuration.addMapper(AssetTypeMapper.class);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.