简体   繁体   中英

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

Is there a way to loop through Result<Record> from jOOQ in a <c:forEach> ?

Here's the getter method:

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:

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?

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:

<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:

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:

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.

<%@ 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 .

Here's a simple implementation: https://gist.github.com/dbichko/074641915157ac21996c

You then need to register it with the JSP engine. In a ServletContextListener is as good a place as any:

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.

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. Something like this...

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

and then a DAO class like this...

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.

Edit: I don't think JOOQ can do ORM, but it does seem to have a row mapping system. If you want to stick with JOOQ try creating a RecordHandler and use fetchInto() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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