pom.xml
<?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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBoot2jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot2jdbc</name>
<description>SpringBoot2jdbc</description>
<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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
操作数据源DataSource
package com.example.springboot2jdbc;
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 SpringBoot2jdbcApplicationTests {
@Autowired
DataSource dataSource;//数据源对象,自动装配
@Test
void contextLoads() throws SQLException {
//查看一下默认数据源:结果:class com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
//获取数据库链接
Connection connection =dataSource.getConnection();
System.out.println(connection);
//关闭
connection.close();
}
}
JDBC模板执行SQL操作
写在控制器里面的,执行调用jdbc模板
package com.example.springboot2jdbc.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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
//JDBC定义的模板,这个模板定义了一些写法,可以传入sql语句直接执行,不用自己写繁琐的jdbc了
@Autowired
JdbcTemplate jt;
@GetMapping("/q1")
public List<Map<String,Object>> queryEmps(){
return jt.queryForList("select * from t_emp");
}
@PostMapping("/u1")
public int update(@RequestParam("eid") int eid,
@RequestParam("empName") String empName,
@RequestParam("age") int age,
@RequestParam("sex") String sex,
@RequestParam("email") String email,
@RequestParam("did") int did){
//多个参数的sql语句可以用?这种占位符,执行方法是按照顺序传参
String sql = "update t_emp set emp_name=?,age=?,sex=?,email=?,did=? where eid=?";
return jt.update(sql,empName,age,sex,email,did,eid);
}
}