Spring JDBC是 Spring Framewor的一个模块,是对JDBC的简单封装。提供了一个JdbcTemplate 对象简化JDBC的开发。
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.12</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.22</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency>
src\main\resources\jdbc.properties
#驱动名jdbc.driver=com.mysql.cj.jdbc.Driver# 数据库连接jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=GMT&rewriteBatchedStatements=true#数据库用户名称jdbc.user=root# 数据库用户密码jdbc.password=123456# 指定连接池的初始化连接数。取值应在minPoo1Size 与 maxPoo1Size 之间.Default∶3initialPoolSize=20# 指定连接池中保留的最大连接数. Default∶15maxPoolsize=100# 指定连接池中保留的最小连接数minPoolsize=10# 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default∶0maxIdleTime=600# 当连接池中的连接耗尽的时候c3pO一次同时获取的连接数. Default∶3acquireIncrement=5# JDBC的标准,用以控制数据源内加载的Preparedstatements数量。maxStatements=5# 每60秒检查所有连接池中的空闲连接.Default∶0idleConnectionTestPeriod=60
@Configuration@PropertySource("classpath:jdbc.properties")public class AppConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.password}")private String passwd;@Value("${jdbc.user}")private String user;@Value("${initialPoolSize}")private int initialPoolSize;@Value("${maxPoolsize}")private int maxPoolsize;@Value("${minPoolsize}")private int minPoolsize;@Value("${maxIdleTime}")private int maxIdleTime;@Value("${acquireIncrement}")private int acquireIncrement;@Value("${maxStatements}")private int maxStatements;@Value("${idleConnectionTestPeriod}")private int idleConnectionTestPeriod;@Beanpublic DataSource dataSource() throws PropertyVetoException {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setDriverClass(driver);dataSource.setJdbcUrl(url);dataSource.setUser(user);dataSource.setPassword(passwd);dataSource.setInitialPoolSize(initialPoolSize);dataSource.setMaxPoolSize(maxPoolsize);dataSource.setMinPoolSize(minPoolsize);dataSource.setMaxIdleTime(maxIdleTime);dataSource.setAcquireIncrement(acquireIncrement);dataSource.setMaxStatements(maxStatements);dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);return dataSource;}@Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource) {JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}}
@Testpublic void test() {AnnotationConfigApplicationContext ac =new AnnotationConfigApplicationContext(AppConfig.class);JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user_table");System.out.println(maps);}
具体API参考官网:https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc
//spring jdbc提供的数据源实现@Beanpublic DataSource springJdbcDataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName(driver);dataSource.setPassword(passwd);dataSource.setPassword(passwd);return dataSource;}
