我有一个具有多个实现和一个实体类Person的项目。 在每个实现中,都有一个不同的数据库,不同的表和不同的列。 在DAO层和业务层中,代码是相同的。 如何仅更改持久层,以根据配置文件对Person实体类进行不同的实现,而其余代码保持不变?
//I would like to change table and columns based on a profile
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String first_name;
private String last_name
//getters,setters
}
//I would like to keep DAO unchanged no matter the profile
public interface PersonDao {
public List<Person> listAll() throws Exception;
}
public class PersonDaoImpl implements PersonDao{
@Override
public List<Person> listAll() throws Exception{
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
...the rest of the code
}
}
答案 0 :(得分:0)
具有一个通用的抽象Person和PersonDao,它们将由其他类(例如MongoPerson,MysqlPersonDao,PersonV2 ...根据您的要求)扩展/实现。但是在服务层中只能使用Person和PersonDao。
使用qualifiers和configurations通过Spring自动接线