有可能将两个DAO组合成一个服务方法吗?
我想创建一个泛型方法,它将根据输入参数选择正确的DAO。现在我想出的是从服务对象外部接受Dao的方法。但这需要在控制器中初始化适当的Dao,这有点难看...
@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);
}
}
@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);
}
java prettyprint-override">@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);
}
@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;
}
}
@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;
}
}
您可以编写一个解析器模式,根据您的条件返回所需的dao。您的服务将使用解析器获得正确的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();
}
}
这么低的方法如下,有更好的建议吗?
问题内容: 所以我基本上想做的很简单 由于某种原因,它无法正常工作。在我的Javascript控制台(Chrome浏览器)中 编辑1:我已经添加了实际的代码,如您所见,我在构造函数中绑定了validateEmail 问题答案: 您的方法已正确定义,因此问题出在如何 调用上 。 您以一种设置为实例以外的方式调用它。这在事件侦听器中很常见。我想您的代码中有一些类似的代码: React 的推荐解决方案是
我有一个database.xml来定义spring事务,比如 和我的dao,服务都在utils包或子包中,比如: 提前感谢您的帮助和建议!
问题内容: 当我发现以下代码在没有警告和打印的情况下进行编译时,我感到非常惊讶: 我预期会有编译错误。 编译该代码是否有原因? 确保参数具有相同类型的正确方法是什么? 编辑: 关于有限类型参数呢?我能想到的最好的是: 不幸的是,java不允许循环约束。不编译。这是死胡同吗? 问题答案: 究其原因,这是编译因为Java会推断出参数的最具体的超传入,在这种情况下,后是盒装,以和为传递。 没有泛型: 即
问题内容: 注意:这旨在作为常见问题的规范答案。 我有一个带有字段()的Spring 类(),但是该字段是我尝试使用它时所用的。日志显示同时创建了bean和bean,但是每当尝试在服务bean上调用方法时,我都会得到一个。Spring为什么不自动接线该领域? 控制器类: 服务等级: 应该自动连接的服务bean,但不是: 当我尝试时,出现以下异常: 问题答案: 带注释的字段是因为Spring不知道您
问题内容: 在Bruce Eckel的“ Thinking In Java,第四版”的第428页(有关类型信息的章节)中,具有以下示例: 也许我有点累,但是我看不到add()方法中对add()的调用是如何工作的。我一直认为它应该有一个引用,或者是一个静态方法(并且我在ArrayList或List中找不到静态add())。我想念什么? 我只是为自己测试,发现这可行: 问题答案: Java为这样的方法