栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JDBI SQL对象查询中的动态顺序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBI SQL对象查询中的动态顺序

我最近一直在探索与JDBI捆绑在一起的DropWizard,并很快遇到了同样的问题。不幸的是,JDBI的文档乏善可陈(JavaDoc和它的git存储库中的一些示例单元测试并没有单独解决),这令人失望。

这是我根据示例DAO在JDBI的Sql Object API中实现动态排序的结果:

@UseStringTemplate3StatementLocatorpublic interface ProductsDao {    @RegisterMapperFactory(BeanMapperFactory.class) // will map the result of the query to a list of Product POJOs(Beans)    @SqlQuery("select * from products order by <orderby> <order> limit :limit offset :offset")    List<Product> getProducts(@Define("orderby") String orderBy, @Define("order") String order,    @Bind("limit") int limit, @Bind("offset") int offset);    @SqlQuery("select count(*) from products")    int getProductsCount();}

@ UseStringTemplate3StatementLocator-
此注释使我们可以

<arg>
在查询中使用语法。这些args将被我们通过
@Define
注释提供的任何值替换。

为了能够使用此功能,我还必须将此依赖项添加到我的

pom.xml
文件中:

<dependency>  <groupId>antlr</groupId>  <artifactId>stringtemplate</artifactId>  <version>2.3b6</version> <!-- I am not sure if this specific version is meant to be used though --></dependency>

SQL注入警告 应该注意的是,

SqlInjection
由于将值直接插入到查询中,因此可以为我们打开大门。(
:arg
@Bind
使用准备好的语句并防止sql注入的查询和注释中的语法相反)。至少您应该清理将用于
@Define
字段的参数。(下面是DropWizard的简单示例)。

@Path("/products")@Produces(MediaType.APPLICATION_JSON)public class ProductsResource {  private static ImmutableSet<String> orderByChoices = ImmutableSet.of("id", "name", "price", "manufactureDate");  private final ProductsDao dao;  public ProductsResource(ProductsDao dao) {    this.dao = dao;  }  @GET  // Use @InjectParam to bind many query parameters to a POJO(Bean) instead.   // https://jersey.java.net/apidocs/1.17/jersey/com/sun/jersey/api/core/InjectParam.html  // i.e. public List<Product> index(@InjectParam ProductsRequest request)  // Also use custom Java types for consuming request parameters. This allows to move such validation/sanitization logic outside the 'index' method.  // https://jersey.java.net/documentation/1.17/jax-rs.html#d4e260   public List<Product> index(@DefaultValue("id")  @QueryParam("orderby") String orderBy,       @DefaultValue("asc") @QueryParam("order")   String order,       @DefaultValue("20")  @QueryParam("perpage") IntParam perpage,       @DefaultValue("0")   @QueryParam("page")    IntParam page)   int limit, offset;   order = order.toLowerCase();    orderBy = orderBy.toLowerCase();   if (!orderByChoices.contains(orderBy)) orderBy = "id"; //sanitize <orderby>   if (order != "asc" && order != "desc") order = "asc";  //sanitize <order>   limit = perpage.get();   offset = page.get() < 0 ? 0 : page.get() * limit;   return dao.getProducts(orderBy, order, limit, offset);  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号