我正在使用JDBI和MySQL,我有一个接口GenericDao,我有这个方法
public void insert(T entity);
如何使用Fluent查询而不是SQLObjects来实现JDBI的此方法?
是否可以使用其中任何一个?
handle.insert
或
handle.createQuery
答案 0 :(得分:3)
我发现当我使用SQLObjects样式时,SQLObjects对我很有用:
public interface GenericDao<T> {
List<T> findAll();
long insert(T t);
long update(T t);
long delete(long id);
T findNameById(long id);
}
然后我使用了一个抽象类来放入所有SQL
@RegisterMapper(VesselJDBIMapper.class)
public abstract class VesselJDBI implements Transactional<VesselJDBI>, GenericDao<Vessel> {
@SqlUpdate("UPDATE vessel SET vessel_name = :vesselName, version = version +1 WHERE vessel_id = :id" +
" AND version = :version")
public abstract long update(@BindBean Vessel vessel);
@SqlUpdate("DELETE FROM vessel WHERE vessel_id = :id")
public abstract long delete(@Bind("id") long id);
@SqlQuery("SELECT vessel_id, vessel_name, version FROM vessel WHERE vessel_id = :id")
public abstract Vessel findNameById(@Bind("id") long id);
@SqlUpdate("INSERT INTO vessel (vessel_name, version) VALUES (:vesselName, :version)")
@GetGeneratedKeys
public abstract long insert(@BindBean Vessel vessel);
@SqlQuery("SELECT vessel_id, vessel_name, version FROM vessel ORDER BY vessel_id")
public abstract List<Vessel> findAll();
}
也许这会为某人提供解决方案。
答案 1 :(得分:0)
您可以通过实现GetHandle接口来处理。
public abstract class GenericDao<T> implements GetHandle {
public void insert(T entity) {
getHandle().insert("some sql");
};
}