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>

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

相关推荐
鹿屿二向箔4 小时前
基于SSM(Spring + Spring MVC + MyBatis)框架的汽车租赁共享平台系统
spring·mvc·mybatis
沐雪架构师7 小时前
mybatis连接PGSQL中对于json和jsonb的处理
json·mybatis
鹿屿二向箔9 小时前
基于SSM(Spring + Spring MVC + MyBatis)框架的咖啡馆管理系统
spring·mvc·mybatis
aloha_78918 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
毕业设计制作和分享19 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
尢词20 小时前
SpringMVC
java·spring·java-ee·tomcat·maven
清风百草20 小时前
【04】【Maven项目热部署】将Maven项目热部署到远程tomcat服务器上
tomcat·maven项目热部署
paopaokaka_luck1 天前
基于Spring Boot+Vue的助农销售平台(协同过滤算法、限流算法、支付宝沙盒支付、实时聊天、图形化分析)
java·spring boot·小程序·毕业设计·mybatis·1024程序员节
cooldream20091 天前
Spring Boot中集成MyBatis操作数据库详细教程
java·数据库·spring boot·mybatis
不像程序员的程序媛1 天前
mybatisgenerator生成mapper时报错
maven·mybatis