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
------------------------------------------------------------------
-----------------------------------------------------------
---------------------------------------------
朦胧的夜 留笔~~
-----------------------------------------------------------
---------------------------------------------
朦胧的夜 留笔~~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)