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

2023-12-13 03:57:56

先来"增"操作

在UserInfoXMLMapper.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="insert">
        insert into userinfo(username,password,age,gender,phone)
        values(#{username},#{password},#{age},#{gender},#{phone})
    </insert>
</mapper>

然后在 UserInfoXMLMapper 接口中写

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

然后进行完善

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

<?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 里面加上插入的结果

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

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

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 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>

这样就能插入成功了?

接下来是"删"操作

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 的数据删了

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 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>

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

文章来源:https://blog.csdn.net/qq_40841463/article/details/134820972
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。