简体   繁体   English

我可以在不重新部署的情况下更改 Mybatis 中的 sql 查询吗?

[英]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.我们有一个要求,即我们应该能够在无需重新部署的情况下更改我们在运行时使用 mybatis 运行的 sql。 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.我不是指我们可以根据某些条件添加/删除子句的动态 sql 功能。 I should be able to change the the sql completely.我应该能够完全改变sql。

Can we get the mapper sql from DB or object rather than the mapper xml mentioned in the config xml.我们可以从DB或对象中获取mapper sql而不是config xml中提到的mapper 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.如果可以做到,我们可以通过使用一些 REST 调用来更新数据库或对象,并且下一个运行的 sql 会选择新的 sql。 If required I would cache the DB sql, which can improve the performance.如果需要,我会缓存 DB sql,这可以提高性能。

Is it possible?是否可以? Can we do something of this sort?我们可以做这样的事情吗? If not possible in mybatis, is there any other framework which can support this?如果在 mybatis 中不可能,有没有其他框架可以支持这个?

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.如果您使用 Java Api,您可以使用 sql 提供程序,只需将 @SelectProvider 或其他 @...Provider 注释放入您的映射器接口方法并创建这些提供程序,从您想要的任何地方获取 sql 查询。 Something like this:像这样的东西:

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

Provider class does not need to implement or extend anything. Provider 类不需要实现或扩展任何东西。 You just create a method returning string.您只需创建一个返回字符串的方法。 But, well, this does not work with xml configuration但是,好吧,这不适用于 xml 配置

In mybatis-config.xml we usually configure Mapper locations as follows:mybatis-config.xml我们通常配置Mapper位置如下:

<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.但我认为 MyBatis 读取这些映射器一次并准备对象模型并缓存它。 So, I don't think it will read again those mapper files even we update them on file system.所以,我认为即使我们在文件系统上更新它们,它也不会再次读取这些映射器文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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