2

I have a web application which needs mybatis to connect to a sql server.

I have a separate property file as jdbc.properties which contains the dburl, username, password etc. and mybatis-config.xml file has following to read that

    <properties resource="jdbc.properties"/>

If I add jdbc.properties file to the src folder it works fine.

But I have several other property files located at "catalina_home/bin/config" folder so I would like to move my jdbc.properties file also to that location.

But mybatis is not able to read the property file from their no matter how I changed the path to the resource.

However if I moved the jdbc.properties file to the "catalina_home/lib" folder it works. Therefore then I tries resource path like this "../bin/config" but it didn't worked either.

My question is, can I move the jdbc.properties file to the bin/config folder and let mybatis to read it from there.

EDIT: Since my production environment may change its CATALINA_HOME location I cannot give the full path to the config file location.

1 Answers1

2

The reason that mybatis cannot find these properties files is that tomcat's bin/config folder is not in classpath of tomcat (or your web application) unlike $CATALINA_HOME/lib. And mybatis looks for file specified in resource attribute in classpath. If you use url attribute you can specify full path name to file there.

<properties url="/full/path/to/you/jdbc.properties"/>
  • See http://mybatis.github.io/generator/configreference/properties.html for details. – heenenee Aug 11 '15 at 16:17
  • Many thanks Roman, But the problem is my production environment may change the catalina_home location and therefore I can't specify the full path. Is there anyway that I can point to my file with a relative path? – Lasitha Weerasinghe Aug 12 '15 at 04:51
  • Have the same issue. I don't understand why ibatis is not able to understand a relative path like: . Like others tools like log4j2 understand these types of placeholders.. – brebDev Jan 07 '22 at 12:28
  • @LasithaWeerasinghe Have you found any solution to this one ? Thanks upfront – brebDev Jan 07 '22 at 12:29
  • 2
    @brebDev Mybatis does not support variables in properties files. Here's the answer from the mybatis author/maintainer https://stackoverflow.com/questions/61138521/using-environment-variables-in-mybatis-properties-file with a workaround using java configuration – Roman-Stop RU aggression in UA Jan 07 '22 at 12:32
  • 1
    @RomanKonoval I took a look over that answer, thanks for pointing me. The only issue was that one is forcing you to move all the mappers to java, which I wanted to avoid. But I found a hybrid solution which is working with the xml mappers and properties loaded based on env variables. You can take a look here: https://stackoverflow.com/questions/70649406/ibatis-load-properties-file-based-on-environment-variables – brebDev Jan 10 '22 at 08:16