Implementing DAO Design Pattern in Jedis Using Java API
In this article, we are going to learn the DAO design pattern and the implementation in the Jedis java client.
Join the DZone community and get the full member experience.Join For Free
In this article, we are going to learn the DAO design pattern and the implementation in the Jedis java client. The DAO pattern is implemented as a layer between the Client application and the Database. The client application need not depend on the underlying database interaction API (Low-level). The data being stored in the Redis database is modeled as a Domain object (POJO class). It will have only getter and setter methods. The client application knows only the domain object and the high-level API.
- Eclipse (any version) with Maven capabilities
- Java 8+
- Redis and Jedis
Installing Redis Server on Windows
- Click on the link: https://github.com/dmajkic/redis/downloads
- Download Redis-2.4.5-win32-win64.zip file
- Unzip the file and go to the 64bit folder. There you can find redis-server.exe
- To start the Redis server, execute the redis-server.exe file.
Installing Eclipse-IDE on Windows
- Click on the link: https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2020-09/R/eclipse-inst-jre-win64.exe
- Download the eclipse-inst-jre-win64.exe file and run the eclipse installer.
3. Select Eclipse IDE for Eclipse committers and install
Creating a Maven Project in Eclipse IDE
1. Open the Eclipse IDE
2. Go to File > New > Project
3. Go to Maven -> Maven Project and click Next.
4. Select your workspace location and click Next
5. Select quick start maven archetype and click Next
6. Enter Group Id, Artifact Id, and package name.
- Group Id: Fill a groupId for the project of your choice.
- Artifact Id: Fill artifactId for the project of your choice.
- Package: java package structure of your choice
7. The above process will create a project structure like below.
8. Create packages like com.example.demo.dao, com.example.demo.model and com.example.demo.util under src/test/java folder
9. Place the SiteDaoRedisImplTest.java file in com.example.demo package.
10. Add the SiteDao.java file in com.example.demo.dao package.
11. Add the SiteDaoRedisImpl.java file in com.example.demo.dao package
12. Add HostPort.java,KeyHelper.java,MetricUnit.java,RedisSchema.java and TaskKeyManager.java in com.example.demo.util package
13. Replace the pom.xml with the below content.
Running the Code
- Build the Maven project and run the test case as shown below.
2. Check the test result.
Now, we learned the DAO pattern to perform database operations without knowing the low-level API through the Jedis java client application.
Further, this use case can be adapted according to your requirements.
Feel free to ask any questions.
Opinions expressed by DZone contributors are their own.