The advantages of the Data Access Object design pattern, such as reusable software and decoupling of the business logic and database persistence logic, are well established. We shall use the DAO design pattern for the data access layer. The DAO design pattern will provide standard operations for persisting a Catalog
entity instance and getting Catalog
entity instances. Create an org.jboss.springmvc.data.CatalogDao
Java interface, as shown in the following screenshot:
Add method definitions for the persist(Catalog)
methods to persist of the Catalog
object and getAllCatalogs()
to get List
of Catalog
objects. The CatalogDao
interface is listed in the following code:
package org.jboss.springmvc.data; import java.util.List; import javax.persistence.TypedQuery; import org.jboss.springmvc.model.Catalog; public interface CatalogDao { public void persist(Catalog catalog); public List<Catalog> getAllCatalogs(); }
Create an
org.jboss.springmvc.data.CatalogDaoImpl
implementation class to implement the org.jboss.springmvc.data.CatalogDao
interface. Annotate the class with @Component
, which makes the class a "component" that is auto-detected using class-path scanning when we use annotation-based configuration. In the resources/META-INF/spring/applicationContext.xml
application context configuration file, auto-detection of the org.jboss.springmvc.data
package gets configured with the following <context:component/>
element within the <beans/>
element:
<context:component-scan base-package="org.jboss.springmvc.data" />
Spring MVC autodetects the CatalogDaoImpl
class and injects a CatalogDaoImpl
object, catalogDaoImpl
, into any @Autowired
field, method parameter, or constructor parameter of the type CatalogDaoImpl
. Annotation-based transactions are configured using the following declaration in applicationContext.xml
:
<tx:annotation-driven />
The \jboss-springmvcsrcmain
esourcesMETA-INFspringapplicationContext.xml
file is listed in the following code:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:component-scan base-package="org.jboss.springmvc.model" /> <context:component-scan base-package="org.jboss.springmvc.data" /> <tx:annotation-driven /> </beans>
Annotate the persist(Catalog)
method with @Transactional
. Inject EntityManager
into the DAO implementation class using the @PersistenceContext
annotation. The persist()
method of EntityManager
is used to persist a Catalog
instance. To retrieve all Catalog
instances, create a TypedQuery
object using the createQuery
method of the EntityManager
object. Obtain a List<Catalog>
object as a result using the getResultList()
method of the TypedQuery
object. The org.jboss.springmvc.repo.CatalogDaoImpl
DAO implementation class is listed in the following code:
package org.jboss.springmvc.data; import org.jboss.springmvc.model.*; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @Component public class CatalogDaoImpl implements CatalogDao { // Injected database connection: @PersistenceContext private EntityManager em; // Stores a new catalog: @Transactional public void persist(Catalog catalog) { em.persist(catalog); } // Retrieves all the catalogs: public List<Catalog> getAllCatalogs() { TypedQuery<Catalog> query = em.createQuery( "SELECT c FROM Catalog c ORDER BY c.id", Catalog.class); return query.getResultList(); } }