具有spring-data抽象控制器的primefaces

时间:2015-03-25 15:06:28

标签: spring primefaces maven-3 spring-data-jpa jsf-2.2

如果我做的是对的,我想让你说出来。 我有一个maven项目: 父母是核心 模块是: 实体 DTO 知识库 服务 调节器 卷筒纸 安全

我的第一堂课只是为了说明

>

@Table(name = "address")
@Entity
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String streetNumber;
    private String streetName;
    private String city;
    private String postalCode;
    @ManyToOne
    private Country country;
    private String website;
    private String mail;
    private String tel_1;
    private String tel_2;
    private String fax_1;
    private String fax_2;

    public Address() {
        super();
    }
  

块引用

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.repository.annotation.RestResource;
import com.entities.Address;

@RestResource(rel="address", path="address")
public interface AddressRepository extends JpaRepository<Address, Integer>{

}
  

块引用

import interfaces.IAddressService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.entities.Address;

import dto.AddressDTO;

@Service
@Transactional
public class AddressService extends GenericServiceImpl<Address, AddressDTO, Long> implements IAddressService{


}
  

块引用

package services;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;

public class GenericServiceImpl<T, D, ID extends Serializable> implements GenericService<T, D, ID> {

    @Autowired
    private JpaRepository<T, ID> repository;

    @Autowired
    private DozerBeanMapper mapper;

    protected Class<T> entityClass;

    protected Class<D> dtoClass;

    @SuppressWarnings("unchecked")
    public GenericServiceImpl() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
        this.dtoClass = (Class<D>) genericSuperclass.getActualTypeArguments()[1];
    }

    public D findOne(ID id) {
        return mapper.map(repository.findOne(id), dtoClass);
    }

    public List<D> findAll() {
        List<D> result = new ArrayList<D>();
        for (T t : repository.findAll()) {
            result.add(mapper.map(t, dtoClass));
        }
        return result;
    }

    public void save(D dto) {
        repository.saveAndFlush(mapper.map(dto, entityClass));
    }

    public void removeById(int id) {
        repository.delete(mapper.map(id, entityClass));
    }

}
  

块引用

import java.io.Serializable;
import java.util.List;

public interface GenericService <T, D, ID extends Serializable>{

    D findOne(ID id);

    List<D> findAll();

    void save(D dto);

}
  

块引用

import services.GenericService;

import com.entities.Address;

import dto.AddressDTO;

public interface IAddressService extends GenericService<Address, AddressDTO, Long> {

}
  

块引用

import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import services.AddressService;
import dto.AddressDTO;

@Named(value = "addressController")
@ViewScoped
public class AddressController extends AbstractController<AddressDTO> {

    @Inject
    AddressService addressService;
}
  

块引用

import java.util.Collection;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJBException;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import controller.util.JsfUtil;

public abstract class AbstractController<T> {

    @Inject
    private AbstractFacade<T> ejbFacade;
    private Class<T> itemClass;
    private T selected;
    private Collection<T> items;

    private enum PersistAction {

        CREATE,
        DELETE,
        UPDATE
    }

    public AbstractController() {
    }

    public AbstractController(Class<T> itemClass) {
        this.itemClass = itemClass;
    }

    /**
     * Retrieve the currently selected item
     *
     * @return
     */
    public T getSelected() {
        return selected;
    }

    /**
     * Pass in the currently selected item
     *
     * @param selected
     */
    public void setSelected(T selected) {
        this.selected = selected;
    }
  

块引用

public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }


    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

}

对不起,我提了很多代码。 问题是AbstractController和AbstractFacade。 AbstractFacade我需要它在我的项目中吗? 目标是通过AbstractController和AbstractFacade传递所有控制器(CRUD)的相同功能。 我将Primefaces与spring-data和multi-module maven项目结合使用。 我想要的是如果它不是很清楚的话,如果我想添加任何服务,那就让生活变得轻松(因为我的包装实体中有多个classe,这是我的POJO) 我从网上检索AbstractController和AbstractFacade,我想适应我的项目。 如果你不明白我可以提供更多的信息。 它与我的服务(我说的原则)做genericService一样的事情,并且兴趣是重构代码。 谢谢。

0 个答案:

没有答案