springboot整合jdbc
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.52.3:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jdbc</name>
<description>Spring Boot整合jdbc</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.example.jdbc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class SpringBootJdbcApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
// 查看默认的数据源 class com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
// 获得数据库连接
Connection connection = dataSource.getConnection();
// HikariProxyConnection@1242874959 wrapping com.mysql.cj.jdbc.ConnectionImpl@f08fdce
// Hikari使用jdbc实现
System.out.println(connection);
// springboot中会有大量的 xxxTemplate,就是springboot为我们写好的bean,直接拿来用就可以了
// 在实际开发中我们用JdbcTemplate即可,DataSource在有需要的时候用就行
connection.close();
}
}
package com.example.jdbc.controller;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("userList")
public List<Map<String,Object>> userList(){
String sql = "select * from user";
return jdbcTemplate.queryForList(sql);
}
@GetMapping("addUser")
public void addUser(){
String sql = "insert into user values (3,'弗兰克','9999')";
jdbcTemplate.update(sql);
}
@GetMapping("updateUser")
public void updateUser(){
String sql = "update user set pwd=? where id=?";
Object[] objects = new Object[2];
objects[0] = 2;
objects[1] = 1;
jdbcTemplate.update(sql,objects);
}
@GetMapping("delUser/{id}")
public void delUser(@PathVariable("id") String id){
String sql = "delete from user where id = ?";
jdbcTemplate.update(sql,id);
}
}
springboot整合Druid数据源
父母和外婆都不再是能够更改心中珍视之物先后顺序的年纪。
你的名字
新海诚
部分内容转载自:
https://www.bilibili.com/video/BV1PE411i7CV?p=32&spm_id_from=pageDriver&vd_source=64c73c596c59837e620fed47fa27ada7