ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1. 引入 spring-boot-starter-jdbc** ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` <br/> **2. 配置数据库信息`application.yml`** ```yml 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 type: com.zaxxer.hikari.HikariDataSource ``` <br/> **3. 调用模板类 JdbcTemplate 访问数据库** ```java @SpringBootTest public class JdbcTests { @Autowired private JdbcTemplate jdbcTemplate; @Test public void testJdbc() { 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=男生} } } ``` **** 案例代码:https://gitee.com/flymini/codes03/tree/master/learn-bootjdbc