Question

The Mybatis (3.2) manual describes three ways to configure Mappers as follows: http://mybatis.github.io/spring/mappers.html

Is there any way to extend any of these three procedures so that:

  1. Mapper XML files can be read from outside the classpath (i.e. from a filesystem location)
  2. Mapper XML files can be 'rescanned' for changes that have been made to defined queries since the Application started

Thank you for any advice

Was it helpful?

Solution

It looks like (1) can be achieved along the lines of:

 @Bean   
    public SqlSessionFactory sqlSessionFactory() throws Exception 
    {     
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setMapperLocations(new Resource[]{resource1, resource2});
        sessionFactory.setDataSource(getDataSource());    
        return sessionFactory.getObject();   
    } 

In terms of (2), it seems like this is a much-wanted but unavailable feature in Mybatis. One work-around however is to completely re-create the SqlSession and replace the old one - this effectively picks up any changes made to XML Mappers.

 SqlSession sqlSession = applicationConfiguration.createSqlSessionFactory().openSession();
 sqlSession.selectList(...)

Hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top