一.整合原生的JDBC
1.步骤:
-
创建新项目,引入模块
-
添加数据库
-
编写配置文件
-
测试连接是否成功
-
编写控制器CURD
-
测试
2.创建新项目,引入模块
(1)关键需要引入JDBC API和MySQL Driver,其他模块Spring MVC,Lombok,Thymeleaf等
<dependencies> <!--JDBC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MySQL--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
3.添加数据库(略)
4.编写配置文件
(1)添加一个application.yml文件配置数据源信息
spring: datasource: username: root password: 123456 #?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver
5.测试连接是否成功
-
Springboot06DataApplicationTests.java中测试数据库信息
1 import org.junit.jupiter.api.Test; 2 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.boot.test.context.SpringBootTest; 4 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.SQLException; 8 9 @SpringBootTest 10 class Springboot06DataApplicationTests { 11 12 @Autowired 13 DataSource dataSource; 14 15 @Test 16 void contextLoads() throws SQLException { 17 18 //查看默认数据源 19 System.out.println(dataSource.getClass()); 20 //获得连接 21 Connection connection = dataSource.getConnection(); 22 System.out.println(connection); 23 //关闭连接 24 connection.close(); 25 } 26 27 }
6.编写控制器CURD
- 添加JdbcController.java,并编写CRUD操作
1 import org.springframework.beans.factory.annotation.Autowired; 2 import org.springframework.jdbc.core.JdbcTemplate; 3 import org.springframework.web.bind.annotation.GetMapping; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RestController; 6 7 import java.util.List; 8 import java.util.Map; 9 10 @RestController 11 public class JdbcController { 12 13 //JdbcTemplate 是 core 包的核心类,用于简化 JDBC操作,还能避免一些常见的错误,如忘记关闭数据库连接 14 //Spring Boot 默认提供了数据源,默认提供了 org.springframework.jdbc.core.JdbcTemplate 15 //JdbcTemplate 中会自己注入数据源,使用起来也不用再自己来关闭数据库连接 16 @Autowired 17 JdbcTemplate jdbcTemplate; 18 19 //查询student表中所有数据 20 //List 中的1个 Map 对应数据库的 1行数据 21 //Map 中的 key 对应数据库的字段名,value 对应数据库的字段值 22 @GetMapping("/userList") 23 public List<Map<String, Object>> userList(){ 24 String sql = "select * from user"; 25 List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); 26 return maps; 27 } 28 29 //新增一个用户 30 @GetMapping("/addUser") 31 public String addUser(){ 32 //插入语句 33 String sql = "insert into mybatis.user(id, name, pwd) values (4,'小明','123456')"; 34 jdbcTemplate.update(sql); 35 //查询 36 return "addUser-ok"; 37 } 38 39 //修改用户信息 40 @GetMapping("/updateUser/{id}") 41 public String updateUser(@PathVariable("id") int id){ 42 //插入语句 43 String sql = "update mybatis.user set name=?,pwd=? where id="+id; 44 //数据 45 Object[] objects = new Object[2]; 46 objects[0] = "小明2"; 47 objects[1] = "zxcvbn"; 48 jdbcTemplate.update(sql,objects); 49 //查询 50 return "updateUser-ok"; 51 } 52 53 //删除用户 54 @GetMapping("/delUser/{id}") 55 public String delUser(@PathVariable("id") int id){ 56 //插入语句 57 String sql = "delete from user where id=?"; 58 jdbcTemplate.update(sql,id); 59 //查询 60 return "delUser-ok"; 61 } 62 63 }