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
古人学问无遗力,少壮工夫老始成。
纸上得来终觉浅,绝知此事要躬行。