SpringBoot--使用JDBC连接mysql

1、导入包
    导入mysql和springJDBC的关系依赖包
 
1
2
3
4
5
6
7
8
9
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
 
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

    

2、添加数据库配置
 
 
3、创建表
 
1
2
3
4
5
6
CREATE TABLE `t_user` (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '主键自增',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(50) NOT NULL COMMENT '密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

4、创建对象实体及操作数据库对象
 
  (1)实体对象
  
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo.entity;
 
import lombok.Data;
 
@Data
public class User {
    private Long id;
    private String username;
    private String password;
 
 
}

  (2)操作数据库对象

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.demo.dto;
 
 
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
 
@Repository
public class UserDto {
    @Autowired
    private final JdbcTemplate jdbcTemplate;
 
 
    public UserDto(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
 
    public int AddUser(User user){
        String sql = "insert into t_user(username, password) values(?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
    }
}

 5、添加Controller和Service

复制代码
package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/test")
@Api(value = "SpringBoot测试接口")
public class UserTestController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping(value ="/jdbc")
    @ApiOperation(value="整合jdbc测试")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "username", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "旧密码", required = true, dataType = "String")
    })
    public String test1(String username, String password){
        User u = new User();
        u.setUsername(username);
        u.setPassword(password);
        return userService.AddUser(u) + "";

    }

}
复制代码
package com.example.demo.service;

import com.example.demo.entity.User;

public interface UserService {
    int AddUser(User user);
}
复制代码
package com.example.demo.dto;


import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDto {
    @Autowired
    private final JdbcTemplate jdbcTemplate;


    public UserDto(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public int AddUser(User user){
        String sql = "insert into t_user(username, password) values(?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
    }
}
复制代码

 

 

6、测试

  

 

 

 

    

 

  说明:测试使用的是swagger,关于swagger的使用,请见 https://www.cnblogs.com/liconglong/p/11477401.html

 

 

 

 

 

 

 

 
posted @   李聪龙  阅读(18290)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示