SpringBoot2.2.2 使用 JdbcTemplate

/**
 * @author wf.zhang
 */
@RestController
public class HelloController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/query")
    public Map<String, Object> map() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user ");
        return list.get(0);
    }
}

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jdbc?useSSL=true&serverTimezone=UTC&characterEncoding=UTF8
    data-username: root
    data-password: root
    #initialization-mode: always
    #schema=classpath: schema.sql
    #schema=classpath: user.sql
    #data=classpath: data.sql

run  启动类

结果  查询第一个

 

 数据库

 

 

查询所有

/**
 * @author wf.zhang
 */
@RestController
public class HelloController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/query")
    public  List<Map<String, Object>> list() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user ");
        return list;
    }
}

结果

 

 数据库创建表和数据 请看 https://www.cnblogs.com/wf-zhang/p/12163577.html

posted @ 2020-01-07 22:27  wf.zhang  阅读(767)  评论(0编辑  收藏  举报