ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot09 **** **1. 引入spring-boot-starter-jdbc依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies> ``` **2. 数据库配置** *`resources/application.yml`* ```xml spring: datasource: username: root password: uhg</flEt3dff url: jdbc:mysql://localhost:3306/learndb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver ``` **3. 调用`JdbcTemplate`类的API来操作数据库** ```java @RestController @RequestMapping("/student") public class StudentController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("/find") public void findById() { String sql = "select id,name,age,gender from student where id=?"; Map<String, Object> student = jdbcTemplate.queryForMap(sql, new Object[]{1}); System.out.println(student); //{id=1, name=张三, age=25, gender=男生} } } ```