上一篇:15-Springboot其他数据源配置之Druid
https://blog.csdn.net/fsjwin/article/details/109742300
有了数据源也白搭,不管是内置的HikariDataSource还是用的比较多的第三方数据源DruidDataSource,还是ADataSource、BDataSrouce,因为有了数据源,的有人用起来才行呀,有人会说有呀Mybatis就可以呀,当然Mybatis这位大哥当然可以,但是本节,我们先介绍一个小弟,下节在请出大哥。
JdbcTemplate及时对DataSource的封装,使我们可以在Spring框架内部解决数据裤访问的问题。
1. JdbcTemplate和DataSource关系。
2. JdbcTemplate实例
写一个实例,加深印象
2.1 建表
CREATE TABLE `student` (
`id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
2.2 application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1/yuhl?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
server:
port: 9999
2.3 DruidConfig.java
package com.yuhl;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @author yuhl
* @Date 2020/11/16 11:15
* @Classname DruidConfig
* @Description TODO
*/
@Configuration
public class DruidConfig {
/*
这样的话才会绑定上下面的属性:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new DruidDataSource();
}
}
2.4 JDBCTemplateController
package com.yuhl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
/**
* @author yuhl
* @Date 2020/11/14 15:27
* @Classname HelloController
* @Description TODO
*/
@RestController
public class JDBCTemplateController {
//注入JdbcTemplate 用于操作数据库
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/jdbct")
public List<Map<String, Object>> hello() {
List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from student");
return maps;
}
}
3. JdbcTemplate测试
页面访问:
4. 总结
JdbcTemplate就是对数据源的封装,在不引入其他框架的基础上,在spirng的内部完成对数据库的访问和业务开发,但是在项目上我们更多会使用mybatis等框来做这件事,更少使用jdbcTemplate。
下一篇:17-Springboot整合mybatis注解版Mapper https://blog.csdn.net/fsjwin/article/details/109744677