教你用springboot连接mysql并实现增删改查
1.数据库与数据表的创建
创建名为mybatis的数据库:
create database mybatis;
创建名为user2的数据表:
use mybatis; create table user2( id integer not null primary key, name varchar(20) not null, pwd varchar(10) not null, perms varchar(100) null)
生成如下表结构:
(已经插入了两行数据的)
2.数据库的连接
注意点:url要设置serverTimezone
比如:jdbc:mysql://localhost:3306?serverTimezone=UTC
连接成功后可以在idea中简单测试一下:
3.结构:
4.JDBCController.java
package com.kuang.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.Map; import java.util.List; import java.lang.*; import java.util.Scanner; @RestController public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; //查询数据库的所有信息 //没有实体类,数据库里的东西,怎么获取? Map //查 @GetMapping("/userList") public List<Map<String,Object> > userList(){ String sql="select * from mybatis.user2"; List<Map<String,Object>> list_maps=jdbcTemplate.queryForList(sql); return list_maps; } //增 @GetMapping("/addUser") public String addUser(){ String sql="insert into mybatis.user2(id,name,pwd,perms) values(3,'xiaoming','123456','NULL')"; jdbcTemplate.update(sql); return "add-ok"; } //改 @GetMapping("updateUser/{id}/{name}/{pwd}") public String updateUser(@PathVariable("id") int id,@PathVariable("name") String name,@PathVariable("pwd") String pwd){ String sql="update mybatis.user2 set name=?,pwd=? where id="+id; //封装Object Object[] objects = new Object[2]; objects[0]=name; objects[1]=pwd; jdbcTemplate.update(sql,objects); return "update-ok"; } //删 @GetMapping("/deleteUser/{id}") public String deleteUser(@PathVariable("id") int id){ String sql="delete from mybatis.user2 where id=?"; jdbcTemplate.update(sql,id); return "delete-ok"; } }
5.application.yml
spring: datasource: username: root password: liding url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver
6.Springboot04DataApplication.java
package com.kuang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboot04DataApplication { public static void main(String[] args) { SpringApplication.run(Springboot04DataApplication.class, args); } }
7.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kuang</groupId> <artifactId>springboot-04-data</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-04-data</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.5</version> </plugin> </plugins> </build> </project>
8.pom.xml文件中注意事项
1)<plugin> </plugin>之间的语句爆红
加上与parent中相同的version号即可;
2)建立项目时勾选以下模块 spring web JDBC API mysql Driver
9.查询user
启动项目
输入:localhost:8080/userList
10.修改user
输入:localhost:8080/updateUser/5/hahahaha/1455
(说明:修改id为5的user,名字改为hahahaha,密码改为1455)
修改后的数据表:
11.增加user
输入:localhost:8080/addUser
(这里先写一个简单静态的addUser吧,写了半天类似与updateUser的一直报错)
修改后的数据表:
12.删除user
输入:localhost:8080/deleteUser/3
(删除id为3的user)
修改后的数据表:
到此这篇关于教你用springboot连接mysql并实现增删改查的文章就介绍到这了,更多相关springboot连接mysql内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)