企业🤖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