Springboot 整合JDBC
对于数据访问层,无论是 SQL(关系型数据库) 还是 NOSQL(非关系型数据库),Spring Boot 底层都是采用 Spring Data 的方式进行统一处理。
Spring Boot 底层都是采用 Spring Data 的方式进行统一处理各种数据库
Sping Data 官网:https://spring.io/projects/spring-data
1 SpringBoot 整合JDBC
1 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2 springboot 配置文件 配置数据库连接信息
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai
3 测试数据库是否连接上
- 分析SpringBoot 数据源自动装配
// 使@ConfigurationProperties 注解的类生效。
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })
public class DataSourceAutoConfiguration {
}
我们能够修改的属性在DataSourceProperties中
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
}
4 测试数据源
@SpringBootTest
class SpringbootJdbcApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
System.out.println(dataSource);
}
}
结果
class com.zaxxer.hikari.HikariDataSource
HikariProxyConnection@839259983 wrapping com.mysql.cj.jdbc.ConnectionImpl@42fb8c87
2 SpringBoot JDBC 增删改查
SpringBoot 自动装配JdbcTemplate 帮助我们进行数据库操作
1 搜索JdbcTemplateAutoConfiguration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(JdbcProperties.class)
@Import({ JdbcTemplateConfiguration.class, NamedParameterJdbcTemplateConfiguration.class })
public class JdbcTemplateAutoConfiguration {
}
2 进入JdbcTemplateConfiguration.class
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(JdbcOperations.class)
class JdbcTemplateConfiguration {
@Bean
@Primary
JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
JdbcProperties.Template template = properties.getTemplate();
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
}
return jdbcTemplate;
}
}
1 使用JDBCtemplate 进行增删改查
@RestController
public class JdbcController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/user")
public List<Map<String,Object>> userList(){
String sql = "select * from user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@RequestMapping("/addUser")
public String addUser(){
String sql = "insert into user(id,name,pwd) values (?,?,?)";
Object[] objects = new Object[3];
objects[0] = 4;
objects[1]="zz";
objects[2]="1234";
jdbcTemplate.update(sql,objects);
return "jdbcTmeplate-Add";
}
@RequestMapping("/updateUser/{id}")
public String update(@PathVariable("id") int id){
String sql = "update user set name=?, pwd=? where id ="+id;
Object[] objects = new Object[2];
objects[0]="updatezz";
objects[1]="1234";
jdbcTemplate.update(sql,objects);
return "jdbcTmeplate-update";
}
@RequestMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id){
String sql = "delete from user where id ="+id;
jdbcTemplate.update(sql);
return "jdbcTmeplate-delete";
}
}