是否可以将两个DAO组合成一个Service方法?
我想创建一个通用方法,该方法将根据输入参数选择正确的DAO。现在我想出的是一种从服务对象外部接受Dao的方法。但这需要在Controller中初始化适当的Dao,这有点难看...
测量只是PostgreSQL上具有单独表的Temperature.java和Humidity.java实体的接口。
@Service
public class MeasurementService {
@Autowired
private TemperatureDao temperatureDao;
@Autowired
private HumidityDao humidityDao;
public<T extends PagingAndSortingRepository<Measurement, Long>> void insertMeasurementForUser(String username, List<Measurement> measurements, T dao) {
dao.saveAll(measurements);
}
}
TemperatureDao.java
@Repository
public interface TemperatureDao extends PagingAndSortingRepository<Temperature, Long> {
@Query("select u from Temperature u where u.owner = ?1 order by u.id desc")
List<Temperature> findLatestTemperatureForUser(User user, Pageable pageable);
}
HumidityDao.java
@Repository
public interface HumidityDao extends PagingAndSortingRepository<Humidity, Long> {
@Query("select u from Humidity u where u.owner = ?1 order by u.id desc")
List<Humidity> findLatestHumidityForUser(User user, Pageable pageable);
}
Temperature.java
@Entity
@Table(name = "temperature")
public class Temperature implements Measurement {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(name = "th1value")
private Float th1Value;
@Column(name = "timestamp")
@NotNull
private LocalDateTime timestamp;
@ManyToOne
@JoinColumn(name = "user_id")
@NotNull
private User owner;
public Temperature() {
}
public Temperature(Float th1Value, LocalDateTime timestamp, User owner) {
this.th1Value = th1Value;
this.timestamp = timestamp;
this.owner = owner;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public LocalDateTime getTimestamp() {
return timestamp;
}
@JsonSerialize(using = LocalDateTimeSerializer.class)
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
@Override
public User getOwner() {
return owner;
}
@Override
public void setOwner(User owner) {
this.owner = owner;
}
}
Humidity.java
@Entity
@Table(name = "humidity")
public class Humidity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(name = "hum1value")
private Float hum1Value;
@Column(name = "timestamp")
@NotNull
private LocalDateTime timestamp;
@ManyToOne
@JoinColumn(name = "user_id")
@NotNull
private User owner;
public Humidity() {
}
public Humidity(Float hum1Value, LocalDateTime timestamp, User owner) {
this.hum1Value = hum1Value;
this.timestamp = timestamp;
this.owner = owner;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public LocalDateTime getTimestamp() {
return timestamp;
}
@JsonSerialize(using = LocalDateTimeSerializer.class)
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
}
有什么想法吗?
答案 0 :(得分:1)
您可以编写一个Resolver模式以根据您的条件返回所需的dao。您的服务将使用解析器获取正确的路径。
public HellDao implements BaseDao {
public void save();
}
public ByeDao implements BaseDao {
public void save();
}
public DaoResolver {
@Autowired
private helloDao;
@Autowired
private byeDao;
public BaseDao resolve(Object input) {
//based on input return the correct dao
BaseDao resolvedDao = null;
switch(input.enum) {
case Hello:
resolvedDao = helloDao;
break;
case Hello:
resolvedDao = byeDao;
break;
default:
//decide something for default
}
return resolvedDao;
}
}
public class MyService {
@Autowired
private DaoResolver daoResolver;
public Object doSomething() {
BaseDao dao = daoResolver.resolve(someObject);
//you will get HelloDao or ByeDao based on the input
dao.save();
}
}
答案 1 :(得分:0)
您可以使用9_Whg_Nr_221
检查测量类型,因此无需泛型即可进行测量。
fs.rename