DAO and Service Layer for CRUD operation.
In the Part 4 article, we have seen how to integrate spring and hibernate. Now we create the DAO and service layer for the CRUD Operation.Step 1:
First the Delete Package named “AddressBook” which is created by default. Now Select “Java” folder and right click, Select New –> Other-> Package and enter the Package name as “addressbook.dao” as shown.
Select the “dao” package, right click, select New –> Other->Interface as shown here
Enter the interface name as “CRUDDao” as shown.
We will write our DAO interface as generic, so that in the future we can use for any CRUD screen.
package addressbook.dao;
import java.util.List;
public interface CRUDDao {
<T> List<T> getAll(Class<T> klass);
<T> T save(T t);
<T> void delete(T t);
}
Step 2:
Next we will create CRUDDao implementation class which will implement the above interface.
Select the “dao” package, right click, select New –> Other->class and enter the class name as CRUDDaoImpl.java
Here is the java class code.
package addressbook.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.springframework.stereotype.Repository;
@Repository
public class CRUDDaoImpl implements CRUDDao {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> klass) {
return em.createQuery("Select t from " + klass.getSimpleName() + " t")
.getResultList();
}
public <T> T save(T t) {
T newRecord = null;
newRecord = em.merge(t);
return newRecord;
}
public <T> void delete(T t) {
em.remove(em.merge(t));
em.flush();
}
}
Next we will create our service layer. Now Select “Java” folder and right click, Select New –> Other-> Package and enter the Package name as “addressbook.service” as shown.
Select the “service” package, right click, select New –> Other->Interface as shown here
Enter the interface name as “CRUDService” as shown.
Here is the interface code
package addressbook.service;
import java.util.List;
public interface CRUDService {
<T> List<T> getAll(Class<T> klass);
<T> T save(T t);
<T> void delete(T t);
}
Next we will create CRUDServiceImpl implementation class which will implement the above interface.
Select the “service” package, right click, select New –> Other->class and enter the class name as CRUDServiceImpl.java
Here is the Class code
package addressbook.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import addressbook.dao.CRUDDao;
@Service
public class CRUDServiceImpl implements CRUDService {
@Autowired
private CRUDDao CRUDDao;
@Transactional(readOnly = true)
public <T> List<T> getAll(Class<T> klass) {
return CRUDDao.getAll(klass);
}
@Transactional
public <T> T save(T t) {
T newRecord = null;
newRecord = CRUDDao.save(t);
return newRecord;
}
@Transactional
public <T> void delete(T t) {
CRUDDao.delete(t);
}
}
Step 4:
Next we will domain object for addressbook table in the database. Select “addressbook” package, right click, select new-> other->Package and enter the package name as”domain”
Now select the package “domain”, right click, select new-Other->Class and enter the class name as “AddressBook.java”
Here is the code for the same.
package addressbook.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class AddressBook implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String fullName;
private String address1;
private String address2;
private String city;
private String zipCode;
private String state;
private String email;
private String mobile;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
That’s all. In the next article, we will create our Presentation layer using ZK Framework.
0 comments:
Post a Comment