Mybatis XML增删操作(结合上文)

先来"增"操作

在UserInfoXMLMapper.xml里面写

html 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfoXMLMapper">
   
    <insert id="insert">
        insert into userinfo(username,password,age,gender,phone)
        values(#{username},#{password},#{age},#{gender},#{phone})
    </insert>
</mapper>

然后在 UserInfoXMLMapper 接口中写

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserInfoXMLMapper {
    
    Integer insert(UserInfo userInfo);

}

然后右键,generate,test,勾选insert,ok

然后进行完善

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfoXMLMapperTest {
@Autowired
    private UserInfoXMLMapper userInfoXMLMapper;
    
    @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("haha");
        userInfo.setPassword("666");
        userInfo.setAge(22);
        userInfo.setGender(1);
        userInfo.setPhone("556699");
        Integer result = userInfoXMLMapper.insert(userInfo);
        log.info("影响的行数:{}",result);
    }
}

插入成功

如果我们想插入完以后拿到它的 id 该怎么拿?

在 UserInfoXMLMapper.xml 稍作修改,加上 useGenerateKeys 和 keyProperty

html 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfoXMLMapper">
    
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into userinfo(username,password,age,gender,phone)
        values(#{username},#{password},#{age},#{gender},#{phone})
    </insert>
</mapper>

然后在 UserInfoXMLMapperTest 的log.info 里面加上插入的结果

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfoXMLMapperTest {
@Autowired
    private UserInfoXMLMapper userInfoXMLMapper;
    

    @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("haha");
        userInfo.setPassword("666");
        userInfo.setAge(22);
        userInfo.setGender(1);
        userInfo.setPhone("556699");
        Integer result = userInfoXMLMapper.insert(userInfo);
        log.info("影响的行数:{},插入结果的id:{}",result,userInfo.getId());
    }
}

插入成功

在数据库中也能看到确实是6

插入数据如何进行重命名呢?

用@Param

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserInfoXMLMapper {
   
    Integer insert(UserInfo userInfo);

    Integer insert2(@Param("userInfo2") UserInfo userInfo);
}

然后右键,Generate,test,勾选insert2,ok

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfoXMLMapperTest {
@Autowired
    private UserInfoXMLMapper userInfoXMLMapper;
   

      @Test
    void insert2() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("haha");
        userInfo.setPassword("666");
        userInfo.setAge(22);
        userInfo.setGender(1);
        userInfo.setPhone("556699");
        Integer result = userInfoXMLMapper.insert2(userInfo);
        log.info("影响的行数:{},插入结果的id:{}",result,userInfo.getId());
    }
}

然后在 UserInfoXMLMapper.xml 里面,每个字段前面都要加上 UserInfo2(@Param后面的内容)

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mybatisdemo.mapper.UserInfoXMLMapper">

    <insert id="insert2" useGeneratedKeys="true" keyProperty="id">
        insert into userinfo(username,password,age,gender,phone)
        values(#{userInfo2.username},#{userInfo2.password},#{userInfo2.age},#{userInfo2.gender},#{userInfo2.phone})
    </insert>
</mapper>

这样就能插入成功了

接下来是"删"操作

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserInfoXMLMapper {
    
    Integer delete(Integer id);

}

然后右键,Generate,test,勾选delete,ok

把Id为8 的数据删了

java 复制代码
package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
class UserInfoXMLMapperTest {
@Autowired
    private UserInfoXMLMapper userInfoXMLMapper;
   

    @Test
    void delete() {
        userInfoXMLMapper.delete(8);
    }
}

然后在 UserInfoXMLMapper.xml 里面

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mybatisdemo.mapper.UserInfoXMLMapper">

    <delete id="delete">
        delete from userinfo where id=#{id}
    </delete>
</mapper>

在数据库中再次验证是否删除,上面是删除前,下面是删除后,删除成功了

相关推荐
Habibei1 小时前
Mybatis的分页插件
sql·mybatis
好奇的菜鸟2 小时前
MyBatis TypeHandler 详解与实战:FastJson 实现字符串转 List
数据结构·list·mybatis
图书馆钉子户10 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
m0_7482554111 小时前
适用于IntelliJ IDEA 2024.1.2部署Tomcat的完整方法,以及笔者踩的坑,避免高血压,保姆级教程
java·tomcat·intellij-idea
best_virtuoso13 小时前
XML DOM4J 三、XPath
xml
步、步、为营18 小时前
解锁C# XML编程:从新手到实战高手的蜕变之路
xml·数据库·c#
itsok719 小时前
最新版本SpringAI接入DeepSeek大模型,并集成Mybatis
mybatis
中东大鹅19 小时前
Tomcat 目录结构和应用实现
java·tomcat·firefox
Aphelios38019 小时前
Linux 权限系统和软件安装(二):深入理解 Linux 权限系统
java·linux·运维·服务器·tomcat
笨蛋不要掉眼泪21 小时前
深入解析Tomcat目录结构
java·tomcat