pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>SpringBoot2jdbc</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>SpringBoot2jdbc</name>
  15. <description>SpringBoot2jdbc</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.mybatis.spring.boot</groupId>
  30. <artifactId>mybatis-spring-boot-starter</artifactId>
  31. <version>2.2.2</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-devtools</artifactId>
  36. <scope>runtime</scope>
  37. <optional>true</optional>
  38. </dependency>
  39. <dependency>
  40. <groupId>mysql</groupId>
  41. <artifactId>mysql-connector-java</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

操作数据源DataSource

  1. package com.example.springboot2jdbc;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import javax.sql.DataSource;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8. @SpringBootTest
  9. class SpringBoot2jdbcApplicationTests {
  10. @Autowired
  11. DataSource dataSource;//数据源对象,自动装配
  12. @Test
  13. void contextLoads() throws SQLException {
  14. //查看一下默认数据源:结果:class com.zaxxer.hikari.HikariDataSource
  15. System.out.println(dataSource.getClass());
  16. //获取数据库链接
  17. Connection connection =dataSource.getConnection();
  18. System.out.println(connection);
  19. //关闭
  20. connection.close();
  21. }
  22. }

JDBC模板执行SQL操作

写在控制器里面的,执行调用jdbc模板

  1. package com.example.springboot2jdbc.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.jdbc.core.JdbcTemplate;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. import java.util.Map;
  10. @RestController
  11. public class JDBCController {
  12. //JDBC定义的模板,这个模板定义了一些写法,可以传入sql语句直接执行,不用自己写繁琐的jdbc了
  13. @Autowired
  14. JdbcTemplate jt;
  15. @GetMapping("/q1")
  16. public List<Map<String,Object>> queryEmps(){
  17. return jt.queryForList("select * from t_emp");
  18. }
  19. @PostMapping("/u1")
  20. public int update(@RequestParam("eid") int eid,
  21. @RequestParam("empName") String empName,
  22. @RequestParam("age") int age,
  23. @RequestParam("sex") String sex,
  24. @RequestParam("email") String email,
  25. @RequestParam("did") int did){
  26. //多个参数的sql语句可以用?这种占位符,执行方法是按照顺序传参
  27. String sql = "update t_emp set emp_name=?,age=?,sex=?,email=?,did=? where eid=?";
  28. return jt.update(sql,empName,age,sex,email,did,eid);
  29. }
  30. }