[英]MyBatis - defining a global parameter
First the problem: I'm using XML-defined queries and the SQL contains database name as part of a table name. 首先是问题:我使用XML定义的查询,SQL包含数据库名称作为表名的一部分。 For example:
SELECT * from mydb.bar
. 例如:
SELECT * from mydb.bar
。 Unfortunately, databases are created/named all over the place and mudb
part is really dynamic and can change at any moment. 不幸的是,数据库是在所有地方创建/命名的,而
mudb
部分实际上是动态的,并且可以随时改变。 So I wanted to replace it with a property so it would look like SELECT * FROM ${dbname}.bar
and then I defined the following section in mybatis-config.xml: 所以我想用属性替换它,所以它看起来像
SELECT * FROM ${dbname}.bar
然后我在mybatis-config.xml中定义了以下部分:
<properties>
<property name="dbname" value="mydb"/>
</properties>
But when I run the query ${dbname}
evaluates to null. 但是当我运行查询时,
${dbname}
计算结果为null。 Same happens if I define this property in the properties file. 如果我在属性文件中定义此属性,也会发生相同的情况 I would hate to pass this as part of the each call parameters since this is truly a global property.
我不想将此作为每个调用参数的一部分传递,因为这是一个真正的全局属性。 Can this be done?
可以这样做吗? And if yes - how?
如果是 - 怎么样?
Yes, you can! 是的你可以! This is kind of a weird undocumented feature maybe.
这可能是一种奇怪的无证特征。 When building your Configuration object, do something like this.
构建Configuration对象时,请执行以下操作。 (org.apache.ibatis.session.Configuration)
(org.apache.ibatis.session.Configuration)
configuration.getVariables().put("global_param", "123");
Then in your XML map, you can reference. 然后在XML地图中,您可以参考。
select * from ${global_param}
I had the same issue using Spring+MyBatis, and solved it by setting 'configurationProperties' using my Spring XML definition for sqlSessionFactory. 我使用Spring + MyBatis遇到了同样的问题,并通过使用我的Spring XML定义为sqlSessionFactory设置'configurationProperties'来解决它。 My example below shows how to set a custom global property named 'encryptionKey', with a value which you can either hard-code in the XML file, or load from an external file using the context:property-placeholder tag (as below).
下面的示例显示了如何设置名为“encryptionKey”的自定义全局属性,其值可以在XML文件中进行硬编码,也可以使用context:property-placeholder标记从外部文件加载(如下所示)。
<context:property-placeholder location="/WEB-INF/spring/config-datasource.properties" />
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="typeAliasesPackage" value="com.example.model" />
<beans:property name="configurationProperties">
<beans:props>
<beans:prop key="encryptionKey">${jdbc.encryptionKey}</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
I was using an XML configuration but not Spring and set a property inside the Configuration object but discovered that had to be done before the mapper files are loaded (see here ). 我使用的是XML配置但不是Spring,并在Configuration对象中设置了一个属性,但发现必须在加载映射器文件之前完成(参见此处 )。 I abandoned the Configuration object approach and went with this approach, which worked for me:
我放弃了配置对象的方法,并采用这种方法,这对我有用:
Reader reader = Resources.getResourceAsReader("..../mybatis-config.xml");
Properties properties = new Properties();
properties.setProperty("dbname", "mydb");
SqlSessionFactory.factory = new SqlSessionFactoryBuilder().build(reader, "development", properties);
Then, as Andy Pryor posted, use select * from ${dbname}
in the XML mapper. 然后,正如Andy Pryor发布的那样,在XML映射器中使用
select * from ${dbname}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.