Springboot-JdbcTemplate

142 阅读1分钟
package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value="/get/{id}")
    public String index(@PathVariable String id) {
        String sql = "SELECT * FROM user WHERE id = id";
        // 通过jdbcTemplate查询数据库
        String userString = jdbcTemplate.queryForList(sql).toString();
        return userString;
    }
}

输出:[{id=1, userName=admin, age=30, address=[B@4d0d328d, info=已婚, note=BAT}]