当前位置:Gxlcms > 数据库问题 > SpringBoot2.2.2 使用 JdbcTemplate

SpringBoot2.2.2 使用 JdbcTemplate

时间:2021-07-01 10:21:17 帮助过:14人阅读

* @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

SpringBoot2.2.2 使用 JdbcTemplate

标签:select   classpath   ppi   get   app   use   cat   创建   enc   

人气教程排行