JDBC学习

配置文件

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";  
    }  
  
}
相关推荐
拽着尾巴的鱼儿7 分钟前
spring 动态代理
java·后端·spring
gf132111112 分钟前
python_【更新已发送的消息卡片】
java·前端·python
WL_Aurora17 分钟前
Java字符输入全攻略
java·开发语言
Traving Yu17 分钟前
向量数据库Milvus
数据库·人工智能·milvus
2501_9010064721 分钟前
golang如何使用DTM分布式事务框架_golang DTM分布式事务框架使用方法
jvm·数据库·python
2501_9012005326 分钟前
Golang如何做Clean Architecture_Golang整洁架构教程【详解】
jvm·数据库·python
咖啡里的茶i26 分钟前
实验三 数据完整性实验
数据库·oracle
韶博雅27 分钟前
oracle + parfile(数据泵)
数据库·oracle
weixin_4597539428 分钟前
Go 中嵌入类型字段在派生结构体字面量中的初始化规则详解
jvm·数据库·python
CLX050530 分钟前
HTML5中Mediastream实现摄像头画面实时捕获
jvm·数据库·python