在 MyBatis 3.0.6 中使用 configuration.xml 文件并提供 DataSource 设置

发布于 2024-12-08 19:13:58 字数 1685 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-12-15 19:13:58

在配置 XML 中,您可以插入数据源 URL 的占位符,如下所示:

<property name="url" value="${url}"/>

然后 SqlSessionFactoryBuilderbuild() 方法替换 "${url}" ,属性 "url" 的值由 props 提供。

In your configuration XML you can insert a placeholder for the datasource URL like that:

<property name="url" value="${url}"/>

Then the build() method of SqlSessionFactoryBuilder replaces "${url}" in the XML with the value of property "url" provided by props.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文