在 MyBatis 3.0.6 中使用 configuration.xml 文件并提供 DataSource 设置
MyBatis 文档展示了一种通过 XML 配置文件构建 SqlSessionFactory
或通过 Java 代码构建 Configuration
对象的方法。它还提到传递将覆盖 XML 文件中指定内容的属性。
除了 XML 配置之外,我还尝试使用属性工具提供数据源 URL,但未设置该属性。
这是我正在使用的配置 XML(为简洁起见,删除了所有别名和映射):
<configuration>
<typeAliases>
<typeAlias alias="Item" type="com.example.project.Item"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:C:/path_to_db_file_in_the_filesystem"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/project/mappers/ItemMapper.xml"/>
</mappers>
</configuration>
XML 文件对所有内容都适用,但我需要通过 Java 代码提供 url
(通过代码,它是从另一个配置文件获取,因此必须动态提供给 MyBatis)。我知道我可以用代码完成这一切,但这将是不必要的工作,所以我想如果可能的话避免这条路线。
根据手册中的描述,我从 XML 文件中删除了 url
行,并得到了以下代码:
String resource = "com/example/project/MyBatisConfiguration.xml";
Reader reader = Resources.getResourceAsReader(resource);
Properties props = new Properties();
props.setProperty("url", url);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader, props);
该代码构建了 SqlSessionFactory
,但我得到了“ url不能为null”异常,表明该属性没有成功覆盖。我认为该属性的名称应该采用特殊格式,但我找不到该格式是什么。
预先非常感谢您的所有帮助。
MyBatis documentation shows a way to build a SqlSessionFactory
through XML configuration file or a Configuration
object through Java code. It also mentions passing properties that will override what is specified in the XML file.
I am trying use the properties facility to supply a data source URL in addition to the XML configuration, but the property does not get set.
Here's the configuration XML I am using (removed all the aliases and mappings for brevity):
<configuration>
<typeAliases>
<typeAlias alias="Item" type="com.example.project.Item"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:C:/path_to_db_file_in_the_filesystem"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/project/mappers/ItemMapper.xml"/>
</mappers>
</configuration>
The XML file works fine for everything, but I need to supply the url
through Java code (through the code, it is obtained from another configuration file so it has to be dynamically supplied to MyBatis). I know I can do it all in code, but that will be unnecessary work so I would like to avoid that route if possible.
Based on the description in the manual, I removed the url
line from the XML file and came up with the following code:
String resource = "com/example/project/MyBatisConfiguration.xml";
Reader reader = Resources.getResourceAsReader(resource);
Properties props = new Properties();
props.setProperty("url", url);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader, props);
The code builds the SqlSessionFactory
, but I get the "url cannot be null" exception, indicating that the property was not successfully overwritten. I think the name of the property should be in a special format, but I could not find out what that format is.
Thanks a lot in advance for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在配置 XML 中,您可以插入数据源 URL 的占位符,如下所示:
然后
SqlSessionFactoryBuilder
的build()
方法替换"${url}" ,属性
"url"
的值由props
提供。In your configuration XML you can insert a placeholder for the datasource URL like that:
Then the
build()
method ofSqlSessionFactoryBuilder
replaces"${url}"
in the XML with the value of property"url"
provided byprops
.