title: Springboot + thymeleaf displaying database contents in html table
body: <p>I'm trying to learn how to make a web app using springboot and thymeleaf. As an exercise I want to display two columns from a random database table I have created in mysql (persons) in a simple html table.</p>
  
  <p>I've used a couple of tutorials for this and wrote the code below however my html does not display the contents of the database only the table header. I have absolutely no idea where I got it wrong. I looked up other questions here and they were all using something called jpa. Is that better than my approach? If so where can I find a begginer's tutorial.</p>
  
  <p>Code</p>
  
  <p>The app class</p>
  
  <pre><code>package ro.database.jdbcTest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.scheduling.annotation.EnableAsync;
  import ro.database.jdbcTest.controllers.UsersController;
  
  @SpringBootApplication
  @EnableAsync
  public class App
  {
      @Autowired
      UsersController service;
  
      public static void main( String[] args )
      {
          SpringApplication.run(App.class);
      }
  
  }
  </code></pre>
  
  <p>The controller</p>
  
  <pre><code>package ro.database.jdbcTest.controllers;
  
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import ro.database.jdbcTest.model.Users;
  import ro.database.jdbcTest.services.UserService;
  
  import java.util.List;
  import java.util.Map;
  
  
  @Controller
  public class UsersController {
  
      @Autowired
      UserService service;
  
      @RequestMapping(value = "/user", method = RequestMethod.GET)
      public String index(Model md){
          md.addAttribute("user", service.findAll());
  
          return "user";
      }
  }
  </code></pre>
  
  <p>Service class</p>
  
  <pre><code>package ro.database.jdbcTest.services;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.core.RowMapper;
  import org.springframework.stereotype.Service;
  import ro.database.jdbcTest.model.Users;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.List;
  
  @Service
  public class UserService {
  
      @Autowired
      JdbcTemplate template;
  
      public List&lt;Users&gt; findAll() {
          String sql = "select * from people";
          RowMapper&lt;Users&gt; rm = new RowMapper&lt;Users&gt;() {
              @Override
              public Users mapRow(ResultSet resultSet, int i) throws SQLException {
                  Users user = new Users(resultSet.getInt("id"),
                          resultSet.getString("name"),
                          resultSet.getInt("age"));
                  String email = resultSet.getString("email");
                  if (email != null) {
                      user.setEmail(email);
                  }
  
                  return user;
              }
          };
  
          return template.query(sql, rm);
      }
  </code></pre>
  
  <p>And the model class</p>
  
  <pre><code>package ro.database.jdbcTest.model;
  
  public class Users {
  
          private int id;
          private String name;
          private int age;
          private String email;
  
          public Users(int id, String name, int age){
              this.id=id;
              this.name=name;
              this.age=age;
          }
  
          public void setEmail(String email){
              this.email=email;
          }
  }
  </code></pre>
  
  <p>Html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h2&gt;Users&lt;/h2&gt;
  &lt;table border="1"&gt;
      &lt;tr&gt;
          &lt;th&gt;name&lt;/th&gt;
          &lt;th&gt;age&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr th:each = "user: ${users}"&gt;
          &lt;td th:text="${user.name}"&gt;vasile&lt;/td&gt;
          &lt;td th:text="${user.age}"&gt;45&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>