Show Me Some Messages

We can start coding our application! Let's display the last 10 messages on the index page.

This step requires some more coding. Download it all instead! Or go ahead and fire up that keyboard!

Model

We need two model classes: Message and Response (for comments). These will be mapped to the database tables. As they are simple POJOs, we just need to mark them with appropriate DbOom database mapping annotations.

Message class: @DbTable public class Message { @DbId private long messageId; @DbColumn private String text; // ...getters and setters... } ... and comments class: @DbTable public class Response { @DbId private long responseId; @DbColumn private String text; @DbColumn private long messageId; // ...getters and setters... } Once again, simple POJOs with Jodd annotations.

Please note that we haven't said anything about the relationship between these two entities. We will discuss this in more detail later; for now, just keep in mind that Jodd is all about mapping, so relations are not defined up front. Instead, you'll define them with your queries when needed.

Database configuration

It's a good practice to use a prefix for the database tables that your application uses. We can specify the prefix in Jodd annotations, but it's better to specify a default prefix for all entity beans. You can do this in joy.props . While you're at it, we can also enable the debug mode for more verbose logs.

db.debug=true db.dbOomConfig.tableNames.prefix=jd_

Java Service

Finally, it's time to read the database and return the data! We are going to write a bean that contains the (business) logic. We will mark this bean as @PetiteBean to have it registered in the application's Petite container. No other registration is required!

Since our method accesses the database, it has to run under a database transaction. To enable a database session for this method, we just need to mark it with a transactional annotation.

Instead of SQL, we will write queries using T-SQL (Template SQL). It's an extension of the common SQL that provides special macros which may reference to our Java entities. This significantly simplifies writing native SQL queries!

@PetiteBean public class AppService { @ReadOnlyTransaction public List<Message> findLastMessages(int count) { DbSqlBuilder dbsql = sql("select $C{m.*} from $T{Message m} " + "order by $m.messageId desc limit :count"); DbOomQuery dbquery = query(dbsql); dbquery.setInteger("count", count); return dbquery.list(Message.class); } } Our service is simple POJO class, annotated with @PetiteBean annotation. You need nothing else, no XML files, registration, etc. And you may use plain SQL queries instead if you wish so. Noticed the @ReadOnlyTransaction annotation? Yeah, that's all that's required to supply your database code with a transactional database session.

Awesome, right?!

View

For the web layer, we'll need a Madvoc action and JSP that renders the result. Madvoc action is a simple bean, annotated with @MadvocAction . Created on every request, Madvoc action recognizes the container's context and you can easily inject your services.

@MadvocAction public class IndexAction { @PetiteInject AppService appService; @Out List<Message> messages; @Action public void view() { messages = appService.findLastMessages(10); } } Madvoc actions are automatically registered by default. You can use manual registration instead!

Madvoc action uses simple conventions and smart annotation to define input and outputs and target JSP. Our example has only one output – a list of messages.

<%@ taglib prefix="j" uri="/jodd" %> <html> <body> <h1>Messages</h1> <ul> <j:iter items="${messages}" var="msg"> <li>${msg.messageId} ${msg.text} </j:iter> </ul> </body> </html> Another gem for you: there is an entire powerful tag library you can use in your JSPs!

Run!