JingDAO Tutorial: Basic UsageIn 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
<?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 (
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
|