Spring JDBC是 Spring Framewor的一个模块,是对JDBC的简单封装。提供了一个JdbcTemplate 对象简化JDBC的开发。

    1. <dependency>
    2. <groupId>org.springframework</groupId>
    3. <artifactId>spring-context</artifactId>
    4. <version>5.3.12</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework</groupId>
    8. <artifactId>spring-jdbc</artifactId>
    9. <version>5.3.12</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>mysql</groupId>
    13. <artifactId>mysql-connector-java</artifactId>
    14. <version>8.0.22</version>
    15. </dependency>
    16. <dependency>
    17. <groupId>c3p0</groupId>
    18. <artifactId>c3p0</artifactId>
    19. <version>0.9.1.2</version>
    20. </dependency>

    src\main\resources\jdbc.properties

    1. #驱动名
    2. jdbc.driver=com.mysql.cj.jdbc.Driver
    3. # 数据库连接
    4. jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=GMT&rewriteBatchedStatements=true
    5. #数据库用户名称
    6. jdbc.user=root
    7. # 数据库用户密码
    8. jdbc.password=123456
    9. # 指定连接池的初始化连接数。取值应在minPoo1Size 与 maxPoo1Size 之间.Default∶3
    10. initialPoolSize=20
    11. # 指定连接池中保留的最大连接数. Default∶15
    12. maxPoolsize=100
    13. # 指定连接池中保留的最小连接数
    14. minPoolsize=10
    15. # 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default∶0
    16. maxIdleTime=600
    17. # 当连接池中的连接耗尽的时候c3pO一次同时获取的连接数. Default∶3
    18. acquireIncrement=5
    19. # JDBC的标准,用以控制数据源内加载的Preparedstatements数量。
    20. maxStatements=5
    21. # 每60秒检查所有连接池中的空闲连接.Default∶0
    22. idleConnectionTestPeriod=60
    1. @Configuration
    2. @PropertySource("classpath:jdbc.properties")
    3. public class AppConfig {
    4. @Value("${jdbc.driver}")
    5. private String driver;
    6. @Value("${jdbc.url}")
    7. private String url;
    8. @Value("${jdbc.password}")
    9. private String passwd;
    10. @Value("${jdbc.user}")
    11. private String user;
    12. @Value("${initialPoolSize}")
    13. private int initialPoolSize;
    14. @Value("${maxPoolsize}")
    15. private int maxPoolsize;
    16. @Value("${minPoolsize}")
    17. private int minPoolsize;
    18. @Value("${maxIdleTime}")
    19. private int maxIdleTime;
    20. @Value("${acquireIncrement}")
    21. private int acquireIncrement;
    22. @Value("${maxStatements}")
    23. private int maxStatements;
    24. @Value("${idleConnectionTestPeriod}")
    25. private int idleConnectionTestPeriod;
    26. @Bean
    27. public DataSource dataSource() throws PropertyVetoException {
    28. ComboPooledDataSource dataSource = new ComboPooledDataSource();
    29. dataSource.setDriverClass(driver);
    30. dataSource.setJdbcUrl(url);
    31. dataSource.setUser(user);
    32. dataSource.setPassword(passwd);
    33. dataSource.setInitialPoolSize(initialPoolSize);
    34. dataSource.setMaxPoolSize(maxPoolsize);
    35. dataSource.setMinPoolSize(minPoolsize);
    36. dataSource.setMaxIdleTime(maxIdleTime);
    37. dataSource.setAcquireIncrement(acquireIncrement);
    38. dataSource.setMaxStatements(maxStatements);
    39. dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
    40. return dataSource;
    41. }
    42. @Bean
    43. public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    44. JdbcTemplate jdbcTemplate = new JdbcTemplate();
    45. jdbcTemplate.setDataSource(dataSource);
    46. return jdbcTemplate;
    47. }
    48. }
    1. @Test
    2. public void test() {
    3. AnnotationConfigApplicationContext ac =
    4. new AnnotationConfigApplicationContext(AppConfig.class);
    5. JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
    6. List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user_table");
    7. System.out.println(maps);
    8. }

    具体API参考官网:https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc

    1. //spring jdbc提供的数据源实现
    2. @Bean
    3. public DataSource springJdbcDataSource() {
    4. DriverManagerDataSource dataSource = new DriverManagerDataSource();
    5. dataSource.setDriverClassName(driver);
    6. dataSource.setPassword(passwd);
    7. dataSource.setPassword(passwd);
    8. return dataSource;
    9. }