Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:56:50+00:00 2026-05-26T00:56:50+00:00

MyBatis documentation shows a way to build a SqlSessionFactory through XML configuration file or

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:56:50+00:00Added an answer on May 26, 2026 at 12:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have both the XML + Annotation based configuration for MyBatis
I used MyBatis Generator 1.3.1 to create a Mapper.xml file. When MyBatis parses the
I want to use interface instead of mapper XML file in MyBatis. In the
I wonder how could I describe in mybatis xml stored procedure call with optional
I want to build an application using Hibernate and MyBatis integrate with Spring. In
I'm upgrading from iBatis 2.x to myBatis 3.0.6 and I have a mapper file
I am currently migrating code from iBatis 2 to MyBatis 3. I have a
MyBatis migrations splits each SQL file into two sections: One for migrating forward one
I'm new to MyBatis and my project requires me to read the data from
I am using MyBatis with Spring Integration as described here . I am further

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.