First, you need to activate the feature core/runtime.sqlmap
which is
disable in the default runtime.xml
provided in the default template.
If you want to use SqlMap, you need to create an extension for the
extension point org.ametys.runtime.plugins.core.sqlmap.SqlMapExtensionPoint
.
This extension will refers to an avalon component which will be injected with one or more
SqlMapClient instances.
For example, if you want to inject an SqlMapClient instance in the
avalon component with the role com.test.MyDAO
and using the mapping in the classpath resource
com/test/test.xml
, you need to declare the following extension in your plugin.xml
:
<feature name="myfeature" depends="core/runtime.sqlmap"> <extensions> <extension point="org.ametys.runtime.plugins.core.sqlmap.SqlMapExtensionPoint" id="com.test.MyDAO" class="org.ametys.runtime.plugins.core.sqlmap.SelfSqlMapClientComponentProvider"> <sqlMap resource="com/test/test.xml"/> </extension> </extensions> <components> <component role="com.test.MyDAO" class="com.test.MyDAO"/> </components> </feature>If you want to specify an additionnal mapping with an alternate datasource pool (
mydatasource.jdbc.pool
),
you can define it like this:
<extensions> <extension point="org.ametys.runtime.plugins.core.sqlmap.SqlMapExtensionPoint" id="com.test.MyDAO" class="org.ametys.runtime.plugins.core.sqlmap.SelfSqlMapClientComponentProvider"> <!-- use core datasource for this mapping (default) --> <sqlMap resource="com/test/test.xml"/> <!-- use my datasource for this mapping --> <sqlMap datasource="mydatasource.jdbc.pool" resource="com/test/test_external.xml"/> </extension> </extensions>Remember that you have to declare your alternate datasource, like this :
<extension point="org.apache.avalon.excalibur.datasource.DataSourceComponentSelector" id="mydatasource.jdbc.pool" class="org.ametys.runtime.cocoon.JDBCDataSource" logger="mydatasource.jdbc.pool"> <pool-controller max="10" max-strict="true" blocking="true" timeout="0" trim-interval="60000"/> <keep-alive age="5000" disabled="false">select 1</keep-alive> <auto-commit>false</auto-commit> <driver runtime-config-parameter="mydatasource.jdbc.driver"/> <dburl runtime-config-parameter="mydatasource.jdbc.url"/> <user runtime-config-parameter="mydatasource.jdbc.user"/> <password runtime-config-parameter="mydatasource.jdbc.passwd"/> </extension>
The com.test.MyDAO
must implements SqlMapClientsAware
.
The abstract class org.ametys.runtime.plugins.core.sqlmap.dao.AbstractDAO
help to retrieve the SqlMapClient instance using _getSqlMapClient()
and provide SQLException wrapping:
public class MyDAO extends AbstractDAO implements ThreadSafe, Component { public static final String ROLE = MyDAO.class.getName(); // No SQLException wrapping public List<Test> getTestsNotWrapped() throws SQLException { return (List<Test>) _getSqlMapClient().queryForList("Test.getTests"); } // SQLException wrapping public List<Test> getTests() throws DataAccessException { return new SQLExceptionWrapper<List<Test>>() { @Override public List<Test> processWithSqlMapClient(SqlMapClient sqlMapClient) throws SQLException { return sqlMapClient.queryForList("Test.getTests"); } }.process(); } }
TODO JUnit/DBUnit tests integration.