package com.lhoussaine.springjsfjpa.entities;
@Table(name="address")
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String streetNumber;
private String streetName;
private String city;
getter/setter
}
我有 30 个实体。
现在存储库:
package com.lhoussaine.springjsfjpa.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.repository.annotation.RestResource;
import com.lhoussaine.springjsfjpa.entities.Address;
@RestResource(rel="address", path="address")
public interface AddressRepository extends JpaRepository<Address,Integer> {
}
这里我不需要实现 CRUD 操作!感谢 spring-data-jpa!我希望控制器和服务具有相同的标准:
public interface IAddressService {
}
package com.lhoussaine.springjsfjpa.services.generic;
import java.util.List;
public abstract class GenericService<T,K> {
public abstract T create(T saved);
public abstract void remove(T deleted);
public abstract T findById(K id) ;
public abstract List<T> findAll();
public abstract T removeById(K id);
}
package com.lhoussaine.springjsfjpa.services.impl;
@Service
@Transactional
public class AddressService extends GenericService<Address, Integer> implements IAddressService {
@Autowired private AddressRepository iaddressRepository;
public Address create(Address saved) {
Address address=saved;
return iaddressRepository.save(address);
}
public void remove(Address deleted) {
iaddressRepository.delete(deleted);
}
public Address findById(Integer id) {
return iaddressRepository.findOne(id);
}
public List<Address> findAll() {
return iaddressRepository.findAll();
}
public Address removeById(Integer id) {
Address addr= iaddressRepository.findOne(id);
if(addr!=null){
iaddressRepository.delete(addr);
}
return addr;
}
}
现在的问题是:有了控制器我怎么办?为每个类开发一个控制器?知道我有 30 个服务类。是否有一些接近相同标准的东西,例如 Spring Data JPA,但对于服务和控制器?正如您在服务类中看到的那样!我有义务为GenericService
我的包实体中的每个类创建类并创建一个接口。