配置文件
application.yml
yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/rbac?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
rbac为我自己本地的数据库,可以进行替换,后面的characterEncoding是更改编码方式为utf-8,serverTimezone为设置时区
其中数据库可以提前导入到IDEA的数据库配置连接中。
测试连接
在test里面进行测试链接是否成功
java
package com.learn.jdbc_demo;
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 JdbcDemoApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
//查看默认的数据源:class com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
//获得数据库连接
try {
Connection connection = dataSource.getConnection();
//链接配置:HikariProxyConnection@2084912180 wrapping com.mysql.cj.jdbc.ConnectionImpl@17ff8810
System.out.println(connection);
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
测试返回数据
在web中测试是否可以返回到前端
java
package com.learn.jdbc_demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查询数据库的所有信息
//没有实体类,数据库的东西,怎么获取? Map
@GetMapping("/userList")
public List<Map<String,Object>> userList(){
String sql = "select * from my_dept";
List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
}
简单实现增删改查
java
package com.learn.jdbc_demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
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;
//查询数据库的所有信息
//没有实体类,数据库的东西,怎么获取? Map
@GetMapping("/userList")
public List<Map<String,Object>> userList(){
String sql = "select * from my_user_job";
List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@GetMapping("/addUser")
public String addUser(){
String sql = "insert into rbac.my_user_job(user_id,job_id) values(1,11)";
jdbcTemplate.update(sql);
return "updata-ok";
}
@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id){
String sql = "update rbac.my_user_job set job_id=? where user_id="+id;
//封装
Object[] objects =new Object[1];
objects[0] = "1";
jdbcTemplate.update(sql,objects);
return "updata-ok";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id")int id){
String sql = "delete from rbac.my_user_job where user_id=?";
jdbcTemplate.update(sql,id);
return "delete-ok";
}
}