Springboot整合JDBC

前言

Springboot已经帮我们把jdbc的操作给封装好了

叫做jdbc Template 类似的都叫做xxxx Template
例如RedisTemplate 等等

我们只需要做两件事情,导入相关的依赖,以及配置好我们的连接属性,使用即可

导入依赖

   <!--jdbc依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
<!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

关键就是这两个依赖,我们可以在创建项目的时候选中即可,Springboot会自动帮我们导入相关的依赖。
在这里插入图片描述
如果你使用mybatis来整合就需要选中Mybatis即可

配置连接属性

我们在application.yml中进行配置

server:
  port: 8081
spring:
  datasource:
    username: root
    password: 123456
    # 假如时区报错了就增加一个时区的配置 ServerTimezone=UTC
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

注意:如果使用高版本的Mysql(5以上的版本),需要加一个时区的配置。

测试

我们写一个Controller来测试jdbc

package com.cjh.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;

    //查询数据库的所有信息
    //没有实体类,数据库中的东西没办法获取  Map
    @GetMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql = "select * from user";
        List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }

    //增加一名用户
    @GetMapping("/addUser")
    public String addUser(){
        String sql = "insert into mybatis.user(id,name,pwd) values (4,'小明','123456')";
        jdbcTemplate.update(sql);
        return "update-ok";
    }

    //修改一名用户
    @GetMapping("/updateUser{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql = "update mybatis.user set name = ?,pwd=? where id = "+id;

        //封装
        Object[] objects = new Object[2];
        objects[0]="小明2";
        objects[1]="zzzz";
        jdbcTemplate.update(sql,objects);
        return "update-ok";
    }

    //删除一名用户
    @GetMapping("/delUser{id}")
    public String delUser(@PathVariable("id") int id){
        String sql = "delete from mybatis.user where id=?";
        jdbcTemplate.update(sql,id);
        return "update-ok";
    }
}

我们只需要拿出JdbcTemplate即可使用。

 @Autowired
    JdbcTemplate jdbcTemplate;
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot整合MyBatis-Plus非常简单,可以按照以下步骤进行操作: 1. 添加MyBatis-Plus和相关依赖:在`pom.xml`文件添加以下依赖: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis-Plus Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 自行替换`最新版本号`为MyBatis-Plus的最新版本号。 2. 配置数据源信息:在`application.properties`(或`application.yml`)文件配置数据库连接信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建实体类和Mapper接口:创建与数据库表对应的实体类,使用`@TableName`注解指定表名,然后创建对应的Mapper接口,继承自`BaseMapper`。 ```java @TableName("user") // 指定对应的表名 public class User { private Long id; private String name; // getter和setter方法省略... } public interface UserMapper extends BaseMapper<User> { } ``` 4. 编写业务逻辑:可以创建Service层来封装业务逻辑,使用`@Service`注解标记为Spring的Bean。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.selectById(id); } // 其他业务方法... } ``` 5. 使用MyBatis-Plus提供的API:MyBatis-Plus提供了丰富的API,可以方便地操作数据库,例如查询、插入、更新和删除等操作。 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users/{id}") public User getUser(@Pa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我菜菜就好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值