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>

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

相关推荐
繁依Fanyi1 小时前
旅游心动盲盒:开启个性化旅行新体验
java·服务器·python·算法·eclipse·tomcat·旅游
IvorySQL3 小时前
济南站活动回顾|IvorySQL中的Oracle XML函数使用示例及技术实现原理
xml·数据库·sql·postgresql·oracle·开源
企业码道刘某5 小时前
EasyExcel 大数据量导入导出解决方案进阶
java·后端·mybatis
计算机学姐7 小时前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
小徐敲java20 小时前
通用mybatis-plus查询封装(QueryGenerator)
mybatis
OEC小胖胖21 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
计算机学姐1 天前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
落落落sss1 天前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
海绵波波1071 天前
Qt操作主/从视图及XML——实例:汽车管理系统
xml·qt·汽车
罗曼蒂克在消亡1 天前
2.3MyBatis——插件机制
java·mybatis·源码学习