SpringBoot整合Mybatis|入门级增删改查|2025

server/2025/2/5 0:34:26/

SpringBoot整合Mybatis

文章目录

  • SpringBoot整合Mybatis
    • 1. 新建User表
    • 2. 初始化项目
      • 2.1 新建项目
      • 2.2 配置数据库连接
      • 2.3 完善项目的架子
    • 3. 正式开始
      • 3.1 新增用户
      • 3.2 根据邮箱查询
      • 3.4 改密码 和 删除用户
      • 3.5 用xml再写一遍
    • 4. 进阶

1. 新建User表

CREATE DATABASE mybatis_demo;
USE mybatis_demo;CREATE TABLE user (id BIGINT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE,password VARCHAR(255) NOT NULL
);

2. 初始化项目

2.1 新建项目

这个项目是可以直接用IDEA创建的,就不给pom文件了。

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

创建好之后,记得去改改mvn的配置和jdk版本

2.2 配置数据库连接

数据库的ip、端口号、数据库名以及用户名和密码,改成自己的

spring:datasource:url: jdbc:mysql://192.168.171.135:3306/mybatis_demo?serverTimezone=UTC&useSSL=falseusername: new_userpassword: your_passworddriver-class-name: com.mysql.cj.jdbc.Drivermybatis:mapper-locations: classpath:mapper/*.xmlconfiguration:map-underscore-to-camel-case: true  # 下划线转驼峰 user_name  userName

2.3 完善项目的架子

在这里插入图片描述

3. 正式开始

3.1 新增用户

UserMapper里写一个insert接口:

package com.example.demo.mapper;import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;@Mapper
public interface UserMapper {@Insert("INSERT INTO user (username, email, password) VALUES (#{username}, #{email}, #{password})")@Options(useGeneratedKeys = true, keyProperty = "id")  // 自动获取数据库生成的主键IDint insertUser(User user);
}

UserService测试类里编写调用和测试的代码

package com.example.demo.service;import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public int insertUser(User user) {return userMapper.insertUser(user);}
}
package com.example.demo.unit;import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.assertEquals;@SpringBootTest
class UserServiceTest {@AutowiredUserService userService;@Testvoid insertUser(){int res=userService.insertUser(User.builder().email("123@example.com").password("password").username("urfread").build());assertEquals(1, res);}
}

这里可能会遇到重复录入的问题,后续再解决。

3.2 根据邮箱查询

@Select("SELECT * FROM user where email = #{email}")
User findUserByEmail(String email);

3.4 改密码 和 删除用户

@Update("UPDATE user SET password = #{password} WHERE email = #{email}")
int updateUserPassword(String password,String email);
@Delete("DELETE FROM user WHERE email = #{email}")
int deleteUserByEmail(String email);

3.5 用xml再写一遍

``UserMapper`只保留方法定义

package com.example.demo.mapper;import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserMapper {int insertUser(User user);User findUserByEmail(String email);int updateUserPasswordByEmail(String password, String email);int deleteUserById(Long id);
}
<?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.demo.mapper.UserMapper"><insert id="insertUser" parameterType="com.example.demo.domain.User">insert into user (email,password,username)values (#{email},#{password},#{username})</insert><update id="updateUserPasswordByEmail">update user set password = #{password} where email = #{email}</update><delete id="deleteUserById" parameterType="java.lang.Long">delete from user where id = #{id}</delete><select id="findUserByEmail" resultType="com.example.demo.domain.User">SELECT * FROM user WHERE email = #{email}</select>
</mapper>

再往后,逻辑删除、只更新非空字段、分页查询、事务等内容我都不太熟,所以都放在进阶里了。

4. 进阶


http://www.ppmy.cn/server/165022.html

相关文章

音视频入门基础:RTP专题(7)——RTP协议简介

一、引言 本文对RTP协议进行简介。在简介之前&#xff0c;请各位先下载RTP的官方文档《RFC 3550》和《RFC 3551》。《RFC 3550》总共有89页&#xff0c;《RFC 3551》总共有44页。本文下面所说的“页数”是指在pdf阅读器中显示的页数&#xff1a; 二、RTP协议简介 根据《RFC 35…

nvm的安装和使用

打开地址下载 https://github.com/coreybutler/nvm-windows/releases 推荐下载&#xff0c;nvm-setup.zip 这个。可能有的教程会让下载nvm-noinstall.zip 。noinstall确实下载之后不用安装&#xff0c;但是要自己配置setting.txt文件&#xff0c;以及环境变量 。 安装nvm 在电…

深度学习-98-大语言模型LLM之基于langchain的代理create_react_agent工具

文章目录 1 Agent代理1.1 代理的分类1.2 ReAct和Structured chat2 代理应用ReAct2.1 创建工具2.1.1 嵌入模型2.1.2 创建检索器2.1.3 测试检索结果2.1.4 创建工具列表2.2 初始化大模型2.3 创建Agent2.4 运行Agent3 参考附录1 Agent代理 Agent代理的核心思想是使用语言模型来选择…

数据结构 树2

文章目录 前言 一&#xff0c;二叉搜索树的高度 二&#xff0c;广度优先VS深度优先 三&#xff0c;广度优先的代码实现 四&#xff0c;深度优先代码实现 五&#xff0c;判断是否为二叉搜索树 六&#xff0c;删除一个节点 七&#xff0c;二叉收索树的中序后续节点 总结 …

Go学习:格式化输入输出

目录 1. 输出 2. 输入 1. 输出 常用格式&#xff1a; 格式说明%d整型格式%s字符串格式%c字符格式%f浮点数格式%T操作变量所属类型%v自动匹配格式输出 简单示例代码&#xff1a; package mainimport "fmt"func main() {a : 10b : "abc"c : ad : 3.14/…

【深度分析】DeepSeek大模型技术解析:从架构到应用的全面探索

深度与创新&#xff1a;AI领域的革新者 DeepSeek&#xff0c;这个由幻方量化创立的人工智能公司推出的一系列AI模型&#xff0c;不仅在技术架构上展现出了前所未有的突破&#xff0c;更在应用领域中开启了无限可能的大门。从其混合专家架构&#xff08;MoE&#xff09;到多头潜…

Spring WebFlux揭秘:下一代响应式编程框架,与Spring MVC有何不同?

Spring WebFlux和Spring MVC都是Spring家族里的成员&#xff0c;它们都能帮助我们开发Web应用&#xff0c;但工作方式有所不同。 可以把Spring MVC想象成一个服务员&#xff0c;每次有客人&#xff08;请求&#xff09;来&#xff0c;它就会专门找一个服务员&#xff08;线程&a…

数仓ETL测试

提取&#xff0c;转换和加载有助于组织使数据在不同的数据系统中可访问&#xff0c;有意义且可用。ETL工具是用于提取&#xff0c;转换和加载数据的软件。在当今数据驱动的世界中&#xff0c;无论大小如何&#xff0c;都会从各种组织&#xff0c;机器和小工具中生成大量数据。 …