i'm new to mybatis and i was trying to set my configurations from one file.
i had my configuration.xml file and i was trying to make it read values from
a database.properties file to fill the properties, so i did this:
<properties resource="config/database.properties">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}"/>
</properties>
and here is my *config/database.properties* file:
database.diver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/dbname
database.username=root
database.password=admin
i was expecting it to fill the fields with this values, but i'm getting this
error:
Exception in thread "main"
org.apache.ibatis.exceptions.PersistenceException:
### Error opening session. Cause:
org.apache.ibatis.datasource.DataSourceException: Error setting driver on
UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find
class: ${database.driver}
### Cause: org.apache.ibatis.datasource.DataSourceException: Error setting
driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException:
Cannot find class: ${database.driver}
at
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:83)
at
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)
at com.ds.classfisc.dao.ClientDAO.updateClient(ClientDAO.java:34)
at test.Tests.main(Tests.java:20)
Caused by: org.apache.ibatis.datasource.DataSourceException: Error setting
driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException:
Cannot find class: ${database.driver}
at
org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:179)
at
org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:57)
at
org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:349)
at
org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:55)
at
org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:73)
... 3 more
Caused by: java.lang.ClassNotFoundException: Cannot find class:
${database.driver}
at
org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:173)
at
org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:72)
at org.apache.ibatis.io.Resources.classForName(Resources.java:235)
at
org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:175)
... 7 more
why is it happening?
Thanks and regards
--
View this message in context: http://mybatis-user.963551.n3.nabble.com/Error-setting-driver-on-UnpooledDataSource-tp3348406p3348406.html
Sent from the mybatis-user mailing list archive at Nabble.com.
Here is the code I use, I read the properties from the properties file myself:
// Reader
final Reader reader;
// Access the sql session configuration url as a reader
try {
Resources.setCharset(Charset.forName("UTF-8"));
reader = Resources.getUrlAsReader(MyBatis.myBatisConfigurationUrl.toString());
}
catch (IOException exception) {
final String message = String.format("Error accessing myBatis configuration url: '%1$s', exception: '%2$s'.", MyBatis.myBatisConfigurationUrl.toString(), exception);
logger.error(message);
throw new RuntimeException(message, exception);
}
// Create the properties and populate it with the values which
// will be substituted in the sqlmap-configuration.xml file
// http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
final Properties properties = new Properties();
final String url = "jdbc:mysql://" + hostName + "/" + databaseName + "?" + MyBatis.JDBC_MYSQL_URL_QUERY_OPTIONS;
properties.setProperty("url", url);
properties.setProperty("username", userName);
properties.setProperty("password", password);
// Create the sql session manager
final SqlSessionManager sqlSessionManager = SqlSessionManager.newInstance(reader, properties);