简体   繁体   中英

Can I change a sql query in Mybatis without redeploying?

We have a requirement that we should be able to change the sql that we run using mybatis at runtime that is without redploying. I have looked across the web for answers but could not anything.

I don't mean the dynamic sql feature where we can add/ remove clause based on some condition. I should be able to change the the sql completely.

Can we get the mapper sql from DB or object rather than the mapper xml mentioned in the config xml. If that can be done, we can update the DB or the object by using some REST call and the next sql that runs picks up the new sql. If required I would cache the DB sql, which can improve the performance.

Is it possible? Can we do something of this sort? If not possible in mybatis, is there any other framework which can support this?

If you use Java Api, you may use sql providers, just putting @SelectProvider or other @...Provider annotations to your mapper interface methods and creating those providers, getting sql queries from wherever you want. Something like this:

@SelectProvider(type = EntitySqlProvider.class, method =  "buildSelectQuery")
List<T> find();

Provider class does not need to implement or extend anything. You just create a method returning string. But, well, this does not work with xml configuration

In mybatis-config.xml we usually configure Mapper locations as follows:

<mappers>
    <mapper resource="CategoryMapper.xml"/>    
</mappers>

We can store mappers in file system and configure them as follows:

<mappers>   
    <mapper url="file:///D:/CategoryMapper.xml"/>
</mappers>

But I think MyBatis reads these mappers once and prepares object model and caches it. So, I don't think it will read again those mapper files even we update them on file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM