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

先来"增"操作

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

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

  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值