JingDAO Tutorial: Basic Usage

In this example, we'll look at the simpliest of use cases: a single container and a single DAO in a standalone application. First, we'll define our SimpleDAO interface:

package org.jadetower.dao.test.daos;

public interface SimpleDAO {

    public String getName();

}
     

And now our implementation:

package org.jadetower.dao.test.daos.impl;

import org.jadetower.dao.test.daos.SimpleDAO;

public class HelloWorldDaoImpl implements SimpleDAO
{
  public HelloWorldDaoImpl() {
  }

  public String getName() {
    return "Hello World!";
  }
}
     

Doesn't get much simplier than that. Now we'll write our XML configuration file. The file can be named anything, but for this example we'll use simple-example.xconf . In most cases, it would make sense to call the configuration file something like dao.xconf :

<?xml version="1.0" encoding="ISO-8859-1"?>
<container>

    <manager id="standard" default="true">
      <daos>
        <dao name="simple" uri="class://org.jadetower.dao.test.daos.impl.HelloWorldDaoImpl"/>
      </daos>
    </manager>

</container>
    

In this configuration file, we specified a single DaoManager ( <manager/> ) named "standard" and is the default DaoManager (we don't need to explicitly state this since there is only one, so we could have left default="true" out). We also identified our DAO with the keyword " simple " and we specified the implementing class ( class://org.jadetower.dao.test.daos.impl.HelloWorldDaoImpl ). So let's see how we can use this:

    String[] configuration = new String[]{"src/conf/simple-example.xconf"};

    DaoContainer daoContainer = new DaoContainer(configuration, null, null);

    DaoManager manager = daoContainer.getManager();
    SimpleDAO simple = (SimpleDAO) manager.getDao("simple");
    String message = simple.getName();
    System.out.println(message);
    

This section of code would create a new DaoContainer, retrieve the DaoManager and finally look up our SimpleDAO. Notice that we use the keyword or DAO ID to look it up. Also, when we cast the DAO, we cast it into the interface , not the implementation. An example of this code can be seen in the SimpleDAOTestCase unit test.