How to convert a Java resultset into JSON?

前端 未结 10 2041
清酒与你
清酒与你 2020-11-30 04:58

I have a resultset as a result of a MySQL query using the JDBC connector. So my job is to convert the resultset into a JSON format. So that I can send it to the clientside a

10条回答
  •  温柔的废话
    2020-11-30 05:23

    It's pretty easy if you want to use Spring:

    @RestController
    public class MyController
    
      @Autowired
      private JdbcTemplate jdbcTemplate;
    
      @RequestMapping("/")
      List> getAll() {
        return jdbcTemplate.queryForList("select * from my_table");
      }
    }
    

    In mvc-dispatcher-servlet.xml, you'd setup your JdbcTemplate like this:

    
      
        ...data source config...
      
    
    

    Jackson should be in your classpath (ie a Maven dependency).

提交回复
热议问题