简体   繁体   English

遍历结果 <Record> 来自jOOQ in <c:forEach>

[英]Loop through Result<Record> from jOOQ in <c:forEach>

Is there a way to loop through Result<Record> from jOOQ in a <c:forEach> ? 有没有办法在<c:forEach> jOOQ中遍历Result<Record> <c:forEach>

Here's the getter method: 这是getter方法:

public Vector<Map<String, String>> getUsers() {
    Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
    Result<Record> results = sql.select().from("users").fetch();

    Vector<Map<String, String>> v = new Vector<Map<String, String>>();
    for(Record item: results) {
        Map<String, String> m = new HashMap<String, String>();
        m.put("login", item.getValueAsString("login"));
        // other columns
        v.add(m);
    }
    return v;
}

Here's the view: 这是视图:

<c:forEach var="u" items="${users}">
   ${u.login} <br />
</c:forEach>

Is there way to make my getter method simpler like: 有没有办法使我的getter方法变得更简单:

public Result<Record> getUsers() {
    Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
    reutrn sql.select().from("users").fetch();
}

But as mentioned earlier I don't know how to loop through it in <c:forEach> , or maybe is it not possible? 但是如前所述,我不知道如何在<c:forEach>进行遍历,或者是否不可能?

public List<String> getUsers() {
  Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
  Result<Record> results = sql.select().from("users").fetch();

  return results.getValues(loginFieldIndex);
}

And you put returned List in request attributes (eg. attribute name "users") and jsp: 然后将返回的List放入请求属性(例如,属性名称“ users”)和jsp中:

<c:forEach var="u" items="${users}">
  ${u} <br />
</c:forEach>

It seems that you would like to operate on records as if they were maps. 似乎您希望像记录一样对记录进行操作。 Here's how to achieve this with jOOQ: 这是使用jOOQ实现此目标的方法:

public List<Map<String, Object>> getUsers() {
  Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
  return sql.select().from("users").fetchMaps();
}

See the relevant Javadocs here: 请在此处查看相关的Javadocs:

This is the top Google answer for "jooq jsp" so (after some experimentation) here is another way of combining JOOQ with JSP, while preserving the type safety and performance of JOOQ. 这是Google对“ jooq jsp”的最佳答案,因此(经过一些试验)这是将JOOQ与JSP结合起来的另一种方法,同时又保留了JOOQ的类型安全性和性能。

<%@ page isELIgnored="false" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="static com.example.jooq.tables.TblCats.*" %>
<%@ page import="org.jooq.Result, org.jooq.Record" %>

<h2>Length: ${fn:length(cats)}</h2>

<c:forEach var="cat" items="${cats}">
 <% Record cat = (Record) pageContext.getAttribute("cat"); %>

 <%= cat.getValue(TBL_CATS.TITLE) %>,
 <%= cat.getValue(TBL_CATS.POSITION) %>

 <br>

</c:forEach>

If you use JOOQ with JSP a lot, it's worthwhile to add an ELResolver that understands Record . 如果您经常将JOOQ与JSP结合使用,则值得添加一个理解RecordELResolver

Here's a simple implementation: https://gist.github.com/dbichko/074641915157ac21996c 这是一个简单的实现: https : //gist.github.com/dbichko/074641915157ac21996c

You then need to register it with the JSP engine. 然后,您需要在JSP引擎中注册它。 In a ServletContextListener is as good a place as any: ServletContextListener它和其他地方一样好:

public void contextInitialized(ServletContextEvent sce) {
    ServletContext ctx = sce.getServletContext();
    JspFactory.getDefaultFactory()
        .getJspApplicationContext(ctx).addELResolver(new RecordResolver());
}

With that, you can use record fields as properties: 这样,您可以将记录字段用作属性:

<c:forEach var="user" items="${users}">
  ${user.login} <br />
</c:forEach>

I would use an ORM library like Hibernate, so you just iterate through a plain list of objects. 我将使用像Hibernate这样的ORM库,因此您只需遍历简单的对象列表即可。

It might seem a bit heavy-weight if your just doing JDBC operations, but it looks like you've reached where it would be useful to you. 如果您只是执行JDBC操作,这似乎有些繁重,但是看起来您已经达到了对您有用的地方。 Something like this... 像这样

@Entity
@Table(name = "users")
class User {
    private String login;
    ...
}

and then a DAO class like this... 然后像这样的DAO课...

public List<Users> getUsers() {
    return (List<Users>)sessionFactory.getCurrentSession().createQuery("from Users").list();
}

Obviously there's plenty of setup and such to bring Hibernate into your project, but I think the benefits outweight that. 显然,有很多设置可以将Hibernate带入您的项目,但是我认为这样做的好处超过了它。

Edit: I don't think JOOQ can do ORM, but it does seem to have a row mapping system. 编辑:我不认为JOOQ可以做ORM,但它似乎有一个行映射系统。 If you want to stick with JOOQ try creating a RecordHandler and use fetchInto() method. 如果您想坚持使用RecordHandler ,请尝试创建一个RecordHandler并使用fetchInto()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM