SpringBoot增删该查

news/2024/10/10 23:01:58

SpringBoot+Mybatis增删该查()

1、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>3.3.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>SpringBootMybatis</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringBootMybatis</name><description>SpringBootMybatis</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>3.0.3</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、配置文件properties

spring.application.name=SpringBootMybatis
server.port=8088spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#mybatis.config-location=classpath:mybatis/mybatis-config.xml
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.mapper-locations=classpath:mapper/*.xml

3、项目目录

image-20241010224549673

image-20241010224703347

4、代码

User

package com.example.springbootmybatis.domain;public class User {private Long id; // 主键IDprivate String name; // 姓名private String password; // 密码private Integer age; // 年龄private String email; // 邮箱private String updateTime; // 修改时间public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getUpdateTime() {return updateTime;}public void setUpdateTime(String updateTime) {this.updateTime = updateTime;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", password='" + password + '\'' +", age=" + age +", email='" + email + '\'' +", updateTime='" + updateTime + '\'' +'}';}
}

UserMapper

package com.example.springbootmybatis.mapper;import com.example.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;//@Mapper
public interface UserMapper {
//     @Select("select * from user")List<User> getList();int deleteById(Long id);int insert(User user);User getById(Long id);int update(User user);
}

resources/mapper/UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatis.mapper.UserMapper"><select id="getList"  resultType="com.example.springbootmybatis.domain.User">select  * from user</select><delete id="deleteById"  parameterType="java.lang.Long">delete from user where id = #{id}</delete><insert id="insert" parameterType="com.example.springbootmybatis.domain.User">insert  into user(id,name,password,age,email) values(#{id},#{name},#{password},#{age},#{email})</insert><update id="update" parameterType="com.example.springbootmybatis.domain.User">update user set name = #{name},password = #{password},age = #{age},email = #{email} where id = #{id}</update><select id="getById" resultType="com.example.springbootmybatis.domain.User">select  * from user where id = #{id}</select>
</mapper>

UserController

package com.example.springbootmybatis.controller;import com.example.springbootmybatis.domain.User;
import com.example.springbootmybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("test")public String test(){return "hello world";}@GetMapping("getList")public List<User> getList(){return userMapper.getList();}@GetMapping("getById/{id}")public User getById(@PathVariable Long id){System.out.println(id);return userMapper.getById(id);}@PostMapping("insert")public int insert(@RequestBody User user){System.out.println("--------------"+user.toString());return userMapper.insert(user);}@PostMapping("update")public int update(@RequestBody User user){System.out.println("--------------"+user.toString());return userMapper.update(user);}@DeleteMapping("deleteById/{id}")public int deleteById(@PathVariable Long id){System.out.println(id);return userMapper.deleteById(id);}
}

SpringBootMybatisApplication

package com.example.springbootmybatis;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.springbootmybatis.mapper")
public class SpringBootMybatisApplication {public static void main(String[] args) {SpringApplication.run(SpringBootMybatisApplication.class, args);}}

5、请求

image-20241010225336280

{"id": 55,"name": "John Doe","password": "password123","age": 30,"email": "john.doe@example.com","updateTime": "2023-10-10 12:00:00"
}
GET http://localhost:8088/getList
DELETE http://localhost:8088/deleteById/1
### 根据ID查询用户信息
GET http://localhost:8088/getById/3

image-20241010225521628

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/69989.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

20222403 2024-2025-1 《网络与系统攻防技术》实验一实验报告

1.实验内容 本周学习内容 1.熟悉基本的汇编语言指令及其功能。 2.掌握了栈与堆的概念及其在进程内存管理中的应用以及用户态与内核态的区别。 3.熟练运用了Linux系统下的基本操作命令。 2.实验过程 任务一 直接修改程序机器指令,改变程序执行流程 下载并解压目标文件pwn1,然后…

特斯拉 Robotaxi 发布会 All In One

特斯拉 Robotaxi 发布会 All In One Tesla Robotaxi特斯拉 Robotaxi 发布会 All In OneTesla RobotaxiX 直播链接Broadcasthttps://x.com/i/broadcasts/1YqJDkbjazvGV demosTesla Robotaxi Event on October 11th at 04:00.https://www.youtube.com/watch?v=FXGJl1gG6wI(🐞 …

海外SRC信息收集工具

海外SRC信息收集 ​ 子域名爆破工具:bbot,subfinder ​ 相关测评:https://blog.blacklanternsecurity.com/p/subdomain-enumeration-tool-face-off ​ bbot收集的子域名最多,subfinder跑的最快 ​ ​ bbot使用 ​ api配置:vim ~/.conf…

The 3rd Universal Cup 做题记录 (2)

The 3rd Universal Cup 做题记录 Stage 0 - Stage 9:The 3rd Universal Cup 做题记录 (1) Stage 10 - Stage 19:The 3rd Universal Cup 做题记录 (2) The 3rd Universal Cup. Stage 10: West Lake A. Italian Cuisine 复制一遍,枚举 \(i\) 维护右端点 \(j\)。要求 \((x,y)\)…

月灵4.31传奇永恒安装教程+无需虚拟机+GM

今天给大家带来一款单机游戏的架设:月灵4.31传奇永恒服务端客户端1.0.3.94。 注意:这个是能开门的版本。 另外:本人承接各种游戏架设(单机+联网) 本人为了学习和研究软件内含的设计思想和原理,带了架设教程仅供娱乐。教程是本人亲自搭建成功的,绝对是完整可运行的,踩过…

软件工程第二次结对作业之程序实现

软件工程第二次结对作业之程序实现这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/SE2024这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/SE2024/homework/13281这个作业的目标 实现第一次结对作业设计的小程序学号 102202116结对成员学号 102202116李迦勒…

2024.10.10 鲜花(原 I 的交互程序改)

图论 2 I 的交互库Roads in E City夜曲 一群嗜血的蚂蚁 被腐肉所吸引 我面无表情 看孤独的风景 失去你 爱恨开始分明 失去你 还有什么事好关心 当鸽子不再象征和平 我终于被提醒 广场上喂食的是秃鹰 我用漂亮的押韵 形容被掠夺一空的爱情 啊 乌云开始遮蔽 夜色不干净 公园里 葬…

IDEA中git如何快捷的使用Cherry-Pick功能

前言 我们在使用IDEA开发时,一般是使用GIT来管理我们的代码,有时候,我们需要在我们开发的主分支上合并其他分支的部分提交代码。注意,是部分,不是那个分支的全部提交,这时候,我们就需要使用Cherry-Pick功能了。 对于不太习惯使用命令来操作GIT的我们来说,可以使用IDEA自…